Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 17.

Similar presentations


Presentation on theme: "CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 17."— Presentation transcript:

1 CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 17

2 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

3 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

4 Declaration Declaring an array – arrays are objects in Java – Create a reference variable to an array by using [] – instantiate an array object using new [size of array] EXAMPLE: int [] grades; grades = new int[10]; OR int[] grades = new int[10]; grades [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

5 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

6 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

7 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; int [] data = new int[20]; for (int i = 0; i < data.length; i++) data[i] = i + 2; Literal variable

8 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 (int i = 0; i < data.length; i++) data [i] = i * i; for (int i = 0; i < data.length; i++) System.out.println(“data[“ + i + ”] = “ + data[i]); }

9 BREAKOUT 1

10 On the Board: Searching or an Element

11 On the Board: Find Max Element

12 Arrays of Object References: Review Remember Student stu1; only declares a reference variable to a Student object. – no actual Student object yet. stu1 ?

13 Arrays of Object References Student [] class = new Student[10]; Creates and array of 10 Student references. There are no student objects yet. for (int i = 0; i < class.length; i++) class[i] = new Student(); Instantiates a new object for each element of the array to point to.

14 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


Download ppt "CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 17."

Similar presentations


Ads by Google