Array Class in Java

digitization, transformation, binary

Array Class in Java:

Arrays in short:

  1. Arrays are dynamically created with new keyword.
  2. Arrays are manipulated by reference.
  3. Arrays are automatically garbage collected where no longer referred to.

In this post I have talked about Array concepts and how to use it.In this post we are going to check the structure of class Array. Array class contains set of methods that set a value or query from the set of homogeneous objects.. This class also provide us method

Ads code goes here
  • To determine the size of Array
  • How to create a new instance of an Array.
  • How to manipulate an Array.

In Java array is an ordered collection of similar type of variable or data items .Java does not allow us to specify the size of the array at the time of declaration. The memory allocation is purely dynamic. Now Java treats the array as an object with an attribute – length. This length attribute specifies the number of elements in the array.

All elements of the array are initialized to zero by default, but if we want to provide some values , we need to initialize explicitly. Array index starts from zero.

Two arrays can point to the same reference


int x [ ], y [ ];
x = y = new int [7] ;

Here one array is created with new operator. The reference of the array is copied to y and x respectively.
Note – Assigning a reference does not create a copy of the array object.

The methods of Array class are static that applies to all array values.In other words, the Array class provides static methods to manipulate arbitrary arrays in java.There are methods to set and retrieve elements in an array, determine the size of an array and create a new instance of an array.

Array class is used to create an array object and manipulate their elements. The Array class is not used to represent array types.Arrays in Java are objects. Array types are represented by class objects.

Array Class in Java
Array Class in Java Excellannt books for Array

The class structure of Array is given as:


public final class java.lang.reflect.Array extends
                   java.lang.Object implements
                   java.lang.Clonable,
                   java.io.Serializable{
//Methods
public static native Object get(Object array, int index) 
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
public static native boolean getBoolean(Object array, int index) 
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
public static native byte getByte(Object array, int index) 
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
public static native char getChar(Object array, int index) 
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
public static native double getDouble(Object array, int index) 
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
public static native float getFloat(Object array, int index) 
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
public static native int getInt(Object array, int index) 
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
public static native int getLength(Object array) 
throws IllegalArgumentException;
public static native long getLong(Object array, int index) 
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
public static native short getShort(Object array, int index) 
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
public static Object newInstance(Class componentType, int length) 
throws NegativeArraySizeException;
public static Object newInstance(Class componentType, int[] dimensions) 
throws IllegalArgumentException, NegativeArraySizeException;
public static native void set(Object array, int index, Object value)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
public static native void setBoolean(Object array, int index, boolean z)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
public static native void setByte(Object array, int index, byte b)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
public static native void setChar(Object array, int index, char c) 
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
public static native void setDouble(Object array, int index, double d)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
public static native void setFloat(Object array, int index, float f)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
public static native void setInt(Object array, int index, int i) 
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
public static native void setLong(Object array, int index, long l)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
public static native void setShort(Object array, int index, short s)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
} 

The get() method returns the element at the specified index as an Object. If the array elements are primitive data type,the value is converted to equivalent Wrapper class and returned.
we can use

  • getInteger()
  • getBytes()
  • getChar()
  • getDouble()
READ  Class PipedWriter Class in Java

methods to query an Array to get primitive types.

All get methods follow mostly a similar pattern that is

get(Object array,int index) throws IllegalArgumentException,
ArrayIndexOutOfBoundsException,NullPointerException

Parameters:

array- An array Object
index-An index in the array
returns- The object at the given index in the specified array.
IllegalArgumentException– If the given object is not present in the array.
ArrayIndexOutOfBoundsException– if the given index is invalid.
NullPointerException-If the array is null.

The method details

public static native Object get(Object array, int index) 
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;

This method returns the object at the given index in the array.If the array contains values of a primitive type,the value at the given index is wrapped in an appropriate object and the object is returned.

Parameters:

array- An array Object
index-An index in the array

public static native boolean getBoolean(Object array, int index) 
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;

This method returns the object at the given index in the array as a boolean value . if this method can not convert the value to a Boolean, this method may throw the below exceptions-

Parameters:

array- An array Object
index-An index in the array

IllegalArgumentException– If the given object is not present in the array.
ArrayIndexOutOfBoundsException– if the given index is invalid.
NullPointerException-If the array is null.

public static native byte getByte(Object array, int index) 
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;

This method returns the object at the given index in the array as a Byte value . if this method can not convert the value to a Byte, this method may throw the below exceptions-

Parameters:

array- An array Object
index-An index in the array

IllegalArgumentException– If the given object is not present in the array.
ArrayIndexOutOfBoundsException– if the given index is invalid.
NullPointerException-If the array is null.

public static native char getChar(Object array, int index) 
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;

This method returns the object at the given index in the array as a Character value . if this method can not convert the value to a Character , this method may throw the below exceptions-

Parameters:

array- An array Object
index-An index in the array

IllegalArgumentException– If the given object is not present in the array.
ArrayIndexOutOfBoundsException– if the given index is invalid.
NullPointerException-If the array is null.

public static native double getDouble(Object array, int index) 
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;

This method returns the object at the given index in the array as a double value . if this method can not convert the value to a Double , this method may throw the below exceptions-

Parameters:

array- An array Object
index-An index in the array

IllegalArgumentException– If the given object is not present in the array.
ArrayIndexOutOfBoundsException– if the given index is invalid.
NullPointerException-If the array is null.

public static native float getFloat(Object array, int index) 
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;

This method returns the object at the given index in the array as a float value . if this method can not convert the value to a Float, this method may throw the below exceptions-

Parameters:

array- An array Object
index-An index in the array

IllegalArgumentException– If the given object is not present in the array.
ArrayIndexOutOfBoundsException– if the given index is invalid.
NullPointerException-If the array is null.

