Chapter 8

Student Learning Outcomes
1. Declare and initalize an array
2. Use subscripts with an array
3. Declare and use arrays of objects.
4. Search an array
5. Pass arrays to and return arrays from metnods.


Important Points
1. Declaring and initalizing an Array.  an array is a named list of data items that allhave the same type.
a. double[] salesFigure;   int[] idnum;
b. once you create an array you must allocate memory for it with  double[] sale;  (declares array) sale=new double[20];(reserves memory with 20 elements.
or double[] sale= new double[20];  each element is referred to as a subscript.  subscript 15 hold specific data. elements are 0 to 19.  First element is 0.
2. Subscripts
a. sale[0]=2100.00;  puts 2100.00 into the first element of the array whose subscript is 0.
b. system.out.println(sale[19]);  prints out the data in element 19.
c.  JAVA allows variables to set the array elements which must be integers.

Code using Uses
double [] money=new double[10]; literal integer constant
double[] money=new double[NUMBER_ELS]; named integer constant
double[] money=new double[numberOfEls]; integer variable
double[] money=new double[x+y*z]; calculation -
double[] money=new double[getElements()]; methods return value

d. A variable that has a primative type such as int holds a value. A variable with a reference type (array) holds a memory address where a value is stored.
e. When the keyword new is  used to define an array and array name acquires actual memory address value.
f. int[] someNumbs=new int[10];  each element is assinged 0,  double and float array is assigned 0.0, char is assigned '\u0000' and bollean is assigned false.
g. To initalize an array you use a list of values seperated by commas enclose in curly braces also called populating the array. When you initilize an array you do not have to use the word new.  The number of elements in the array are equal to the length of the list of  provided values.
h. String[] studentName ={"Sam","Peter","Mary"};  intilizes the studenName array with three elements 0=Sam, 1=Peter,2=Mary.
i. In JAVA you can not directly initilaze part of an array.
j. When a program contain an array that you want to use every element it is common to use loops that vary the loop control variable from 0 to one less than the size of the array.
k. int[] scoreArray={2,14,35,67,85};
    final int INCREASE=3;
    for(sub=0;sub<5;++sub)          FOR A 100 ELEMENT ARRAY  sub<100 
    scoreArray[sub]+= INCREASE;  this will add 3 to each element of the array 0=5,1=17,2=38,3=70, 4=88.
l.  you can use a symbolic constant to set the loop condition
    final int NUMBER_OF_SCORES=5                                       the advantage of symbolic constant is you know all elements of the array will be processed and
    for (sub=0;sub<NUMBER_OF_SCORES;++sub)                  if the array size changes because your remove or add scores all the loops that use it can be
   scoreArray[sub]+=INCREASE;                                               changed in one place.
m. The length field contain the number of elements in the array.
    for(sub=0;sub<scoreArray.length;++sub)                                  length is called a  property of the  object scoreArray
n. Enyhanced for loop allows you to cycle through an array without specifying the starting and ending point for the loop control variable. foreachloop
for(int val : scoreArray)
    System.out.println(val);    val is defined to be the same type as the array named following the colon. val takes on in turn each value in the array.
3. Declaring and  Using  Arrays of Objects: you can declare arrays that hold elements of any type, objects included.
a. Employee[] emp=new Employee[7]   this creates array with elements 0 to 6. Does not construct Employee  objects: you must call the severn individual constructors in the Employee Class.
b. for(int x=0;x<emp.length;++x)
    system.out.println(emp[x].getEmpNum() + " " +emp[x].getSalary()); 
note: placement of bracketed subscript means the method belongs to a particular employee:
c. Enhanced loop - for(Employee worker : emp)                                   
worker is a local variable that represents each element of emp in turn.    
   System.out.println(worker.getEmpNum() + " " + worker.getsalary();
d. Arrays of Strings
    String[] deptName={"Accounting","Human Resources","Sales"}; 
declares string array that has elements 0,1,2  Initilized arrays length is equal to the number of elements in the array.
for(int a=0;a<deptName.length;++a)                                           
  this prints out each department name.
4. Search an array
    int[] validValues={101,108,201,213,266,304,311,409,411,412};
    for (intemOrdered==validValues.length;++x)        
 since validValues.length will return whatever length the array is, this code could be used if array  was 1000 or more elements.
checks each element of the array and returns true if item found.
a. Parallel array is one with the same number of elements as another in which the values are related. int[] testScore={89,78,70,95] String[] studNames={"Harry","June","Pete","Mary"] are two parallel arrays that are releated in that testScore[0] is assigned to studNames[0].
b.   for(int x=0;x<NUMBER_OF_ITEMS;++x)
This allows the loop to terminate as soon as the correct element is found otherwise it will continue looping
c. Range match - create two corresonding arrays in which you compare a value to the end points of numerical ranges to find. the catagory in which a value belongs.
    final int NUM_RANES=5;
    int[] discountRangeLimit={1,13,50,100, 200};
    double[] discountRate=[0,0.10,0.14,0.18,0.20};
    double customerDiscount;
    String strNumOrdered:
    int numOrdered;
    int sub=NUM_RANGES-1;
    String strNumOrdered=JOptionPane.showInputDialog(null, "How many items are ordered?");
    while(sub>=0 && numOrdered<discountRangeLimit[sub])--sub;   
    JOptionPane.showMessageDialog(null, "Discount rate for " + numOrdered + " items is " + customerDiscount);
5. Passing Arrays to and Returning Arrays from Methods. You can pass a single array element to a method in exactly the same manner as you pass a variable.
a. Passed by Value - Individual array elements are passed by value, that is a copy of the value is made and used within the receiving method
b. Reference types - array hold locations not data - you cannot assign another array to it using the = operator, nor can you compare tow arrays using the == operator.
c. When array is passed to method the method gets a copy of the arrays memory address which means the receiving method can alter the data.
d. Returning an array from a Method.
    public static int[] getArray()
    int[] scores=[90,80,70,60};
    return scores;