1 / 14

Arrays and ArrayLists

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

sukey
Télécharger la présentation

Arrays and ArrayLists

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arrays and ArrayLists

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

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

  4. 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);

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

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

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

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

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

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

  11. Linear Search find(numbers, 9); numbers 9 search, or in general, n searches where n is size of array Worst Case Scenario:

  12. Binary Search find(numbers, 47); numbers

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

  14. Sorting http://math.hws.edu/TMCM/java/xSortLab/ • Bubble sort • Selection sort • Insertion Sort • Quick Sort • Merge Sort

More Related