arrays. collections we would like to be able to keep lots of information at once example: keep all...

17
Arrays

Upload: daniella-knight

Post on 19-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing

Arrays

Page 2: Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing

Collections

• We would like to be able to keep lots of information at once

• Example: Keep all the students in the class

• Grade each one without writing each one out

• Collections are data types that hold groups of data

Page 3: Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing

Arrays

• An array is a data type that keeps data in a line.

• All the elements in the array have to be the same type.

• Arrays aren’t primitives, but their syntax isn’t really like objects

4 7 3 12 -4 87 0 45

Page 4: Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing

Array Indices

• Elements in an array are numbered for convenience

• Numbering starts at 0, like Strings

4 7 3 12 -4 87 0 45

0 1 2 3 4 5 6 7

Page 5: Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing

Syntax for Arrays• Declare an array: type of data in the array,

followed by square brackets.int[ ] numbers;Car[ ] myCars;

• Create an array: use word new and the number of elements in the array

numbers = new int[7];myCars = new Car[5];

• Can combine the two statements:Car [ ] myCars = new Car[5];

• Notice there are no parentheses!!!

Page 6: Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing

Array Elements

• Use square brackets to access individual array elements

• int x = myNums[4] means put the number in index 4 into variable x.

• Shortcut for filling up an array:

• double[ ] myList = {1.9, 3.4, -3.2, 0.0};

• Using this way you don’t need the word new.

Page 7: Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing

Array Elements

• Also put things in an array with element notation:

• myNumbers[4] = 5;

• Access the length of any array with .length

• int len = myNumbers.length

• No parentheses!!!

Page 8: Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing

Examples

• Make an array of 100 integers, where each number in the array is equal to twice its index value

• Sum up all numbers in an array

• Write a method that reverses an array.

• Find the middle number in an array of ints

• Find the biggest number in an array of ints

Page 9: Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing

Objects in Arrays

• Arrays are capable of holding primitive as well as object types.

• Creating an array of objects does not create the objects in the array.

• Sometimes the length of the array is not the same as the number of elements in the array that you want to use.

Page 10: Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing

Visualizing an Array of Objects

• Create the arrayCar[ ] myCars = new Car[5];

• Create the objectsfor(int i = 0; i< myCars.length; i++){

myCars[i] = new Car();

}

0 1 2 3 4

Ø Ø Ø Ø Ø

myCars

Page 11: Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing

Passing Arrays to Methods• Arrays can be input or output to methods

just like any other data type• Arrays are passed to methods by

reference, like objects• Arrays must be copied explicitly

int[ ] nums1 = {1,2,3};int[ ] nums2 = nums1;nums1[1] = 5;System.out.println(nums1[1]);System.out.println(nums2[1]);

Page 12: Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing

More Exercises• Create an array of 50 BankAccounts. Then deposit

$100 in each.

• Take an array of Car objects and return the Car with the lowest gas mileage. (Assume there is a getGasMileage() method).

public Car lowestGasMileage(Car[] myCars)

• Take an array of Students and return the student object whose name is given. Assume there is a getName() method.

public Student hasName(Student[] students, String name)

Page 13: Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing

Multidimensional Arrays

• We can also create arrays of arrays (matrices)

• int[ ][ ] matrix = new int[2][4]; 0 1 2 3

0

1

Page 14: Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing

Initializing Shortcut

• int[ ][ ] matrix = { {0,3,2,4},{8,9,2,1} };

0 1 2 3

0

1

0 3 2 4

8 9 2 1

Page 15: Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing

Multidimensional Arrays

• myNumbers[1][2] = 1

• myNumbers.length is the number of rows

• myNumbers[0] is the first row

• myNumbers[0].length is the number of elements in the first row (same as number of columns)

0 1 2 3

0

1

2 3 -1 4

7 4 1 8

myNumbers

Page 16: Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing

Array Patterns• Access every element in an array:

for(int i = 0; i<array.length; i++){int x = array[i];

}

• Access every element in a matrix:

for(int i = 0; i<array.length; i++){for(int j = 0; j< array[i].length; j++){

int x = array[i][j];}

}

Page 17: Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing

Exercises

• Create a multiplication table (mult[a][b] = axb)public int[][] multTable(int a, int b){

• Add all the elements in a tablepublic int addUp(int[][]myTable){

• Print the indices of a number in the tablepublic void findIndices(int target, int[][] table){