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 (Note: Most of this we covered on Wednesday) • Array is a data structure that represents a collection of the same types of data. • A "fixed length list". • double[] myList = new double[10]; Declaring an array: datatype[] arrayRefVar = new datatype[arraySize]; double[] myList = new double[10]; • The new keyword is a Java operator that creates the object. • new calls a constructor, which initializes the new object. – Everything in java is class-based. So we need to make objects (instances) for things that are not a primitive type. Declaring an array and putting values into the array in one step: double[] myList = {1.9, 2.9, 3.4, 3.5}; • This shorthand notation is equivalent to the following statements: double[] myList = new double[4]; myList[0] = 1.9; myList[1] = 2.9; myList[2] = 3.4; myList[3] = 3.5; Can do: • int[] anArr = {1, 9, 4, 3}; • int[] anArr = new int[4]; anArr[0] = 1; anArr[1] = 9; anArr[2] = 4; anArr[3] = 3; Can’t do: • int[] anArr = new int[4]; anArr = {1, 9, 4, 3}; • anArr[4] = 5; //Arrays are FIXED LENGTH! What if we want to concatenate 2 arrays into one, longer array? public static int[] addingarrays(int[] arr1, int[] arr2){ int x = arr1.length; int y = arr2.length; int[] newarr = new int[x+y]; for (int ind=0;ind<arr1.length;ind++) { // is this the most efficient? newarr[ind]= arr1[ind]; System.out.println(newarr[ind]); } for (int ind=0;ind<arr2.length; ind++){ newarr[x+ind] = arr2[ind]; System.out.println(newarr[x+ind]); } System.out.println(newarr); //will this work? return newarr; } Note arr2.length and arr2.length. These give you the number of elements in the array. Isn’t that nice of java? Printing an array results in printing out its hashcode (a representation of an object as a hash code). This may be useful to your computer, but it’s not terribly useful to us. We want to print out the values in the array. There are a couple different ways to do this. One is to use a loop, looping through each value in the array and printing it out, e.g., for (int i=0; i < newarr.length; : i++) { System.out.println(newarr[i]); } Or (see below for more details…) for (int i: newarr) { System.out.println(i); } Another way is to use java.util.Arrays, which is a class with methods for arrays. import java.util.Arrays; public class ArrayMethods { public static void print(int[] newarr) { System.out.println(Arrays.toString(newarr)); } // ... } java.util.Arrays contains useful methods for dealing with Arrays, including: – Sort • Arrays.sort(newarr); – binarySearch • int index = Arrays.binarySearch(newarr,3); – Equals • Arrays.equals(newarr,array2) // sees if values inside of array are equal – Fill • int[] array = new int[5]; • Arrays.fill(array,0); – toString (see above example) Back to for loops. You can loop through every element in an array as follows: int[] newarr = {3,2,4,1,5}; for (int i: newarr) { System.out.println(i); } System.out.println(“bye”); In this case, I becomes every element in the array new. So I becomes 3, and that is what is printed out inside the loop. Then I becomes 2 and that is what is printed out inside the loop. Then I becomes 4… You see the pattern. I will become every element in the list. When there are no more elements in the list, then the loop stops and “bye” is printed out. Practice Problems (Yes, you could plug these into a computer to see what happens. But isn’t that kind of pointless in terms of figuring it out?) Problem 1: Which of the methods provided by java.util.Arrays does the following method mimic? … System.out.println(st(new int[] {3,24,6,12,8,9,42})); … public static String st(int[] x){ String s = ""; for (int i:x) { s += i; } return s; } Problem 2: Which of the methods provided by java.util.Arrays does the following method mimic? … System.out.println(ss(3242, new int[] {12,132,253,1032,2428,3242,4281,5550})); … public static int ss(int num, int[] x) { int k = 0; int h = x.length - 1; while(h >= k) { int m = (k + h) / 2; if(x[m] == num) { return m; } if(x[m] < num) { k = m + 1; } if(x[m] > num) { h = m- 1; } } return -1; } Problem 3: Which of the methods provided by java.util.Arrays does the following method mimic? … int[] arr2 = new int[7]; st(arr2,32); System.out.println(st(arr2)); … public static void st(int[] x, int y){ int k = x.length; for (int i=0;i<k;i++) { x[i]=y; } } Problem 4: Which of the methods provided by java.util.Arrays does the following method mimic? … int[] arr3 = {32,12,24,57,23,18,96,4,14}; is(arr3); System.out.println(Arrays.toString(arr3)); … } public static void is(int[] x) { for (int i = 1; i < x.length; i++){ int k = x[i]; int m; for (m = i-1; m>=0 && x[m] > k; m-- ) { x[m+1]= x[m]; } x[m+1] = k; } } Problem 5: Write a method that mimics the Arrays.equals method: Problem 6: what’s printed? … System.out.println(ma(new int[] {3,7,3,42,31,28,5})); … public static int ma(int[] x){ int y = x[0]; for (int k:x){ y = (k>y)?k:y; } return y; } Problem 7: Fix the blanks in the following code so that it reverses an array (in-place) public static void reverse(int[] values) { for (int i = 0; i < values.length / 2; i++) { int temp = values[i]; values[i] = __________________________; ________________________ = temp; } } Problem 8: Fill in the blank so that given an array of ints, the method return true if the array contains no 1's and no 3's, e.g., lucky13({0, 2, 4}) → true lucky13({1, 2, 3}) → false lucky13({1, 2, 4}) → false public static boolean lucky13(int[] nums) { boolean no1or3s = true; int i = 0; while (no1or3s && i < nums.length) { __________________________________________; i++; } return no1or3s; } Problem 9: Given an array of ints, fill in the blank so that the method return true if the sum of all the 2's in the array is exactly 8, e.g., sum28({2, 3, 2, 2, 4, 2}) → true sum28({2, 3, 2, 2, 4, 2, 2}) → false sum28({1, 2, 3, 4}) → false public static boolean sum28(int[] nums) { int sum = 0; for (int value : nums) { if (value == 2) { sum++; } } return _________________; } Problem 10 what’s printed? … System.out.println(m2(new int[][] {{32,74,22,21},{12,18,31},{3,16,19,38,55},{78,12}})); … public static int m2(int[][] x){ int z = -1; for (int[] k:x){ for (int p:k){ z = (p>z)?p:z; } } return z; } Problem 11: what’s printed? … int[] arr = {3,4,2,7,1}; printit(bg(arr)); … public static char[][] bg(int[] x){ char[][] na = new char[x.length][]; int ct=0; for (int k: x){ char[] n = new char[k]; for (int y=0;y<k;y++){ n[y]='*'; } na[ct++]=n; } return(na); } public static void printit(char[][] x){ int z = 0; for (char[] t:x){ z = t.length > z?t.length:z; } for (int j=0;j<z;j++) { for (int i=0;i<x.length;i++){ if (x[i].length > j) { System.out.print(x[i][j]+"\t"); //print doesn't print a new line after each call } else { System.out.print("\t"); } } System.out.println(); } } Problem 12: what’s printed? … System.out.println(ac(new int[] {1,2,0,1,3,1,3,2,1,2,3,3,1},new int[] {1,2,3})); System.out.println(ac(new int[] {1,2,0,1,3,1,3,2,1,2,3,3,1},new int[] {1,2,1})); … public static boolean ac(int[] nums, int[] nums2) { boolean ret = false; for (int i=0; i < (nums.length-nums2.length + 1); i++) { if (nums[i]==nums2[0]) { ret = true; for (int k = 1; k < nums2.length; k++) { if (nums[k+i] != nums2[k]) { ret = false; } } } if (ret) { return (true); } } return false; } Problem 13: Given an array of ints, return true if one of the first 4 elements in the array is a 9. The array length may be less than 4. arrayFront9({1, 2, 9, 3, 4}) → true arrayFront9({1, 2, 3, 4, 9}) → false arrayFront9({1, 2, 3, 4, 5}) → false Problem 14: Write a method that takes as an input parameter an array of integers and returns an array of integers twice as long. Your new array should hold each value twice (next to each other). makeBigger({3,7,2,4})->{3,3,7,7,2,2,4,4}); Problem 15: Return a version of the given array where each zero value in the array is replaced by the largest value to the right of the zero in the array. If there is no value greater than zero to the right of the zero, leave the zero as a zero. zeroMax({0, 5, 0, 3}) → {5, 5, 3, 3} zeroMax({0, 2, 0, 3}) → {3, 2, 3, 3} zeroMax({0, 1, 0}) → {1, 1, 0} * this can be done in ONE loop (and without built-in functions). public static int[] zeroMax(int[] nums) { // ... }