public static native int getInt(Object array, int index) 
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;

This method returns the object at the given index in the array as a int value . if this method can not convert the value to a Integer, this method may throw the below exceptions-

READ  How to stop a Test Process If that Is Hanged or Went To Infinite loop

Parameters:

array- An array Object
index-An index in the array

IllegalArgumentException– If the given object is not present in the array.
ArrayIndexOutOfBoundsException– if the given index is invalid.
NullPointerException-If the array is null.

public static native int getLength(Object array) 
throws IllegalArgumentException;

This method returns the length of an array.

Parameters:

array-the given array

public static native long getLong(Object array, int index) 
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;

This method returns the object at the given index in the array as a long value . if this method can not convert the value to a Long, this method may throw the below exceptions-

IllegalArgumentException– If the given object is not present in the array.
ArrayIndexOutOfBoundsException– if the given index is invalid.
NullPointerException-If the array is null.

public static native short getShort(Object array, int index) 
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;

This method returns the object at the given index in the array as a short value . if this method can not convert the value to a Short, this method may throw the below exceptions-

Parameters:

array- An array Object
index-An index in the array

IllegalArgumentException– If the given object is not present in the array.
ArrayIndexOutOfBoundsException– if the given index is invalid.
NullPointerException-If the array is null.

public static Object newInstance(Class componentType, int length) 
throws NegativeArraySizeException;

This method returns a newInstance of the array or creates a single dimensional array with the given length and component type.

Parameters:

componentType-the type of each element in the array.
Length-The length of the array.
NullPointerException-If the componentType is null.
NegativeArraySizeException-if the size /length is negative.

public static Object newInstance(Class componentType, int[] dimensions) 
throws IllegalArgumentException, NegativeArraySizeException;

This method creates a multi dimensional array with the given dimensions and component type.

Parameters:

componentType-the type of each element in the array.
dimensions-An array that specifies the dimensions of the array to be created.
IllegalArgumentException-If the dimensions has zero dimension itself or it has too many dimensions(upto 255 array dimensions are supported by java).
NullPointerException-If the componentType is null.
NegativeArraySizeException-if the size /length is negative

public static native void set(Object array, int index, Object value)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;

This method sets the object at the given index in the array to the specified value.If the array contains values of a primitive type the given value is automatically unwrapped before it is put into the array.

Parameters:

array-an array object
index-an index of the array
value- the new value

IllegalArgumentException-If the given object is not an array or if it represents an array of primitive values and the given value can not be unwrapped and converted to primitive type.

ArrayIndexOutOfBoundsException– if the given index is invalid.

public static native void setBoolean(Object array, int index, boolean z)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;

This method sets the element at the given index in the array to the given boolean value.

Parameters:

array-an array object
index-an index of the array
z- the new boolean value

IllegalArgumentException-If the given object is not an array or if the given value can not be converted to the component type of the array.

ArrayIndexOutOfBoundsException– if the given index is invalid.

public static native void setByte(Object array, int index, byte b)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;

This method sets the element at the given index in the array to the given byte value.

READ  Massive OCJP Cerification Sample Question Set

Parameters:

array-an array object
index-an index of the array
b- the new byte value

IllegalArgumentException-If the given object is not an array or if the given value can not be converted to the component type of the array.

ArrayIndexOutOfBoundsException– if the given index is invalid.

public static native void setChar(Object array, int index, char c) 
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;

This method sets the element at the given index in the array to the given character (chr) value.

Parameters:

array-an array object
index-an index of the array
c- the new character value

IllegalArgumentException-If the given object is not an array or if the given value can not be converted to the component type of the array.

ArrayIndexOutOfBoundsException– if the given index is invalid.

public static native void setDouble(Object array, int index, double d)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;

This method sets the element at the given index in the array to the given double value.

Parameters:

array-an array object
index-an index of the array
d- the new double value

IllegalArgumentException-If the given object is not an array or if the given value can not be converted to the component type of the array.

ArrayIndexOutOfBoundsException– if the given index is invalid.

public static native void setFloat(Object array, int index, float f)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;

This method sets the element at the given index in the array to the given float value.

Parameters:

array-an array object
index-an index of the array
f- the new float value

IllegalArgumentException-If the given object is not an array or if the given value can not be converted to the component type of the array.

ArrayIndexOutOfBoundsException– if the given index is invalid.

public static native void setInt(Object array, int index, int i) 
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;

This method sets the element at the given index in the array to the given int value.

Parameters:

array-an array object
index-an index of the array
i- the new int value

IllegalArgumentException-If the given object is not an array or if the given value can not be converted to the component type of the array.

ArrayIndexOutOfBoundsException– if the given index is invalid.

public static native void setLong(Object array, int index, long l)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;

This method sets the element at the given index in the array to the given long value.

Parameters:

array-an array object
index-an index of the array
l- the new long value

IllegalArgumentException-If the given object is not an array or if the given value can not be converted to the component type of the array.

ArrayIndexOutOfBoundsException– if the given index is invalid.

public static native void setShort(Object array, int index, short s)
throws IllegalArgumentException, ArrayIndexOutOfBoundsException;

This method sets the element at the given index in the array to the given short value.

Parameters:

array-an array object
index-an index of the array
s- the new short value

IllegalArgumentException-If the given object is not an array or if the given value can not be converted to the component type of the array.

ArrayIndexOutOfBoundsException– if the given index is invalid.

Inherited Methods

Apart from the above an Array also has the below written methods inherited from Object and Lang class-

  • clone()
  • hasCode()
  • notifyAll()
  • wait()
  • wait(long,int)
  • finalize()
  • equals(object)
  • getClass()
  • notify()
  • toString()
  • wait(long)

 

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *