Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Arrays

2 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

3 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

4 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

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

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

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

8 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

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

10 Visualizing an Array of Objects Create the array Car[ ] myCars = new Car[5]; Create the objects for(int i = 0; i< myCars.length; i++){ myCars[i] = new Car(); } 0 1 2 3 4 Ø Ø Ø Ø Ø myCars

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

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

13 Multidimensional Arrays We can also create arrays of arrays (matrices) int[ ][ ] matrix = new int[2][4]; 0 1 2 3 0 1

14 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

15 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

16 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]; } }

17 Exercises Create a multiplication table (mult[a][b] = axb) public int[][] multTable(int a, int b){ Add all the elements in a table public int addUp(int[][]myTable){ Print the indices of a number in the table public void findIndices(int target, int[][] table){


Download ppt "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."

Similar presentations


Ads by Google