Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
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 each one out • Collections are data types that hold groups of data 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 Array Indices 4 7 3 12 -4 87 0 45 0 1 2 3 4 5 6 7 • Elements in an array are numbered for convenience • Numbering starts at 0, like Strings 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!!! 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. 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!!! 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 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. Visualizing an Array of Objects • Create the array Car[ ] myCars = new Car[5]; myCars Ø Ø Ø Ø Ø 0 1 2 3 4 • Create the objects for(int i = 0; i< myCars.length; i++){ myCars[i] = new Car(); } 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]); 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) Multidimensional Arrays • We can also create arrays of arrays (matrices) • int[ ][ ] matrix = new int[2][4]; 0 0 1 1 2 3 Initializing Shortcut • int[ ][ ] matrix = { {0,3,2,4},{8,9,2,1} }; 0 1 2 3 0 0 3 2 4 1 8 9 2 1 Multidimensional Arrays 0 1 2 3 0 2 3 -1 4 1 7 4 1 8 myNumbers • • • • 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) 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]; } } 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){