CS 200 - Week 9 Jim Williams, PhD
This Week BP1: Milestone 2 due this week Scanner examples labs 3, 4, 6, and 8 Team Lab: Multi-Dimensional Arrays Bring paper and pencil to draw diagrams. Lecture: Objects and ArrayList comparison with arrays
What happens when array grows automatically array index out of bounds error who knows? You try to add more elements than will fit into an array? B and technically C since B is an error. (try it)
What happens when int [] list = new int[]{1,4,5}; list[1] = 2; list array now contains 1,2,5 list array now contains 1,2,4,5 Error int [] list = new int[]{1,4,5}; list[1] = 2; A is the best answer.
How do we insert 2 into the array? int [] list = new int[]{1,4,5}; list[1] = 2; //?
Recall Wrapper Classes Primitive Data Type Wrapper class int Integer double Double char 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? true Integer m = 5; Integer n = 5; false boolean result = m == n; true false error A is correct. The fine print at the provided link says for Integer instances based on int literals between -128 and 127 will have the same reference for the same int literal value. The intention of this example is to emphasize experimenting, discovering short illustrative examples and an introduction to the Java Language Specification. Part of relevant details: If the value p being boxed is an integer literal of type int between -128 and 127 inclusive (§3.10.1), or the boolean literal true or false (§3.10.3), or a character literal between '\u0000' and '\u007f' inclusive (§3.10.4), then let a and b be the results of any two boxing conversions of p. It is always the case that a == b. Ideally, boxing a primitive value would always yield an identical reference. In practice, this may not be feasible using existing implementation techniques. The rule above is a pragmatic compromise, requiring that certain common values always be boxed into indistinguishable objects. Java Language Specification https://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html
Printing out, toString method Integer [] list2 = {1,2,3}; System.out.println( list2[1] ); System.out.println( list2 ); //java.util.Arrays.toString( list2)
Object Class java.lang.Object has methods such as toString()
Arrays vs ArrayList Arrays fixed length, contiguous in memory automatically grows as elements are added by allocating new array and copying.
Java Source Code Usually within Java Development Kit (JDK). On Windows typically found under: C:\Program Files\Java Look for src.zip
ArrayList Example ArrayList<Integer> list; list = new ArrayList<Integer>(); list.add( 2); list.add(0,3); System.out.println( list);
Lots of Elements ArrayList<Integer> list4; list4 = new ArrayList<Integer>(); for ( int i = 0; i < 1000; i++) { list4.add( i); } System.out.println( list4);
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 have to make sure there is enough room and move all the elements down and then insert it. Insertion is handled automatically You want to insert an element in an ArrayList at a specific index? B is the best answer
How many elements in an Array vs ArrayList? static void methodA(int [] arr) { //how to find capacity of array? } static void methodB(ArrayList list) { //how to find the number of //elements in list? arr.length list.size() list.length() arr.size correct: A (try it)
What size and elements? 5 [A,B,C,D,E] 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 } public static void printArray(int [] arr) { System.out.print("\n["); boolean first = true; for ( int num : arr) { if ( first) { first = false; } else { System.out.print(", "); } System.out.print( num); System.out.println("]");
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) { } /** * Returns the index of where the element x was found * or -1 if not found. * @param arr * @param x * @return */ public static int linearSearch( int arr[], int x) { for ( int i = 0; i < arr.length; i++) { if ( arr[i] == x) { return i; } return -1;
Binary Search // Return index of where x is in list if found, // otherwise returns -1; public static int binarySearch(ArrayList<Integer> list, int x) { } /* * Return index of where x is in array if found, otherwise * returns -1; */ public static int binarySearch(int arr[], int x) { int left = 0, right = arr.length - 1; while (left <= right) { //determine mid point int mid = left + (right-left)/2; //check mid point if (arr[mid] == x) return mid; //if x is greater than mid point then change left if (arr[mid] < x) left = mid + 1; //if x is less than mid point then change right else right = mid - 1; } //not found, return -1 return -1;
Draw Picture ArrayList<ArrayList<String>> list = new ArrayList<>(); 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
Sorting public static void bubbleSort( ArrayList<Integer> list) { } public static void bubbleSort(ArrayList<Integer> list) { int temp = 0; for (int i = 0; i < list.size(); i++) { for (int j = 1; j < (list.size() - i); j++) { if (list.get(j - 1) > list.get(j)) { // swap temp = list.get(j - 1); list.set(j - 1, list.get(j)); list.set(j, temp); }