Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 200 Objects and ArrayList

Similar presentations


Presentation on theme: "CS 200 Objects and ArrayList"— Presentation transcript:

1 CS 200 Objects and ArrayList
Jim Williams, PhD

2 This Week Academic Integrity BP1: Milestone 2 due this week
Team Lab: Multi-Dimensional Arrays Bring paper and pencil to draw diagrams. Code Standards Lecture: Objects and ArrayList comparison with arrays

3 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.

4 class vs instance/object
class: Object,String,Random,Scanner,Integer instance/object: new String("hello") new Integer(3) new Random() new Scanner( System.in)

5 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

6 Is result true or false? true Integer m = 5; Integer n = 5;
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

7 Printing out, toString method
Integer [] list = {1,2,3}; System.out.println( list[1] ); System.out.println( list ); //java.util.Arrays.toString( list)

8 How do we add 7 to the array?
int [] list = new int[]{1,4,5}; list[3] = 7; //? public static int [] addElementAtEnd(int [] list, int elem) { int newLength = list.length + 1; int [] newList = new int[newLength]; for ( int i = 0; i < list.length; i++) { newList[i] = list[i]; } newList[newList.length -1] = elem; return newList;

9 Arrays vs ArrayList Arrays fixed length, constant access time
works with any type ArrayList only works with Reference types automatically grows as elements are added by allocating a new array and copying.

10 ArrayList Diagram ArrayList<Integer> list;
list = new ArrayList<Integer>(); list.add( 2); list.add(0,3); System.out.println( list);

11 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);

12 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)

13 Java Source Code Usually within Java Development Kit (JDK).
On Windows typically found under: C:\Program Files\Java Look for src.zip

14 Capacity vs Size Array ArrayList Capacity .length attribute
changes as needed Size keep separate count of elements inserted in array .size() method

15 How to retrieve the number of elements added to an Array vs ArrayList?
A arr.length B list.size() B list.length() A arr.size Other static int numElements(int [] arr) { return A; } static int numElements(ArrayList list) { return B; correct: A (try it)

16 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);

17 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

18 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

19 Enhanced For Loop ArrayList<String> names = new ArrayList<>(); names.add("spot"); names.add("fido"); for ( String name : names) { System.out.println( name); }

20 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("]");

21 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;

22 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;

23 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


Download ppt "CS 200 Objects and ArrayList"

Similar presentations


Ads by Google