140 likes | 304 Views
Arrays and ArrayLists. numbers. int numbers[] = new int [10];. Other ways to declare the same array. 1) int [] numbers = new int [10];. 2) int [] numbers; numbers = new int [10];. 3) int [] numbers = {0,0,0,0,0,0,0,0,0,0};. Important points on arrays
E N D
numbers int numbers[] = new int[10]; Other ways to declare the same array 1) int[] numbers = new int[10]; 2) int[] numbers; numbers = new int[10]; 3) int[] numbers = {0,0,0,0,0,0,0,0,0,0}; • Important points on arrays • Great way to create a bunch of homogenous data. • Very fast to access all the data. • Size is static, you cannot change it once you declare the size. • Elements are initialized with default values.
Accessing Elements numbers 11 3 numbers[4]; 5 8 7 numbers[6] = 12; 1 13 numbers[0] = numbers[0] + numbers[2]; 14 12 23 numbers[10] = 9; 4 IndexOutOfBounds Exception 9
ArrayList<Integer> numbers = new ArrayList<Integer>(); numbers 1 0 0 2 1 1 1 0 2 0 14 14 4 9 23 23 9 9 57 9 numbers.add(4); numbers.add(9); numbers.add(14); numbers.get(0); • Important notes on ArrayLists • Dynamic size: they grow and shrink with data • Can add elements into middle of list • Can remove any element • Not as efficient as regular arrays • Still homogenous data structure but can beused with polymorphism to store ahierarchy of similar data. numbers.remove(0); numbers.set(1, 23); numbers.add(1, 57);
Important Algorithms – Summing a list of ints public int sum(intarr[]) { int total = 0; for(inti=0; i < arr.length; i++) { total += arr[i]; } return total; } With regular for loop public int sum(intarr[]) { int total = 0; for(int temp: arr) { total += temp; } return total; } With for-each loop
Important Algorithms – Summing a list of ints public int sum(ArrayList<Integer> arr) { int total = 0; for(inti=0; i < arr.size(); i++) { total += arr.get(i); } return total; } With regular for loop public int sum(ArrayList<Integer> arr) { int total = 0; for(int temp: arr) { total += temp; } return total; } With for-each loop
Important Algorithms – Summing a list of ints public intsumAge(ArrayList<Person> arr) { int total = 0; for(inti=0; i < arr.size(); i++) { total += arr.get(i).getAge(); } return total; } With regular for loop public int sum(ArrayList<Person> arr) { int total = 0; for(int temp: arr) { total += temp.getAge(); } return total; } With for-each loop
Important Algorithms – Linear Search public int find(intarr[], int key) { for(inti=0; i < arr.length; i++) { if(arr[i] == key) return i; //where it was } return -1; //not found } With regular for loop Not possible!!!! For-each loop is not able to tell the programmer where they are in the list at a given time With for-each loop
Important Algorithms – Linear Search public int find(ArrayList<Integer> arr, int key) { for(inti=0; i < arr.size(); i++) { if(arr.get(i) == key) return i; //where it was } return -1; //not found } With regular for loop Not possible!!!! For-each loop is not able to tell the programmer where they are in the list at a given time With for-each loop
Important Algorithms – Linear Search public int find(ArrayList<Person> arr, int key) { for(inti=0; i < arr.size(); i++) { if(arr.get(i).getSsn() == key) return i; //where it was } return -1; //not found } With regular for loop Not possible!!!! For-each loop is not able to tell the programmer where they are in the list at a given time With for-each loop
Linear Search find(numbers, 9); numbers 9 search, or in general, n searches where n is size of array Worst Case Scenario:
Binary Search find(numbers, 47); numbers
Binary Search How many search, max, to find an element in a list of size n? The smallest power of 2 that is larger than the length of the list.
Sorting http://math.hws.edu/TMCM/java/xSortLab/ • Bubble sort • Selection sort • Insertion Sort • Quick Sort • Merge Sort