arrays and arraylists

14
Arrays and ArrayLists

Upload: sukey

Post on 05-Jan-2016

56 views

Category:

Documents


0 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Arrays and  ArrayLists

Arrays and ArrayLists

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

Page 3: Arrays and  ArrayLists

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

Page 4: Arrays and  ArrayLists

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.

Page 5: Arrays and  ArrayLists

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

Page 6: Arrays and  ArrayLists

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

Page 7: Arrays and  ArrayLists

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

Page 8: Arrays and  ArrayLists

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

Page 9: Arrays and  ArrayLists

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

Page 10: Arrays and  ArrayLists

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

Page 11: Arrays and  ArrayLists

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

Page 12: Arrays and  ArrayLists

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

Page 13: Arrays and  ArrayLists

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

Page 14: Arrays and  ArrayLists

Sorting

• Bubble sort• Selection sort• Insertion Sort• Quick Sort• Merge Sort

http://math.hws.edu/TMCM/java/xSortLab/