Review:    helper/support methods for array

  • Previously, we have studied the array data structure in Chapter 7 of the text book

  • When we discussed the array, we also learned about some support methods in the java.util.arrays class (), such as:

      Arrays.sort( arrayName ): 
             sort an array
    
      Arrays.equals( array1, array2 ):
             check if 2 arrays contains the same elements
    
      Arrays.copyOf(arrayName, arrayName.length): 
             returns a copy of an array
    
      Arrays.toString(arrayName): 
             returns a string of the elements in the array
    
      and so on


  • Java's library also contains support methods for ArrayList -- discussed next

  Support methods for ArrayList  

  • The support methods for ArrayList are stored in the:

    • java.util.Collections class (online doc: )


  • We will discuss the following support methods for:

    • Convert between an array and an ArrayList:

      • Converting an array to an ArrayList

      • Converting an ArrayList to an array

    • Sort an ArrayList

    • Find the maximum and minimum value stored in an ArrayList

    • Shuffle the elements stored in an ArrayList

Converting an Array into an ArrayList     Array ⇒ ArrayList

  • To convert an array to an array list, you instantiate an array list using an array as follows:

     String[] myArray = {"red", "green", "blue"};  // Array to be converted
    
     ArrayList<String> myList = new ArrayList<>( Arrays.asList(myArray) );  

    Arrays.asList(myArray) returns a list of elements in myArray.


  • Alternately, we can use a loop and add the elements one at a time to the ArrayList:

     String[] myArray = {"red", "green", "blue"};
    
     ArrayList<String> myList = new ArrayList<>(); // Create ArrayList
            
     // Add elements in myArray to myList
     for (int i = 0; i < myArray.length; i++) 
         myList.add( myArray[i] ); 

DEMO: demo/11-arrayList/05-support/ArrayToArrList.java

Converting an ArrayList into an Array     ArrayList ⇒ Array

  • To convert an array list to an array, you use the instance method toArray(myArray) that copies the elements in an ArrayList to the array myArray:

      ArrayList<String> myList = ... ;    // ArrayList to be converted
    
      String[] myArray = new String[ myList.size() ]; // Create array
    
      myList.toArray( myArray ); // Copies elems in myList to myArray 


  • Alternately, we can use a loop and copy the elements one at a time to the array:

      ArrayList<String> myList = ... ;    // ArrayList to be converted
    
      String[] myArray = new String[ myList.size() ]; // Create array
            
      for ( int i = 0; i < myArray.length; i++ )  // Loop to copy elements
         myArray[i] = myList.get(i);

DEMO: demo/11-arrayList/05-support/ArrListToArray.java

The sort support method for ArrayList objects

  • The java.util.Collections class contains a (static) sort(list) method that sorts elements stored in an ArrayList

  • Example of the use of sort( ):

     public static void main(String[] args)
     {
         ArrayList<String> myList = new ArrayList<>();
         myList.add("red"); myList.add("green"); myList.add("blue");
    
         System.out.println( myList );  // Unsorted
    
         Collections.sort( myList );    // Sorts a list
    
         System.out.println( myList );  // Sorted
     } 

DEMO: demo/11-arrayList/05-support/Sort.java

Find the maximum or the minimum in an ArrayList

  • The java.util.Collections class contains a (static) max(list) and min(list) method that find the max/min value stored in an ArrayList object:

      Collections.max( ArrayList ): returns the maximum element
                                    in the ArrayList
    
      Collections.min( arrayList ): returns the minimum element
                                    in the ArrayList
    

    Example:

     public static void main(String[] args)
     {
         ArrayList<String> myList = new ArrayList<>();
         myList.add("red"); myList.add("green"); myList.add("blue");
    
        System.out.println("Max = " + Collections.max( myList ) );
        System.out.println("Min = " + Collections.min( myList ) );
     }

DEMO: demo/11-arrayList/05-support/MaxMin.java

Shuffle the elements in an ArrayList

  • The java.util.Collections class contains a (static) shuffle(list) method that randomly shuffles the elements stored in an ArrayList object:

      Collections.shuffle( ArrayList )
    

    Example:

     public static void main(String[] args)
     {
         ArrayList<String> myList = new ArrayList<>();
         myList.add("aaa"); myList.add("bbb"); myList.add("ccc");
         myList.add("ddd"); myList.add("eee"); myList.add("fff");
     
         System.out.println( myList );   // Original order
    
         Collections.shuffle( myList );  // Shuffles a list
    
         System.out.println( myList );   // Show new order
        }

DEMO: demo/11-arrayList/05-support/Shuffle.java