140 likes | 234 Views
CSE 1341 Honors. Professor Mark Fontenot Southern Methodist University Note Set 17. What if…. What if we needed to track the grades for 20 students, the GPAs for 10000 students, the balance for 10M checking accounts? one variable for each “thing” we need to track?
E N D
CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 17
What if… • What if we needed to track the grades for 20 students, the GPAs for 10000 students, the balance for 10M checking accounts? • one variable for each “thing” we need to track? • Technically, it would be possible to do it this way. • Ridiculously difficult • Extensibility? • Ability to perform a simple calculation on 10M accounts such as Pay interest might take 10M lines of code – one line for each calculation. • Making a deposit would require 10M if statements. • Simply impractical
Arrays • Allow programmer to refer to more than one object of the same type by using one name/identifier and an index. • index can be a variable • lend themselves well to use with loops
Declaration • Declaring an array • arrays are objects in Java • Create a reference variable to an array by using [] • instantiate an array object using new <dataType> [size of array] EXAMPLE: int [] grades; grades = new int[10]; OR int[] grades = new int[10]; [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] grades
Declaration • Size can be input by the user. Scanner s = new Scanner(System.in); int size = 0; int [] data; System.out.print(“Please enter the number of students: “); size = s.nextInt(); data = new int [size]; //and so on
Characteristics of Arrays • Zero subscripted • first slot (called element) has index of zero. • last element has index of (size – 1) • Homogeneously Typed • Every element store in array has to be of the same data type • Cannot change size once instantiated • Can tell you their size using data member length • int [] data = new int[1001];System.out.println( data.length );//prints 1001
Accessing Elements of Array • Use an index to access the elements. • can be a literal or variable int [] data = new int [20]; data[0] = 98; data[1] = 20; Literal int [] data = new int[20]; for (inti = 0; i < data.length; i++) data[i] = i + 2; variable
Array Example //Declare each element with the square of its index. //e.g. [2] would contain 4, [3] would contain 9, etc. public static void main (String [] args) { int [] data = new int [20]; for (inti = 0; i < data.length; i++) data [i] = i * i; for (inti = 0; i < data.length; i++) System.out.println(“data[“ + i + ”] = “ + data[i]); }
Arrays of Object References: Review • Remember Student stu1;only declares a reference variable to a Student object. • no actual Student object yet. stu1 ?
Arrays of Object References Student [] class = new Student[10]; Creates and array of 10 Student references. There are no student objects yet. for (inti = 0; i < class.length; i++) class[i] = new Student(); Instantiates a new object for each element of the array to point to.
Arrays of Object References • Access public interface of object from array element just like normal. System.out.println(class[2].getName()); class[3].setGPA(3.99); Acts like a typical Student reference variable