public class ArrayExamples {
 /** Return the number of null items in list */
  public static int numNulls(Object[] list) {
    int numSoFar = 0; // num nulls so far
    for (int i = 0; i != list.length; i++) {
      if (list[i] == null) {
        numSoFar ++;
      }
    }
    return numSoFar;
  }
  
  /**  Return the chars in string as an array */
  public static char[] toCharArray(String s) {
    char[] result = new char[s.length()];
    for (int i = 0; i < s.length(); i++){
      result[i] = s.charAt(i);
    }
    return result;
  }
  
  /** print items in list looking like this:
   *  [v0, v1, v2, ..., vn-1]
   */
  public static void printCharArray(char[] list) {
    System.out.print("[");
    for (int i = 0; i < list.length - 1; i++) {
      System.out.print(list[i] + ", ");
    }
    if (list.length > 0) {
      System.out.print(list[list.length-1]);
    }                       
    System.out.print("]");
  }
  /** 
   * return the first index of s in list, or -1 if s is
   * not in the list.
   */
  public static int indexOf(String s, String[] list) {
    int i = 0;
    while (i != list.length && !s.equals(list[i])) {
      i++;
    }
    if (i == list.length) {
      return - 1;
    } else {
      return i;
    }
  }
/* These methods were written on Monday, March 7 */
  /** 
   *  Return the maximum integer in an array.
   *  @param nums the array of integers.
   *  @return the largest integer in nums.    
   */
  public static int maxInt(int[] nums) {
    int result = nums[0];
    for (int i = 1; i < nums.length; i++) {
      if (nums[i] > result) {
        result = nums[i];
      }
    }
    return result;
  }
 /** 
  * Accepts an integer array and swaps the elements in 
  * position p1 and p2 in the array.
  * @param nums the array of numbers.
  * @param p1 position in the array of first number to be swapped. 
  * @param p2 position in the array of second number to be swapped. 
  * @return the array with the integers in positions p1 and p2 swapped
  */
  public static int[] switchIt(int [] nums, int p1, int p2) {
    int temp;
    if (p1< nums.length && p2 < nums.length) {
      temp = nums[p1];
      nums[p1] = nums[p2];
      nums[p2] = temp;
    }
    return nums;
  }
  
      
  
}
