Download presentation
Presentation is loading. Please wait.
Published byRoxanne Farmer Modified over 9 years ago
1
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Arrays and ArrayLists COMP 112 2014T1 #5
2
COMP 112 5: 2 © D. Streader Array and ArrayList In Java an array 1.Is an Object 2.allows indexed data retrieval. 3.Is of fixed size once built In Java an ArrayList is an indexed data structure that can change its size at runtime. Data in Arrays and ArrayLists is commonly processed by iterating over the size of the data structure. If the size of the array can be fixed when built use an array.
3
COMP 112 5: 3 © D. Streader Array and reference When building an array ( new int[4] ) the name ( z ) points to a fixed size of memory. int[] z = new int[4]; z arr21 arr21 00000000 int[] y = {1,2,3,4}; y arr22 arr22 12341234
4
COMP 112 5: 4 © D. Streader Array of fixed size array size fixed at runtime constructor
5
COMP 112 5: 5 © D. Streader Arrays If ( ) { } while( ){ } return in loop easy syntax a[i]
6
COMP 112 5: 6 © D. Streader Objects The API will list the methods for an Object Objects are: 1.Of Reference type 2. myOb == anotherAL (same reference) 3. myOb = anotherAL (make references equal) 4.Compared by myOb.Equal(anotherOb) Use.Equal() not ==
7
COMP 112 5: 7 © D. Streader ArrayList An ArrayList is an Object The elements in an ArrayList are also Objects How do you: 1.Find out if two ArrayLists are the same? 2.Copy one ArrayList to another? Two Arrays of Integers are the same if at each index they both contain Integers with the same value. If a student is represented by a Student Object (each with a unique ID) then two arrays of Students are the same if they contain the same references.
8
COMP 112 5: 8 © D. Streader Look up API from BlueJ Type Ctrl space Type ClassName.
9
COMP 112 5: 9 © D. Streader ArrayList vs List ArrayList 1.has indexed retrieval 2.run time change of size 3.you can add and delete items while iterating! Array 1.has indexed retrieval 2.size fixed when built 3.easy syntax a[i] Be very careful when deleting
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.