Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Arrays and ArrayLists COMP T1 #5

Similar presentations


Presentation on theme: "Java Arrays and ArrayLists COMP T1 #5"— Presentation transcript:

1 Java Arrays and ArrayLists COMP 112 2017T1 #5

2 Array and ArrayList In Java an array Is an Object
allows indexed data retrieval. 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 Array and reference arr21 z arr21 y arr22
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 int[] y = {1,2,3,4}; y arr22 arr22 1 2 3 4

4 Array of fixed size constructor array size fixed at runtime

5 Arrays while(<cond>){<body>} return in loop
----- Meeting Notes (8/03/14 17:50) ----- ASK what == means easy syntax a[i] If (<cond>) {<body>}

6 Objects The API will list the methods for an Object Objects are:
Of Reference type myOb == anotherAL (same object) myOb = anotherAL (point to same object) Compared by myOb.equals(anotherOb) Compare an objects “value” with .equals(_) not == Beware Strings are objects but == some times works like .equals(). Hence == must not be relied upon! See example code StringEqu.java.

7 ArrayList An ArrayList is an Object
The elements in an ArrayList are also Objects How do you: Find out if two ArrayLists are the same? Copy one ArrayList to another? Two Arrays of Integers are the same if each index contains 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 Look up API from BlueJ Type ClassName. Type Ctrl space

9 ArrayList or Array ArrayList has indexed retrieval
run time change of size you can add and delete items while iterating! Array size fixed when built easy syntax a[i] Be very careful when deleting Google stackoverflow


Download ppt "Java Arrays and ArrayLists COMP T1 #5"

Similar presentations


Ads by Google