Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays and Collections

Similar presentations


Presentation on theme: "Arrays and Collections"— Presentation transcript:

1 Arrays and Collections
Intro to the Java Collections Framework CS-2851 Dr. Mark L. Hornick

2 CS-1020 12/4/2018 Java Array Review Declaration of an array: <datatype>[] <variable>; // approach 1 <datatype> <variable>[]; // approach 2 Creation of the actual array: <variable> = new <datatype>[<size>]; int[] scores; scores = new int[12]; Variation 1 int scores[]; scores = new int[12]; Variation 2 As you can declare and create objects in one statement such as Person p = new Person( ); you can declare and create an array in one statement as double[ ] rainfall = new double[12]; Strictly speaking, an array is a reference data type and really an object because there is no class called Array in Java. The thumbnail note in page 413 is therefore not 100 percent accurate. Arrays are created like objects! Portions adapted with permission from the textbook author. CS-2851 Dr. Mark L. Hornick Dr. Mark L. Hornick

3 The distinction between declaration and creation
CS-1020 12/4/2018 The distinction between declaration and creation int[] scores; // or int scores[] After Declaration of an array, a variable that references an array is created somewhere in memory But since there’s no actual array (yet), the variable has a value of null. Start of Memory As you can declare and create objects in one statement such as Person p = new Person( ); you can declare and create an array in one statement as double[ ] rainfall = new double[12]; Strictly speaking, an array is a reference data type and really an object because there is no class called Array in Java. The thumbnail note in page 413 is therefore not 100 percent accurate. scores null End of Memory Dr. Mark L. Hornick

4 The distinction between declaration and creation
CS-1020 12/4/2018 The distinction between declaration and creation int[] scores; // or int scores[] scores = new int[12]; After Creation of the array, the memory for the array elements is allocated and the variable is assigned to reference the first element of the array All elements of the array are initialized (in this case) to 0. scores addr As you can declare and create objects in one statement such as Person p = new Person( ); you can declare and create an array in one statement as double[ ] rainfall = new double[12]; Strictly speaking, an array is a reference data type and really an object because there is no class called Array in Java. The thumbnail note in page 413 is therefore not 100 percent accurate. Portions adapted with permission from the textbook author. CS-2851 Dr. Mark L. Hornick Dr. Mark L. Hornick

5 After assigning values to the array elements:
CS-1020 12/4/2018 After assigning values to the array elements: int[] scores; // or int scores[] scores = new int[12]; for( int i=0; i<scores.length; i++ ) scores[i] = i; scores As you can declare and create objects in one statement such as Person p = new Person( ); you can declare and create an array in one statement as double[ ] rainfall = new double[12]; Strictly speaking, an array is a reference data type and really an object because there is no class called Array in Java. The thumbnail note in page 413 is therefore not 100 percent accurate. The index of the first position in an array is 0. CS-2851 Dr. Mark L. Hornick Dr. Mark L. Hornick

6 CS-1020 12/4/2018 Another approach: Simultaneous declaration creation, and initialization of an array int[] scores ={8,7,10,4,5,6,5,8,9,10,5,8}; scores 1 2 3 4 5 6 7 8 9 10 11 scores[2] This indexed expression refers to the element at position #2 8 7 10 4 5 6 5 8 9 10 5 8 Portions adapted with permission from the textbook author. CS-2851 Dr. Mark L. Hornick Dr. Mark L. Hornick

7 Declaring an array of objects
CS-1020 12/4/2018 Declaring an array of objects Person[] people; people = new Person[3]; The people array is actually an array of references to Person objects After Creation of the array, the memory for the array elements is allocated and the variable is assigned to reference the first element of the array All elements of the array are initialized (in this case) to null. people addr null null null As you can declare and create objects in one statement such as Person p = new Person( ); you can declare and create an array in one statement as double[ ] rainfall = new double[12]; Strictly speaking, an array is a reference data type and really an object because there is no class called Array in Java. The thumbnail note in page 413 is therefore not 100 percent accurate. Portions adapted with permission from the textbook author. CS-2851 Dr. Mark L. Hornick Dr. Mark L. Hornick

8 Creating an array of objects
CS-1020 12/4/2018 Creating an array of objects Person[] people; people = new Person[3]; people[0] = new Person(A); When a Person object is created, the object is placed somewhere in memory. The new operator returns this address in memory. After initializing an array element to reference an instance of a Person object, the array element is assigned to reference to the Person object people addr addr null null Person As you can declare and create objects in one statement such as Person p = new Person( ); you can declare and create an array in one statement as double[ ] rainfall = new double[12]; Strictly speaking, an array is a reference data type and really an object because there is no class called Array in Java. The thumbnail note in page 413 is therefore not 100 percent accurate. Portions adapted with permission from the textbook author. CS-2851 Dr. Mark L. Hornick Dr. Mark L. Hornick

9 Another way of looking at it
CS-1020 12/4/2018 Another way of looking at it Code Person[ ] people; people = new Person[20]; people[0] = new Person(A); One Person object is created and the reference to this object is placed in position 0. C 1 2 3 4 16 17 18 19 person people 1 2 3 4 16 17 18 19 After is executed C Person A State of Memory Portions adapted with permission from the textbook author. CS-2851 Dr. Mark L. Hornick Dr. Mark L. Hornick

10 CS-1020 12/4/2018 Simultaneous declaration creation, and initialization of an object array Person[] people ={new Person(A), new Person(B), new Person(C)}; people 1 2 Person A Person B Person C Portions adapted with permission from the textbook author. CS-2851 Dr. Mark L. Hornick Dr. Mark L. Hornick

11 Element Deletion A Before is executed After is executed 1 2 3 people 1
CS-1020 12/4/2018 Element Deletion int index= 1; people[index] = null; Delete Person B by setting the reference in position 1 to null. What problem does this introduce? A 1 2 3 people A B C D 1 2 3 people A C D Before is executed After is executed In this approach, we simply leave the position of a deleted object to a null. With this approach, an array index positions will be a mixture of null and real pointers. Portions adapted with permission from the textbook author. CS-2851 Dr. Mark L. Hornick Dr. Mark L. Hornick

12 Element Insertion (?) A Before is executed A 1 2 3 people A B C D
CS-1020 12/4/2018 Element Insertion (?) int index= 1; people[index] = new Person(E); What happens?? A 1 2 3 people A B C D In this approach, we simply leave the position of a deleted object to a null. With this approach, an array index positions will be a mixture of null and real pointers. Before is executed A Portions adapted with permission from the textbook author. CS-2851 Dr. Mark L. Hornick Dr. Mark L. Hornick

13 Element Insertion problems
CS-1020 12/4/2018 Element Insertion problems int index= 1; people[index] = new Person(E); The old element is simply replaced. What happened to B??? A 1 2 3 people A B C D people 1 2 3 A E C D In this approach, we simply leave the position of a deleted object to a null. With this approach, an array index positions will be a mixture of null and real pointers. Before is executed A After is executed A Portions adapted with permission from the textbook author. CS-2851 Dr. Mark L. Hornick Dr. Mark L. Hornick


Download ppt "Arrays and Collections"

Similar presentations


Ads by Google