CS 200 - Programming I: ArrayList Marc Renault Department of Computer Sciences University of Wisconsin Madison Spring 2018 TopHat Sec 3 (AM) Join Code: 427811 TopHat Sec 4 (PM) Join Code: 165455
ArrayLists
1/34 Dynamic Arrays arrl: 0 1 2 3 4 5 6 11 34 29 25 42 16 12 A Dynamic Array A data structure. Organization: A container that sequentially groups cells of the same type like an array. The array will grow automatically as items are added. Cells are accessed based on an index starting from 0.
1/34 Dynamic Arrays arrl: 0 1 2 3 4 5 6 7 11 34 29 25 42 16 12 43 A Dynamic Array A data structure. Organization: A container that sequentially groups cells of the same type like an array. The array will grow automatically as items are added. Cells are accessed based on an index starting from 0.
1/34 Dynamic Arrays arrl: 0 1 2 3 4 5 6 7 8 11 34 29 25 42 16 12 43 47 A Dynamic Array A data structure. Organization: A container that sequentially groups cells of the same type like an array. The array will grow automatically as items are added. Cells are accessed based on an index starting from 0.
1/34 Dynamic Arrays arrl: 0 1 2 3 4 5 6 7 8 9 11 34 29 25 42 16 12 43 47 15 A Dynamic Array A data structure. Organization: A container that sequentially groups cells of the same type like an array. The array will grow automatically as items are added. Cells are accessed based on an index starting from 0.
1/34 Dynamic Arrays 0 1 2 3 4 5 6 7 8 9 arrl: 11 34 29 25 42 16 12 43 47 15 A Dynamic Array A data structure. Organization: A container that sequentially groups cells of the same type like an array. The array will grow automatically as items are added. Cells are accessed based on an index starting from 0. Java s java.util.arraylist A dynamic array. Only works with reference types.
2/34 Objects Object Concept Basic Concept: Way to group together related variables and methods. Object-oriented programming: Programming paradigm based on the interaction of objects. E.g. String, Scanner, Integer, etc.
2/34 Objects Object Concept Basic Concept: Way to group together related variables and methods. Object-oriented programming: Programming paradigm based on the interaction of objects. E.g. String, Scanner, Integer, etc. java.lang.object class Every object in Java is derived from the Object class. That is: a String object is an Object. a Scanner object is an Object an Integer object is an Object Primitives are not java.lang.objects
3/34 Creating ArrayList Don t forget: import java.util.arraylist;
3/34 Creating ArrayList Don t forget: import java.util.arraylist; Declaration ArrayList<RefType> arrlist; A reference type. RefType : Notice that contents of the ArrayList is specified between < and >. E.g. ArrayList<Integer> arrlist;
3/34 Creating ArrayList Don t forget: import java.util.arraylist; Declaration ArrayList<RefType> arrlist; A reference type. RefType : Notice that contents of the ArrayList is specified between < and >. E.g. ArrayList<Integer> arrlist; Allocation Constructor with new operator: arrlist = new ArrayList<RefType>();
3/34 Creating ArrayList import java.util.arraylist; Declaration ArrayList<RefType> arrlist; Allocation Constructor with new operator: arrlist = new ArrayList<RefType>(); Some examples: ArrayList < Integer > arrl1 = new ArrayList < Integer >(); ArrayList < String > arrl2 = new ArrayList < String >();
4/34 TopHat Question 1 Will the following compile? import java. util. ArrayList ; public class TopHat1 { } public static void main ( String [] args ) { ArrayList < char > arrl = new ArrayList < char >(); }
5/34 TopHat Question 2 Which statement is true? ArrayList < Double > arrl ; Creates a reference type variable for an ArrayList<Double> called arrl on the stack, and allocates memory for the ArrayList<Double> object on the heap. Creates a reference type variable for an ArrayList<Double> called arrl on the stack.
6/34 Appending an ArrayList Adding an Element to the End Member method: add(objtoadd) objtoadd element to add to the end of the ArrayList.
6/34 Appending an ArrayList arrl: ArrayList < Integer > arrl = new ArrayList < Integer >(); arrl. add ( new Integer (11)); arrl. add ( new Integer (34)); arrl. add (29); Adding an Element to the End Member method: add(objtoadd) objtoadd element to add to the end of the ArrayList.
6/34 Appending an ArrayList arrl: ArrayList < Integer > arrl = new ArrayList < Integer >(); arrl. add ( new Integer (11)); arrl. add ( new Integer (34)); arrl. add (29); Adding an Element to the End Member method: add(objtoadd) objtoadd element to add to the end of the ArrayList.
6/34 Appending an ArrayList arrl: 11 ArrayList < Integer > arrl = new ArrayList < Integer >(); arrl. add ( new Integer (11)); arrl. add ( new Integer (34)); arrl. add (29); Adding an Element to the End Member method: add(objtoadd) objtoadd element to add to the end of the ArrayList.
6/34 Appending an ArrayList arrl: 11 ArrayList < Integer > arrl = new ArrayList < Integer >(); arrl. add ( new Integer (11)); arrl. add ( new Integer (34)); arrl. add (29); Adding an Element to the End Member method: add(objtoadd) objtoadd element to add to the end of the ArrayList.
6/34 Appending an ArrayList arrl: 11 34 ArrayList < Integer > arrl = new ArrayList < Integer >(); arrl. add ( new Integer (11)); arrl. add ( new Integer (34)); arrl. add (29); Adding an Element to the End Member method: add(objtoadd) objtoadd element to add to the end of the ArrayList.
6/34 Appending an ArrayList arrl: 11 34 ArrayList < Integer > arrl = new ArrayList < Integer >(); arrl. add ( new Integer (11)); arrl. add ( new Integer (34)); arrl. add (29); Adding an Element to the End Member method: add(objtoadd) objtoadd element to add to the end of the ArrayList.
6/34 Appending an ArrayList arrl: 11 34 29 ArrayList < Integer > arrl = new ArrayList < Integer >(); arrl. add ( new Integer (11)); arrl. add ( new Integer (34)); arrl. add (29); Adding an Element to the End Member method: add(objtoadd) objtoadd element to add to the end of the ArrayList.
7/34 Automatic Wrapping/Unwrapping int Integer short Short long Long double Double float Float char Character byte Byte boolean Boolean Autoboxing In Java, the automatic conversion between a primitive and its object wrapper class (and vice-versa) is called autoboxing. Boxing Automatically converting a primitive to its object wrapper class. Unboxing Automatically converting an object wrapper class to its primitive value.
8/34 Inside an ArrayList ArrayList < Integer > arrl = new ArrayList < Integer >(); arrl. add ( new Integer (11)); arrl. add ( new Integer (34)); arrl. add (29);
8/34 Inside an ArrayList ArrayList < Integer > arrl = new ArrayList < Integer >(); arrl. add ( new Integer (11)); arrl. add ( new Integer (34)); arrl. add (29); size: 0 arrl elementdata:
8/34 Inside an ArrayList ArrayList < Integer > arrl = new ArrayList < Integer >(); arrl. add ( new Integer (11)); arrl. add ( new Integer (34)); arrl. add (29); size: 0 arrl elementdata:
8/34 Inside an ArrayList ArrayList < Integer > arrl = new ArrayList < Integer >(); arrl. add ( new Integer (11)); arrl. add ( new Integer (34)); arrl. add (29); size: 1 arrl elementdata: 11
8/34 Inside an ArrayList ArrayList < Integer > arrl = new ArrayList < Integer >(); arrl. add ( new Integer (11)); arrl. add ( new Integer (34)); arrl. add (29); size: 1 arrl elementdata: 11
8/34 Inside an ArrayList ArrayList < Integer > arrl = new ArrayList < Integer >(); arrl. add ( new Integer (11)); arrl. add ( new Integer (34)); arrl. add (29); size: 2 arrl elementdata: 11 34
8/34 Inside an ArrayList ArrayList < Integer > arrl = new ArrayList < Integer >(); arrl. add ( new Integer (11)); arrl. add ( new Integer (34)); arrl. add (29); size: 2 arrl elementdata: 11 34
8/34 Inside an ArrayList ArrayList < Integer > arrl = new ArrayList < Integer >(); arrl. add ( new Integer (11)); arrl. add ( new Integer (34)); arrl. add (29); size: 3 arrl elementdata: 11 34 29
Life of an ArrayList arrl. add (12); arrl. add (42); arrl. add (16); arrl. add (47); arrl. add (43); arrl. add (28); arrl. add (32); arrl. add (20); arrl size: 3 elementdata: 11 34 29 9/34
Life of an ArrayList arrl. add (12); arrl. add (42); arrl. add (16); arrl. add (47); arrl. add (43); arrl. add (28); arrl. add (32); arrl. add (20); arrl size: 10 elementdata: 11 34 29 12 42 16 47 43 28 32 9/34
Life of an ArrayList arrl. add (12); arrl. add (42); arrl. add (16); arrl. add (47); arrl. add (43); arrl. add (28); arrl. add (32); arrl. add (20); arrl size: 10 elementdata: 11 34 29 12 42 16 47 43 28 32 9/34
9/34 Life of an ArrayList arrl. add (20); arrl size: 10 elementdata: 11 34 29 12 42 16 47 43 28 32
9/34 Life of an ArrayList arrl. add (20); arrl size: 10 elementdata: 11 34 29 12 42 16 47 43 28 32
9/34 Life of an ArrayList arrl. add (20); arrl size: 10 elementdata: 11 34 29 12 42 16 47 43 28 32
9/34 Life of an ArrayList arrl. add (20); arrl size: 11 elementdata: 11 34 29 12 42 16 47 43 28 32 20
10/34 Capacity Constructor ArrayList<RefType> arrlist = new ArrayList<RefType>(capacity); Allocation with Capacity capacity specifies the initial capacity of the list.
10/34 Capacity Constructor ArrayList<RefType> arrlist = new ArrayList<RefType>(capacity); Allocation with Capacity capacity specifies the initial capacity of the list. TopHat Question 3 With the goal of optimizing the running time of your program, how would you declare an ArrayList, called vals, to store a sequence of measurements (real values) given that there would always be at least 100 measurements?
11/34 Changing an Element in an ArrayList oldobj set(idx, newobj) Params: idx index to replace, and newobj new object. Returns the reference to the replaced object.
11/34 Changing an Element in an ArrayList arrl. set (10, 24); arrl size: 11 elementdata: 11 34 29 12 42 16 47 43 28 32 20
11/34 Changing an Element in an ArrayList arrl. set (10, 24); arrl size: 11 elementdata: 24 11 34 29 12 42 16 47 43 28 32 20
12/34 Removing from an ArrayList remove Methods oldobj remove(idx): Removes and returns the reference at index idx. boolean remove(reftoobj): Removes first occurance of reftoobj if found, returning true on success.
12/34 Removing from an ArrayList arrl. remove (4); arrl. remove (( Integer ) 47); // Use with caution arrl size: 11 elementdata: 11 34 29 12 42 16 47 43 28 32 24
12/34 Removing from an ArrayList arrl. remove (4); arrl. remove (( Integer ) 47); // Use with caution arrl size: 10 elementdata: 11 34 29 12 42 16 47 43 28 32 24
12/34 Removing from an ArrayList arrl. remove (4); arrl. remove (( Integer ) 47); // Use with caution arrl size: 10 elementdata: 11 34 29 12 16 47 43 28 32 24
12/34 Removing from an ArrayList arrl. remove (4); arrl. remove (( Integer ) 47); // Use with caution arrl size: 9 elementdata: 11 34 29 12 16 47 43 28 32 24
13/34 Inserting into an ArrayList Another add Method add(idx, objtoadd) idx insert at index idx. objtoadd object to insert.
13/34 Inserting into an ArrayList arrl. add (5, 15); arrl size: 9 elementdata: 11 34 29 12 16 43 28 32 24
13/34 Inserting into an ArrayList arrl. add (5, 15); arrl size: 10 elementdata: 11 34 29 12 16 15 43 28 32 24
14/34 TopHat Question 4 Which operation is the slowest? Let arrl be an ArrayList with a size of 8 and default capacity. arrl.add(0, 10); arrl.add(10); No difference.
15/34 TopHat Question 5 Which operation is the slowest? Let arrl1 be an ArrayList with a size of 8 and default capacity, and arrl2 be an ArrayList with a size of 10 and default capacity. arrl1.add(10); arrl2.add(10); No difference.
16/34 Other ArrayList Methods refobj get(idx) Returns the reference found at index idx. int size() Returns the number of item in the ArrayList. clear() Removes all the items from the ArrayList. boolean isempty() Returns true if ArrayList is empty.
17/34 Array vs ArrayList Array ArrayList Fixed size Constant access time Works with any type Grows dynamically Constant access time Only works with ref types
17/34 Array vs ArrayList Array ArrayList Fixed size Constant access time Works with any type Grows dynamically Constant access time Only works with ref types int [] arr = {1, 2, 3, 4}; ArrayList < Integer > arrl = arr [0] = 5; new ArrayList < Integer >(); int x = arr [3]; arrl. add (1); arrl. add (2); arrl. add (3); arrl. add (4); arrl. set (0, 5); int x = arrl. get (3);
17/34 Array vs ArrayList Array ArrayList Fixed size Constant access time Works with any type Grows dynamically Constant access time Only works with ref types int [] arr = {1, 2, 3, 4}; ArrayList < Integer > arrl = arr [0] = 5; new ArrayList < Integer >(); int x = arr [3]; arrl. add (1); arrl. add (2); arrl. add (3); arrl. add (4); arrl. set (0, 5); int x = arrl. get (3); arrl. add (5);
18/34 TopHat Question 6 Array or ArrayList? For a classic 3 x 3 tic tac toe game, would you use an array (char[]) or an ArrayList (ArrayList<Character>) to track the moves on the game board?
19/34 TopHat Question 7 Array or ArrayList? Part of a program involves reading in lines from files of any length into an array-type data structure. Would an array (String[]) or an ArrayList (ArrayList<String>) be more suited?
20/34 TopHat Question 8 What is the output? ArrayList < String > arrl = new ArrayList < String >(); arrl. add ("Do"); arrl. add (" you "); arrl. add (" like "); arrl. add (" green "); arrl. add (" eggs "); arrl. add (" and "); arrl. add (" ham?"); System. out. print ( arrl. get (3). length ());
21/34 Using Objects in ArrayLists ArrayList < String > arrl = new ArrayList < String >(); arrl. add ("Do"); arrl. add (" you "); arrl. add (" like "); arrl. add (" green "); arrl. add (" eggs "); arrl. add (" and "); arrl. add (" ham?"); Both of the following are equivalent: System. out. print ( arrl. get (3). length ()); String tmpstr = arrl. get (3); System. out. print ( tmpstr. length ());
22/34 TopHat Question 9 What is the output? ArrayList < ArrayList < Integer > > arrl2d = new ArrayList < ArrayList < Integer > >(); for ( int i = 0; i < 3; i ++) { ArrayList < Integer > arrloopy = new ArrayList < Integer >(); for ( int j = i*3 + 1; j <= (i +1)*3; j ++) { arrloopy. add (j); } arrl2d. add ( arrloopy ); } System. out. print ( arrl2d. get (1). get (1));
23/34 ArrayLists of ArrayLists Analogue of multidimensional arrays. Stack Heap arrl2d
23/34 ArrayLists of ArrayLists Analogue of multidimensional arrays. Stack Heap arrl2d
23/34 ArrayLists of ArrayLists Analogue of multidimensional arrays. Stack Heap 4 5 6 1 2 3 7 8 9 arrl2d
23/34 ArrayLists of ArrayLists Analogue of multidimensional arrays. Stack Heap 4 5 6 1 2 3 7 8 9 arrl2d 1 2 3 4 5 6 7 8 9
Iterating ArrayLists
24/34 TopHat Question 10 What is the output? a. [1, 2, 3, 4] ArrayList < Integer > arrl = new ArrayList < Integer >(); arrl. add (1); arrl. add (2); arrl. add (3); arrl. add (4); System. out. print ( arrl ); b. I d like it to print out the array, but I ve see this trick before and it prints out something like java.util.arraylist@e93c3
25/34 TopHat Question 11 Which loop would generally be the most suited for iterating through an ArrayList? a. for b. while c. do while
26/34 Standard Loops import java. util. ArrayList ; public class ArrayListStringSum { public static void main ( String [] args ) { ArrayList < String > arrl = new ArrayList < String >(); arrl. add ("Do"); arrl. add (" you "); arrl. add (" like "); arrl. add (" green "); arrl. add (" eggs "); arrl. add (" and "); arrl. add (" ham?"); printline ( arrl ); } } public static void printline ( ArrayList < String > line ) { for ( int i = 0; i < line. size (); i ++){ System. out. print ( line. get (i )); if(i < line. size () - 1) System. out. print (" "); } }
27/34 Enhanced for Loop Java s for each Loop for ( type var : collection ) { loopstatement1 ; loopstatement2 ;... loopstatementn ; }
28/34 Enhanced for Loop Example import java. util. ArrayList ; public class ArrayListStringSum { } public static void main ( String [] args ) { ArrayList < String > arrl = new ArrayList < String >(); arrl. add ("Do"); arrl. add (" you "); arrl. add (" like "); arrl. add (" green "); arrl. add (" eggs "); arrl. add (" and "); arrl. add (" ham?"); printlineredux ( arrl ); } public static void printlineredux ( ArrayList < String > line ) { for ( String word : line ){ System. out. print ( word + " "); } }
29/34 TopHat Question 12 What is the output? import java. util. ArrayList ; public class ArrayListStringSum { } public static void main ( String [] args ) { ArrayList < String > arrl = new ArrayList < String >(); arrl. add ("Do"); arrl. add (" you "); arrl. add (" like "); arrl. add (" green "); arrl. add (" eggs "); arrl. add (" and "); arrl. add (" ham?"); System. out. print (f( arrl )); } public static int f( ArrayList < String > line ) { int retval = 0; for ( String word : line ) { retval += word. length (); } return retval ; }
30/34 for vs enhanced for for ( int i = 0; i < line. size (); i ++){ System. out. print ( line. get (i )); if(i < line. size () - 1) System. out. print (" "); } for ( String word : line ){ System. out. print ( word + " "); }
30/34 for vs enhanced for for ( int i = 0; i < line. size (); i ++){ System. out. print ( line. get (i )); if(i < line. size () - 1) System. out. print (" "); } for ( String word : line ){ System. out. print ( word + " "); } Enhanced for loops are more syntactic sugar.
30/34 for vs enhanced for for ( int i = 0; i < line. size (); i ++){ System. out. print ( line. get (i )); if(i < line. size () - 1) System. out. print (" "); } for ( String word : line ){ System. out. print ( word + " "); } Enhanced for loops are more syntactic sugar. are quick, clean code to iterate through all the items.
30/34 for vs enhanced for for ( int i = 0; i < line. size (); i ++){ System. out. print ( line. get (i )); if(i < line. size () - 1) System. out. print (" "); } for ( String word : line ){ System. out. print ( word + " "); } Enhanced for loops are more syntactic sugar. are quick, clean code to iterate through all the items. prevent out of bounds errors.
30/34 for vs enhanced for for ( int i = 0; i < line. size (); i ++){ System. out. print ( line. get (i )); if(i < line. size () - 1) System. out. print (" "); } for ( String word : line ){ System. out. print ( word + " "); } Enhanced for loops are more syntactic sugar. are quick, clean code to iterate through all the items. prevent out of bounds errors. inflexible; lose the position information.
Other Useful Stuff
31/34 Enhanced for and Arrays public static int sumarray ( int [] arr ) { int sum = 0; for ( int val : arr ) { sum += val ; } return sum ; }
32/34 java.util.arrays java.util.arrays Class A collection of static methods for manipulating arrays (copying, sorting, searching, printing,... ).
32/34 java.util.arrays java.util.arrays Class A collection of static methods for manipulating arrays (copying, sorting, searching, printing,... ). ArrayList from an Array ArrayList < Integer > arrl = new ArrayList < Integer >( Arrays. aslist (1,2,3,4)); Integer [] a = {5,6,7,8}; arrl. addall ( Arrays. aslist (a ));
32/34 java.util.arrays java.util.arrays Class A collection of static methods for manipulating arrays (copying, sorting, searching, printing,... ). ArrayList from an Array ArrayList < Integer > arrl = new ArrayList < Integer >( Arrays. aslist (1,2,3,4)); Integer [] a = {5,6,7,8}; arrl. addall ( Arrays. aslist (a )); More ArrayList methods: addall(collection) Appends items addall(idx, collection) Inserts items at idx
33/34 TopHat Question 13 What is the output? ArrayList < ArrayList < Integer > > arrl2d = new ArrayList < ArrayList < Integer > >(); for ( int i = 0; i < 3; i ++) { ArrayList < Integer > arrloopy = new ArrayList < Integer >(); for ( int j = i*3 + 1; j <= (i +1)*3; j ++) { arrloopy. add (j); } arrl2d. add ( arrloopy ); } arrl2d. set (1, arrl2d. get (0)); System. out. print ( arrl2d. get (1). get (1));
34/34 Further Reading COMP SCI 200: Programming I zybooks.com, 2015. zybook code: WISCCOMPSCI200Spring2018 Chapter 9. Objects and ArrayList
Appendix References Appendix
Appendix References References
Appendix References 35/34 Image Sources I https://brand.wisc.edu/web/logos/ http://www.zybooks.com/