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 :
Array Creation : We can create an array in two ways, using the new keyword and with the initialization.
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];
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};
Array Initialization : Assigning values to an array by index.
1 2 3 4 5 6 7 8 |
int[] int_array = new int[6]; int_array[0] = 20; int_array[1] = 15; int_array[2] = 30; int_array[3] = 40; int_array[4] = 10; int_array[5] = 35; /* int[] int_array = {20,15,30,40,10,35}; */ |
Array Accessing : Retrieving/getting values from the array by index.
1 2 3 4 |
for(int index = 0 ;index<int_array.length;index++ ) { System.out.println("The element at ["+index+"] index: "+ int_array[index]); } |
Example to Creating ,Initializing and Accessing an Array in Java :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
/* *Author : java2db.com */ public class Array_Example{ public static void main(String[] args) { //Creation and initializing an array int[] int_array = new int[6]; int_array[0] = 20; int_array[1] = 15; int_array[2] = 30; int_array[3] = 40; int_array[4] = 10; int_array[5] = 35; /*//Also declare like below int[] int_array = {20,15,30,40,10,35};*/ //Accessing an array for(int index = 0 ;index<int_array.length;index++ ) { System.out.println("The element at ["+index+"] index: "+ int_array[index]); } /*Also we can loop like below * for(int value : int_array) { System.out.println(value); }*/ } } //Output The element at [0] index: 20 The element at [1] index: 15 The element at [2] index: 30 The element at [3] index: 40 The element at [4] index: 10 The element at [5] index: 35 |
Copying 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
/* *Author : java2db.com */ public class copyArrayExe { public static void main(String[] args) { String fruit_names[] = {"mango","banana","grape","apple"}; String copy_fruit_name[] = new String[fruit_names.length]; //Method to copy an array System.arraycopy(fruit_names, 0, copy_fruit_name, 0, fruit_names.length); for(String name : copy_fruit_name) { System.out.println(name); } }//end main }//end class //Output mango banana grape apple |
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 |