All pastes #741742 Raw Edit

javalab

public text v1 · immutable
·published 2007-10-18 22:58 UTC
rendered paste body
public class ArrayLab 
{
  public static void main(String[] args) 
  {
    // use array initializer to declare, create and initialize an array 
    char[] testArray1 = {'a', 'b', 'c', 'd'};
    char[] testArray2;
    
    printArray("The initial array is: ", testArray1);
    
    // B: write your method call to reverseArray here
    
    printArray("The reversed array is: ", testArray2);
    
    int[][] grid = {{1, 2, 3}, {4, 5, 6}};
    
    // D: write your method call to print2DArray here
  }
  
  // print out an array
  public static void printArray(String promptMessage, char[] a) 
  {
    System.out.print(promptMessage);
    for (int i = 0; i < a.length; i++) 
    {
      System.out.print(a[i]);
    }
    System.out.println();
  }
    
  public static char[] reverseArray(char[] a)
  {
    // A: write your code for method reverseArray here
    // 1. Declare a character array reference variable
    
    char[] b;
    // 2. Create a new character array using the "new" operator and assign the reference to the variable declared in 1. 
    //    The size of the new array should be the same as "a", use "a.length" to get the size  
    b = new char[a.length];
    
    // 3. Use a "for" loop to assign values to the new array so that it is a reversal of "a". 
    
    //    For example, the element at index 0 in the new array should be the same as the last element in a, i.e. a[a.length-1].

    
    // 4. Return the reference to the new array
  }
  
  public static void print2DArray(String promptMessage, int[][] a)
  {
     // C: write your code for method print2DArray here??? Not sure
     System.out.print(promptMessage);
    for (int i = 0; i < a.length; i++)
    {
      System.out.print(a[i]);
    }
    System.out.println();
     // 1. Use the outer "for" loop to go down rows, there are "a.length" rows.
     // 2. Use the inner "for" loop to print out every element in a row. 
     //    For example, there are "a[0].length" elements in the first row.

  }
}