Array Class in Java

0
41
Spread the love
  • 28
  • 24
  • 23
  • 25
  •  
  •  
  •  
  • 10
  •  
  •  
    110
    Shares

Array Class in Java
Array Class in Java
Array Class in Java

Array Class in Java:

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

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

The methods of Array class 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 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.

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()

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-

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 28
  • 24
  • 23
  • 25
  •  
  •  
  •  
  • 10
  •  
  •  
    110
    Shares

LEAVE A REPLY

Please enter your comment!
Please enter your name here