java2db

How to Declare , Initialize , Access and Copy an Array in Java



An array is a container object that holds a homogeneous and fixed number of elements.The length of an array is fixed that what we given the length at the time of array declaration.The array items are called elements ,the array index always starts with 0(Zero). Each element can accessed by its numerical index.

Creating ,Initializing and Accessing an Array in Java :

How to Declare , Initialize , Access and copy an array in javaArray Creation : We can create an array in two ways, using the new keyword and  with the initialization.

How to Declare , Initialize , Access and copy an array in java Array creation (one dimensional array) using the new keyword :

Syntax:

<Array_Type>[] <Array_Name> = new <Array_Type>[<Length>];

<Array_Type> <Array_Name>[] = new <Array_Type>[<Length>];

Example :

int[]  int_array = new int[6];

 
How to Declare , Initialize , Access and copy an array in java Initializing an array at the time of it’s creation :

Syntax:

<Array_Type>[] <Array_Name> = {<Element1>,<Element1>,<Element1>,….};
Example :

int[] int_array = {20,15,30,40,10,35};

 

 

How to Declare , Initialize , Access and copy an array in javaArray Initialization : Assigning values to an array by index.

 

How to Declare , Initialize , Access and copy an array in javaArray Accessing : Retrieving/getting values from the array by index.

How to Declare , Initialize , Access and copy an array in javaExample to Creating ,Initializing and Accessing an Array in Java :

 

How to Declare , Initialize , Access and copy an array in javaCopying an array to another array : To copy an array , use the below method from java.lang.System  class.

public static void arraycopy(Object srcArray,int srcPos,Object destArray,int destPos,int length)

srcArray– the source array.
srcPos – starting position in the source array.
destArray– the destination array.
destPos – starting position in the destination data.
length – the number of array elements to be copied.

 How to Declare , Initialize , Access and copy an array in java If the value assignment to an array is greater than it’s length , it throws ArrayIndexOutOf BoundsException.




Related Posts :
What is LDAP or Lightweight Directory Access Protocol
   [will not be published]



^ <