CS 200 Objects and ArrayList Jim Williams, PhD
This Week 1. Academic Integrity 2. BP1: Milestone 2 due this week 3. Team Lab: Multi-Dimensional Arrays a. Bring paper and pencil to draw diagrams. b. Code Standards 4. Lecture: Objects and ArrayList a. comparison with arrays
Object class java.lang.object Every class is a descendent of Object, either directly or indirectly. has methods such as tostring() that are inherited by every class.
class vs instance/object class: Object,String,Random,Scanner,Integer instance/object: new String("hello") new Integer(3) new Random() new Scanner( System.in)
Recall Wrapper Classes Primitive Data Type Wrapper class int double char Integer Double Character etc. Boxing: create an instance of wrapper class for primitive value. Unboxing: get primitive value from instance of wrapper class. Java Language Specification https://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html
Is result true or false? Integer m = 5; Integer n = 5; boolean result = m == n; true false error Java Language Specification https://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html
Printing out, tostring method Integer [] list = {1,2,3}; System.out.println( list[1] ); System.out.println( list ); //java.util.arrays.tostring( list)
How do we add 7 to the array? int [] list = new int[]{1,4,5}; list[3] = 7; //?
Arrays vs ArrayList Arrays fixed length, constant access time works with any type ArrayList fixed length, constant access time only works with Reference types automatically grows as elements are added by allocating a new array and copying.
ArrayList Diagram ArrayList<Integer> list; list = new ArrayList<Integer>(); list.add( 2); list.add(0,3); System.out.println( list);
Draw a Picture ArrayList<Integer> list; list = new ArrayList<Integer>(20); list.add(0,3); list.add(5); list.add(0,4); list.remove( 1); System.out.println( list);
How many elements? ArrayList<Integer> list4; list4 = new ArrayList<Integer>(100); for ( int i = 0; i < 1000; i++) { list4.add( i); } System.out.println( list4.size()); 1000 100 error (after 100 added)
Java Source Code Usually within Java Development Kit (JDK). On Windows typically found under: C:\Program Files\Java Look for src.zip
Capacity vs Size Array Capacity.length attribute Size keep separate count of elements inserted in array ArrayList changes as needed.size() method
How to retrieve the number of elements added to an Array vs ArrayList? static int numelements(int [] arr) { return A; } static int numelements(arraylist list) { return B; } A arr.length B list.size() A arr.length B list.length() A arr.size B list.length() A arr.size B list.size() Other
Adding an array to ArrayList ArrayList<Integer> list = new ArrayList<Integer>(); list.add(10); Integer [] arr = new Integer[]{1,3,4,5}; list.addall( java.util.arrays.aslist( arr)); System.out.println( list);
What happens when You want to insert an element in an ArrayList at a specific index? you have to make sure there is enough room and move all the elements down and then insert it. Insertion is handled automatically
What size and elements? ArrayList<String> list; list = new ArrayList<String>(); list.add("a"); list.add(0,"b"); list.add("c"); list.set(2,"d"); list.add("e"); System.out.println( list.size()); System.out.println( list); 5 [A,B,C,D,E] 4 [B, A, D, E] 3 [B,D,E] error or other
Enhanced For Loop ArrayList<String> names = new ArrayList<>(); names.add("spot"); names.add("fido"); for ( String name : names) { System.out.println( name); }
Write a method to print out array public static void printarray(int [] arr) { //use enhanced for loop }
Linear Search //Returns the index of where the element x was //found or -1 if not found. public static int linearsearch( ArrayList<Integer> list, int x) { }
Binary Search // Return index of where x is in list if found, // otherwise returns -1; public static int binarysearch(arraylist<integer> list, int x) { }
Draw Picture ArrayList<ArrayList<String>> list = new ArrayList<>(); list.add( new ArrayList<String>()); list.add( new ArrayList<String>()); list.add(1, new ArrayList<String>()); list.get(1).add("d"); list.set(0, list.get(1)); list.get(2).clear(); System.out.println( list); [[],[],[]] [[], [D], []] [[D],[D],[]] error or other