Java library's Arrays class

  • The Java library provides many useful operations on arrays

  • These operations provided in the java.util.Arrays class include:

      • Searching for some value stored in an array

      • Sorting an array

      • Comparing the content of 2 arrays

      • Make a copy of an array

      • Converting the data in an array to String representation (for printing)

  • To gain access to the operations in java.util.Arrays, you must import it as follows:

         import java.util.Arrays              

  • You can google the documentation of the java.util.Arrays class:

Searching an array with binary search

  • You can use the binary search method in the Arrays class as follows:

     Arrays.binarySearch( arrayName, key ): returns the index of array 
                                            element containing key
    					neg. index = not found

    Example:

    import java.util.Arrays;
    
    public class BinSearch
    {
       public static void main(String[] args)
       {
         int[] list = {2, 4, 7, 10, 11, 45, 50, 59, 60, 70, 79};
    
         int index1 = Arrays.binarySearch(list, 11);  // returns 4
         int index2 = Arrays.binarySearch(list, 999); // returns a neg value
       }
    } 

DEMO: demo/08-array/14-arrays-class/BinSearch.java

Sorting an entrie array

  • You can sort an entire array using:

        Arrays.sort( arrayName )  

    Example:

    import java.util.Arrays;
    
    public class myProg
    {
       public static void main(String[] args)
       {
           double[] myList = {8, 4, 9, 7, 3, 5, 6};
     
           Arrays.sort(myList); // Sort the whole array
    
           // Result:  myList = { 3, 4, 5, 6, 7, 8, 9 }
       }
    } 

DEMO: demo/08-array/14-arrays-class/SortArray.java

Sorting a partial array

  • You can sort a part/portion of an array using:

     Arrays.sort(arrayName,  s ,  e ): sorts elems s to (e−1) in "arrayName"

    Example:

    import java.util.Arrays;
    
    public class myProg
    {
       public static void main(String[] args)
       {
           char[] myList = { 'X', 'B', 'Z', 'A', 'Y', 'C' };
     
           Arrays.sort(myList, 2 , 5 ); // Sort elems 2, 3, 4
    
           // Result:  myList = { 'X', 'B', 'A', 'Y', 'Z', 'C' }
       }
    } 

DEMO: demo/08-array/14-arrays-class/SortArrayPart.java

Check if 2 arrays are equal

  • You can check if 2 arrays contains the same list of values with equals( ):

     Arrays.equals( arrayName1, arrayName2 )   

    Example:

       public static void main(String[] args)
       {
          int[] list1 = {2, 4, 7, 10};
          int[] list2 = {2, 4, 7, 10};
          int[] list3 = {4, 2, 7, 10};
    
          boolean result;
    
          result = Arrays.equals(list1, list2); // true
          result = Arrays.equals(list2, list3); // false
       }

DEMO: demo/08-array/14-arrays-class/Equals.java

Make a copy of an part or whole array

  • You can make a copy of an array with copyOf( ):

     Arrays.copyOf(arrayName, N): returns a copy of the 
                                  first N elements in array

    Example:

    import java.util.Arrays;
    
    public class myProg
    {
       public static void main(String[] args)
       {
            int[] list = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
            int[] copy
    
    	copy = Arrays.copyOf(list, 4);    // copy = {1, 2, 3, 4}
    	copy = Arrays.copyOf(list, list.length); // Copy entire array
       }
    } 

DEMO: demo/08-array/14-arrays-class/CopyOf.java

Fill whole or part of array with a specific value

  • You can use the fill( ) method to fill in all or a part of an array:

     Arrays.fill(arrayName, val):         fill whole array with val
     Arrays.fill(arrayName, s , e , val): fill elems [s]-[e−1] with val

    Example:

    import java.util.Arrays;
    
    public class myProg
    {
       public static void main(String[] args)
       {
          int[] list = new int[10];
    
          Arrays.fill(list, 4); // Fill whole array with the value 4
    
          Arrays.fill(list, 2 , 5 , 7); // Fill list[2-4] with the value 7
       }
    } 

DEMO: demo/08-array/14-arrays-class/Fill.java

Convert all values in an array into a single string (for printing without using a loop)

  • The Arrays.toString(arrayName) method to returns a string representation of all elements in the array:

     Arrays.toString(arrayName): return a string of 
                                 all elements in array

    Example: 2 ways to print an array:

       public static void main(String[] args)
       {
           int[] list = {1, 2, 3, 4};
    
           for (int i = 0; i < list.length; i++) // Method 1 to print array
               System.out.println( list[i] );
    
           // Method 2 to print array
           String s;
    
           s = Arrays.toString(list); // Convert all elements to a string
           System.out.println(s);
       } 

DEMO: demo/08-array/14-arrays-class/ToString.java