import java.util.ArrayList;

public class ArrayListTester {

  public static void main ( String[] args ) {
  
    // Declare an array of Strings (capacity 10)
    String[ ] arr = new String [ 10 ];
    // Declare a variable to keep track of the number of 
    //   items in the array
    int numItems = 0;
    
    // Declare an ArrayList of String
    ArrayList <String> list = new ArrayList <String>();
    
    // Add 5 Strings to each data structure
    for (int i = 0; i < 5; i++) {
      System.out.print ( "Enter name: " );
      String s = StdIn.readString ( );
      // add to array
      //PRECONDITION: i < arr.length
      arr[i] = s;
      numItems++;
      
   // add to ArrayList - No need to check if there is room!!!
      System.out.print ( "Enter another name: " );
      String s2 = StdIn.readString ( );
      list.add(s2);
  }
  
    // call method to print array elts
  printArray(arr, numItems);
  // call method to print ArrayList elts
  printList(list);
  System.out.println (  );
  
  System.out.println ( "Adding THOMAS to end" );
  // call method to add "THOMAS" to the end of the array
  // if method returns true (item added) then increase numItems
  if (addToArray(arr, numItems, "THOMAS")) {
    numItems++;
  }
  // call method to print array elts 
  printArray(arr, numItems);  
  // call method to add "THOMAS" to the end of the ArrayList
  list.add ( "THOMAS" ); //java.util.ArrayList provides this method
  // call method to print ArrayList elts
  printList(list);
  System.out.println (  );
  
  System.out.println ( "Adding ANNIE to index 1" );
  // call method to add "ANNIE" to position 1 in the array
  // if there was room; if item added, increment numItems
  if (addToArr(arr, numItems, "ANNIE", 1)) {
    numItems++;
  }
  // call method to print array elts
  printArray(arr, numItems);
  // call method to add "ANNIE" to position 1 in the ArrayList
  list.add ( 1, "ANNIE");
  // call method to print ArrayList elts
  printList(list);
  System.out.println (  );
  
  System.out.println ( "Removing from index 2" );
   // add method to remove item in index 2 of array 
  if (removeFromArray(arr,2, numItems)){
    numItems--;
  }
  printArray(arr, numItems);
  // call method to remove item in index 2 of ArrayList
  list.remove(2);
  printList(list);
  System.out.println(list);
}

  //Prints elements in ArrayList-For Each Loop
  public static void printList(ArrayList<String> aList) {
    System.out.println ( "Printing ArrayList-For Each Loop" );
    for (String elt : aList) {
      System.out.print ( elt + " ");
    }
    System.out.println ( );

  //Prints element in ArrayList-standard For Loop
    System.out.println ( "Printing ArrayList-Standard For Loop" );
    for(int i = 0; i < aList.size(); i++){
      System.out.print(aList.get(i) + " ");
    }
    System.out.println();
  }
  //Prints elements in array-standard For Loop
  public static void printArray(String[] a, int num) {
    System.out.println ( "Printing Array" );
    for(int i = 0; i < num; i++) {
      System.out.print  ( a[i] + " " );
    }
    System.out.println (  );
  
  //Prints elements in array-enhanced for loop
  System.out.println ( "Printing Array-For-Each loop" );
  for (String elt : a) {
    System.out.print ( elt + " ");
  }
  System.out.println ( );
}

  
   public static boolean addToArray(String[] arr,int num, String s) {
     if (num < arr.length 
     ) {
       arr[num] = s;
       return true;    
   }
     return false;
   }
   
   //add s to array a in position place if room and returns true; 
   // Otherwise, returns false
   public static boolean addToArr(String[] a, int num, String s, int place ) {
     if (num < a.length) {
       //shifting elements in array one place to the right
       // to make room for item to be added.
       for (int i = num; i >= place; i--) {
         a[i] = a[ i - 1];     
       }
       // adds item to correct position
       a[place] = s;
       return true;
     }
    return false; 
   }
   
   public static boolean removeFromArray(String[] a, int pos, int numInArr) {
     if(pos >= a.length) {
       return false;
     }
     for(int i = pos; i < numInArr - 1; i++) {
       a[i] = a[i + 1];
     }
     a[numInArr - 1] = null;
     return true;
   }
}