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

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

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

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

CSE 1341 Honors

Professor Mark FontenotSouthern Methodist University

Note Set 17

Page 2: 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

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

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

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

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];

grades[0][1][2][3][4][5][6][7][8][9]

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

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

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

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

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

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

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

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]);

}

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

BREAKOUT 1

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

On the Board: Searching or an Element

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

On the Board: Find Max Element

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

Arrays of Object References: Review

• Remember Student stu1;

only declares a reference variable to a Student object. – no actual Student object yet.

stu1?

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

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 arrayto point to.

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

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 atypical Student

reference variable