Download presentation
Presentation is loading. Please wait.
1
CS Week 9 Jim Williams, PhD
2
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
3
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)
4
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.
5
How do we insert 2 into the array?
int [] list = new int[]{1,4,5}; list[1] = 2; //?
6
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
7
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 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
8
Printing out, toString method
Integer [] list2 = {1,2,3}; System.out.println( list2[1] ); System.out.println( list2 ); //java.util.Arrays.toString( list2)
9
Object Class java.lang.Object has methods such as toString()
10
Arrays vs ArrayList Arrays fixed length, contiguous in memory
automatically grows as elements are added by allocating new array and copying.
11
Java Source Code Usually within Java Development Kit (JDK).
On Windows typically found under: C:\Program Files\Java Look for src.zip
12
ArrayList Example ArrayList<Integer> list;
list = new ArrayList<Integer>(); list.add( 2); list.add(0,3); System.out.println( list);
13
Lots of Elements ArrayList<Integer> list4;
list4 = new ArrayList<Integer>(); for ( int i = 0; i < 1000; i++) { list4.add( i); } System.out.println( list4);
14
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);
15
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
16
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)
17
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
18
Enhanced For Loop ArrayList<String> names = new ArrayList<>(); names.add("spot"); names.add("fido"); for ( String name : names) { System.out.println( name); }
19
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("]");
20
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. arr x */ public static int linearSearch( int arr[], int x) { for ( int i = 0; i < arr.length; i++) { if ( arr[i] == x) { return i; } return -1;
21
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;
22
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
23
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); }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.