Try It Yourself: arrays

  1. Complete the method reverseIt that takes and array of Strings and returns an array consisting of the same Strings but in reverse order. For example, if strArray contains "one", "two", and "three" as its first, second, and third elements, then reverseIt(strArray) returns an array containing "three", "two", and "one" as its first, second, and third elements, respectively.
          public static String[] reverseIt(String[] array) {
          
          


  2. Complete the method average that takes and array of doubless and returns the average of the values in the array.
          public static double average(double[] values) {
          
          


  3. Suppose that word is an array of char (i.e., word has been declared to be char[]). Write a code fragment that takes replaces every a and A in word with @ and every e and E in word with 3. For example, if word contains the characters h a c k e r, then after the code fragment exectuted, word contains the characters h @ c k 3 r.


  4. Complete the method sameShape that returns true if the two matrices have the same shape and false otherwise.  A matrix is simply a two-dimensional array of numbers.  Two matrices have the same shape if they have the same number of rows and corresponding rows have the same number of columns.
          public static boolean sameShape(int[][] matrixA, int[][] matrixB) {
          
          


  5. Write a code fragment that adds two integer matrices A and B and stores the result in matrix sum.  Recall that matrix addition is done element-wise, i.e. to find the value in row i, column j of A+B, just add the value in row i, column j of A and the value in row i, column j of B.  You may assume that A and B have been declared as int[][] and created.