Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays and ArrayLists. int numbers[] = new int[10]; 00 10 20 30 40 50 60 70 80 90 numbers Other ways to declare the same array 1) int[] numbers = new.

Similar presentations


Presentation on theme: "Arrays and ArrayLists. int numbers[] = new int[10]; 00 10 20 30 40 50 60 70 80 90 numbers Other ways to declare the same array 1) int[] numbers = new."— Presentation transcript:

1 Arrays and ArrayLists

2 int numbers[] = new int[10]; 00 10 20 30 40 50 60 70 80 90 numbers 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 0 1 2 3 4 5 6 7 8 9 numbersAccessing Elements numbers[4]; 3 5 8 7 1 13 14 23 4 9 numbers[6] = 12; 12 numbers[0] = numbers[0] + numbers[2]; 11 numbers[10] = 9; IndexOutOfBounds Exception

4 ArrayList numbers = new ArrayList (); numbers.add(4); numbers 04 numbers.add(9); 19 numbers.add(14); 214 numbers.get(0); numbers.remove(0); 09114 numbers.set(1, 23); 09123 numbers.add(1, 57); 09157223 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 be used with polymorphism to store a hierarchy of similar data.

5 public int sum(int arr[]) { int total = 0; for(int i=0; i < arr.length; i++) { total += arr[i]; } return total; } public int sum(int arr[]) { int total = 0; for(int temp: arr) { total += temp; } return total; }

6 public int sum(ArrayList arr) { int total = 0; for(int i=0; i < arr.size(); i++) { total += arr.get(i); } return total; } public int sum(ArrayList arr) { int total = 0; for(int temp: arr) { total += temp; } return total; }

7 public int sumAge(ArrayList arr) { int total = 0; for(int i=0; i < arr.size(); i++) { total += arr.get(i).getAge(); } return total; } public int sum(ArrayList arr) { int total = 0; for(int temp: arr) { total += temp.getAge(); } return total; }

8 public int find(int arr[], int key) { for(int i=0; i < arr.length; i++) { if(arr[i] == key) return i; //where it was } return -1; //not found } Not possible!!!! For-each loop is not able to tell the programmer where they are in the list at a given time

9 public int find(ArrayList arr, int key) { for(int i=0; i < arr.size(); i++) { if(arr.get(i) == key) return i; //where it was } return -1; //not found } Not possible!!!! For-each loop is not able to tell the programmer where they are in the list at a given time

10 public int find(ArrayList arr, int key) { for(int i=0; i < arr.size(); i++) { if(arr.get(i).getSsn() == key) return i; //where it was } return -1; //not found } Not possible!!!! For-each loop is not able to tell the programmer where they are in the list at a given time

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

12 0123456789 481213142326344757 numbers find(numbers, 47);

13 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. List sizePower of 2Max Searches 102 4 = 164 1002 7 = 1287 1,0002 10 = 1,02410 10,0002 14 = 16,38414 100,0002 17 = 131, 07216 1,000,0002 20 = 1,048,57620

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


Download ppt "Arrays and ArrayLists. int numbers[] = new int[10]; 00 10 20 30 40 50 60 70 80 90 numbers Other ways to declare the same array 1) int[] numbers = new."

Similar presentations


Ads by Google