Arrays in Java

Arrays in java are objects that hold fixed numbers of values of single type. Length of the array must be given at initalization , arrays cannot grow or shrink. Each element has an index value starting from 0 , so if you want to reach element 3 , index is 2..

Simple array initializations :

// array of integer with the capacity of 2 integers
int[] intArray = new int[2];

or ,

// [] goes to intArray which makes intArray[]
int intArray[] = new int[2];

Element indexes start from 0(zero) , so the last element index of array above is 1,

intArray[0] = 1;
intArray[1] = 2;

there is no intArray[2] because there is no third element. Capacity is 2.

System.out.println("the length of

gives : the length of intArray=2

You can initialize our sample intArray easily like that :

int intArray[] = new int[]{1,2};

The length of the array will be the same with number of elements you specified.

You can also use Array classes' fill() method like this


which fills the array with the specified value.

Arrays can hold not only primitive types as stated above, but also objects.

Arrays can also be multidimensional :

int[][] twoDimArray = { {1,2,3}, {4,5,6}, {7,8,9} };

or with String objects

String[][] friends = {{"Mr. ", "Mrs. ", "Ms. "},
{"Brown", "Parker"}};
//3x2 Array of Strings
friends[0][0] + friends[1][0]); //Mr. Brown
friends[0][2] + friends[1][1]); //Ms. Parker

Note that if you don't put elements in an array of objects all elements are null.

String[] strArray = new String[2];
strArray[0] = "first string";
if ( strArray[1] == null )
System.out.println("Second string is null");

gives the output Second string is null when launched

To copy an array into another use
System.arraycopy() method.

String[] strArray1 = new String[]{"banana","apple"};
String[] strArray2 = new String[2];

//copy array strtArray1 into strArray2
//starting from element 0 in strArray1 and start copy

//to strArray2[0] with 2 elements
System.arraycopy(strArray1, 0 , strArray2, 0, 2);

gives the output
[banana, apple]

You can also sort the array above using Arrays.sort() method


gives the output
[apple, banana]

Note that elements of the array should be either primitive or implement Comperable interface to use Arrays.sort()

You can assign one element to another. But note that primitive types and immutable objects are assigned to value , while mutable objects are assigned to reference (just in the case of method call). Confused ? Look at the following sample code. int and String gets the only value of other element but mutable java.util.Date objects gets the reference of the other element and completely becomes the other element. (be to spoon and bend yourself) :

int[] intArray = new int[]{1,2}; //array of primitive
intArray[0] = intArray[1];
intArray[1] = 1000;
System.out.println("intArray[0]="+intArray[0]); //prints 2 not 1000

String[] strArray = new String[]{"string1","string2"};//array of immutable objects
strArray[0] = strArray[1];
strArray[1] = "Mr. Hide";
//print string2 no mrhide!

java.util.Date[] dateArray = new
java.util.Date[2];//array of mutable objects
long tenMinutesAgo = System.currentTimeMillis() - 10 * 60 * 1000 ;
dateArray[0] = new Date();
dateArray[1] = new Date(tenMinutesAgo);
//prints the current date
dateArray[0] = dateArray[1];
//prints 10 minutes ago
long twelfMinutesAgo = tenMinutesAgo - 10 * 60 * 1000 ;
//prints 20 minutes ago date1 equals date2


Popular posts from this blog

Get Remote IP Address In Axis SOAP Server

Escape HTML and XML in Java

Read and Write Files in Java