Util.Arrays and Reflect.Array in Java


The util.Arrays class

The java.util.Arrays class contains a static factory that allows arrays to be viewed as lists. Following are the important points about Arrays −

  • This class contains various methods for manipulating arrays (such as sorting and searching).

  • The methods in this class throw a NullPointerException if the specified array reference is null.

For example, the equals() method of this class accepts two arrays and compares them.

Example

 Live Demo

import java.util.Arrays;
public class ComparingArrays {
   public static void main(String args[]){
      String[] myArray1 = {"JavaFX", "HBase", "OpenCV", "WebGL", "FlexBox"};
      String[] myArray2 = {"JavaFX", "HBase", "OpenCV", "WebGL", "FlexBox"};
      Boolean result = Arrays.equals(myArray1, myArray2);
      System.out.println(result);
   }
}

Output

True

The asList() methods of this class accept an array and return a fixed-size list.

Example

 Live Demo

import java.util.Arrays;
import java.util.List;
public class ArrayToList {
   public static void main(String args[]){
      Integer[] myArray = {23, 93, 56, 92, 39};
      List list = Arrays.asList(myArray);
      System.out.println(list);
   }
}

Output

[23, 93, 56, 92, 39]

The lang.reflect.Array class

The Array class of the java.lang.reflect package provides static methods to create and access Java arrays dynamically. Array permits widening conversions to occur during a get or set operation but throws an IllegalArgumentException if a narrowing conversion would occur.

This class provides a newInstance() method, getter methods, and setter methods. The newInstance() method accepts an object of the class named Class representing the required component and an integer representing the length of the array, creates and returns an array with the given specifications.

The getter methods of this class getLong(), getInteger(), getLong(), etc. accepts an array in the form of an object and an integer representing an index and returns the element of the given array in the specified index.

The setter methods of this class setLong(), setInteger(), setLong(), etc. accepts an array in the form of an object, an integer representing the index and a value of the respective datatype and sets the given value at the specified index.

Example

 Live Demo

import java.lang.reflect.Array;
import java.util.Arrays;
public class Reflection_ArrayExample {
   public static void main(String args[]){
      int [] intArray = (int[]) Array.newInstance(int.class, 5);
      intArray[0] = 2001;
      intArray[1] = 12447;
      intArray[2] = 6358;
      intArray[3] = 902;
      intArray[4] = 6654;
      Array.setInt(intArray, 1, 1111);
      Array.setInt(intArray, 3, 3333);
      Array.setInt(intArray, 4, 4444);
      System.out.println(Arrays.toString(intArray));
      System.out.println(Array.getInt(intArray, 1));
      System.out.println(Array.getInt(intArray, 3));
      System.out.println(Array.getInt(intArray, 4));
   }
}

Output

[2001, 1111, 6358, 3333, 4444]
1111
3333
4444
raja
Published on 06-Sep-2019 17:27:13
Advertisements