Post on 05-Jan-2016
description
transcript
Arrays and ArrayLists
int numbers[] = new int[10];
0 0
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
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.
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;
12numbers[0] = numbers[0] + numbers[2];
11
numbers[10] = 9;
IndexOutOfBounds Exception
ArrayList<Integer> numbers = new ArrayList<Integer>();
numbers.add(4);
numbers
0 4
numbers.add(9);
1 9
numbers.add(14);
2 14
numbers.get(0);
numbers.remove(0);
0 9
1 14
numbers.set(1, 23);
0 9
1 23
numbers.add(1, 57);
0 9
1 57
2 23
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 ahierarchy of similar data.
Important Algorithms – Summing a list of intspublic 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;}
WITH REGULAR FOR LOOP
With for-each loop
Important Algorithms – Summing a list of intspublic int sum(ArrayList<Integer> arr){ int total = 0;
for(int i=0; i < arr.size(); i++) { total += arr.get(i); }
return total;}
public int sum(ArrayList<Integer> arr){ int total = 0;
for(int temp: arr) { total += temp; }
return total;}
WITH REGULAR FOR LOOP
With for-each loop
Important Algorithms – Summing a list of intspublic int sumAge(ArrayList<Person> arr){ int total = 0;
for(int i=0; i < arr.size(); i++) { total += arr.get(i).getAge(); }
return total;}
public int sum(ArrayList<Person> arr){ int total = 0;
for(int temp: arr) { total += temp.getAge(); }
return total;}
WITH REGULAR FOR LOOP
With for-each loop
Important Algorithms – Linear Search
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
WITH REGULAR FOR LOOP
With for-each loop
Important Algorithms – Linear Search
public int find(ArrayList<Integer> 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
WIT
H R
EG
ULA
R F
OR
LO
OP
With for-each loop
Important Algorithms – Linear Search
public int find(ArrayList<Person> 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
WIT
H R
EG
ULA
R F
OR
LO
OP
With for-each loop
Linear Search
0 1 2 3 4 5 6 7 8 9
4 8 12 1 6 23 14 9 2 7numbers
find(numbers, 9);
Worst Case Scenario:9 search, or in general, n searches where n is size of array
Binary Search
0 1 2 3 4 5 6 7 8 9
4 8 12 13 14 23 26 34 47 57numbers
find(numbers, 47);
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.
List size Power of 2 Max Searches
10 24 = 16 4
100 27 = 128 7
1,000 210 = 1,024 10
10,000 214 = 16,384 14
100,000 217 = 131, 072 16
1,000,000 220 = 1,048,576 20
Sorting
• Bubble sort• Selection sort• Insertion Sort• Quick Sort• Merge Sort
http://math.hws.edu/TMCM/java/xSortLab/