Arrays (java.util.Arrays)

This class contains various methods for manipulating arrays (such as sorting and searching). This class also contains a static factory that allows arrays to be viewed as lists.
The methods in this class all throw a NullPointerException if the specified array reference is null.

 

Converting an Array to a Collection

    // Fixed-size list
    List list = Arrays.asList(array);
   
    // Growable list
    list = new LinkedList(Arrays.asList(array));
   
    // Duplicate elements are discarded
    Set set = new HashSet(Arrays.asList(array));

 

Converting a Collection to an Array

    // Create an array containing the elements in a list
    Object[] objectArray = list.toArray();
    MyClass[] array = (MyClass[])list.toArray(new MyClass[list.size()]);
   
    // Create an array containing the elements in a set
    objectArray = set.toArray();
    array = (MyClass[])set.toArray(new MyClass[set.size()]);
   
    // Create an array containing the keys in a map
    objectArray = map.keySet().toArray();
    array = (MyClass[])map.keySet().toArray(new MyClass[set.size()]);
   
    // Create an array containing the values in a map
    objectArray = map.values().toArray();
    array = (MyClass[])map.values().toArray(new MyClass[set.size()]);

 

Filling Elements in an Array

The Arrays class has conveninent methods for filling arrays of all eight primitive types:

    boolean[] booleanArr = new boolean[10];
    boolean booleanFillValue = false;
    Arrays.fill(booleanArr, booleanFillValue);
   
    byte[] byteArr = new byte[10];
    byte byteFillValue = (byte)0xFF;
    Arrays.fill(byteArr, byteFillValue);

Simillar methods available for filling arrays of all eight primitive types.

There is also a method for filling object arrays:

    String[] StringArr = new String[1];
    String StringFillValue = "";
    Arrays.fill(StringArr, StringFillValue);

By default, the nine fill methods shown above set all the elements in the array with the fill value. Each fill method has an overloaded version that can restrict the fill to a contiguous range of elements:

    // Fill elements 0, 1, 2, and 3; the end index is exclusive
    int startIndex = 0;
    int endIndex = 4;    
    Arrays.fill(booleanArr, startIndex, endIndex, booleanFillValue);
    Arrays.fill(byteArr, startIndex, endIndex, byteFillValue);

 

Comparing Arrays

    // null arrays are equal
    boolean[] bArr1 = null;
    boolean[] bArr2 = null;
    boolean b = Arrays.equals(bArr1, bArr2);                   // true
   
    // Compare two boolean arrays
    bArr1 = new boolean[]{true, false};
    bArr2 = new boolean[]{true, false};
    b = Arrays.equals(bArr1, null);                            // false
    b = Arrays.equals(bArr1, bArr2);                           // true
   
    // There are equals() methods for all eight primitive types
    b = Arrays.equals(new byte[]{0}, new byte[]{0});           // true
    b = Arrays.equals(new char[]{'a'}, new char[]{'a'});       // true
    b = Arrays.equals(new short[]{0}, new short[]{0});         // true
    b = Arrays.equals(new int[]{0}, new int[]{0});             // true
    b = Arrays.equals(new long[]{0L}, new long[]{0L});         // true
    b = Arrays.equals(new float[]{0F}, new float[]{0F});       // true
    b = Arrays.equals(new double[]{0D}, new double[]{0D});     // true
   
    // When comparing Object arrays, null elements are equal.
    // If the elements are not null, Object.equals() is used.
    b = Arrays.equals(new String[]{"a"}, new String[]{"a"});   // true
    b = Arrays.equals(new String[]{null}, new String[]{null}); // true
    b = Arrays.equals(new String[]{"a"}, new String[]{null});  // false

 

Shuffling the Elements of a List or Array

Use Collections.shuffle() to randomly reorder the elements in a list.

    // Create a list
    List list = new ArrayList();
   
    // Add elements to list
   
    // Shuffle the elements in the list
    Collections.shuffle(list);
   
    // Create an array
    String[] array = new String[]{"a", "b", "c"};
   
    // Shuffle the elements in the array
    Collections.shuffle(Arrays.asList(array));

 

Way to determine if a String element is part of an array of Strings:

public class Test {
 public static void main(String[] args) {
  String myValue = "bidrd";
  String[] animals = { "snake", "kangaroo", "wombat", "bird" };
  // We must call sort method before calling binarySearch
  java.util.Arrays.sort(animals);
  int position = java.util.Arrays.binarySearch(animals, myValue);
  System.out.println("position => " + position);
  if (position >= 0) {
   System.out.println(myValue + " is contained in the array");
  } else {
   System.out.println(myValue + " is not contained in the array");
  }
 }
}