Java Arrays
An array is a type of object that contains values called elements. This gives you a convenient bag or holder for a group of values that can be moved around a program, and allows you to access and change values as you need them. Unlike variables which are accessed by a name, elements are accessed by numbers starting from zero.
Arrays in Java are similar in syntax to arrays in other languages such as C/C++ and Visual Basic.
Single Dimension Arrays
Declaration of array
The declaration of an array does not allocate any storage, it just announces the intention of creating an array.
Thus the following will cause a compile time error:
int num[5];The size of an array is given when it is actually created with the new operator thus.
int num[]; //declartion
num = new int[5]; //allocation
int num[]=new int[5]; //initilization
num[0]=900; Assignment
System.out.println(num[0]); //printing array elements
Initilization of arrays
int items[]={23,232,455,667};
Also the square brackets can be placed either after the data type or after the name of the array. Thus both of the following are legal
int[] num;
int num[];
You can read these as either:•An integer array named num
•An integer type in an array called num.
A big difference between Java and C/C++ is that Java arrays know how big they are, and the language provides protection from walking off the end of arrays.
int len=array_name.length;
Examples
Simple Single Dimension array example
public class ArrayDemo1
{
public static void main(String[] args)
{
int values[]; //declaration
values=new int[3];
values[0]=1001;
values[1]=1002;
values[2]=1003;
System.out.println(values[0]);
System.out.println(values[1]);
System.out.println(values[2]);
}
}
printing java Array elements using traditional for loop
public class ArrayDemo2
{
public static void main(String[] args)
{
int values[]={45,56,78,89,87,566,780,74,89,76};
//finding the array size
int size=values.length;
System.out.println("Array size is..."+size);
for(int i=0;i<size;i++)
{
System.out.print(values[i]+"\t");
}
}
}
Printing java array elements using for-each loop (Since 1.5)
public class ArrayDemo3
{
public static void main(String[] args)
{
int values[]={45,56,78,89,87,566,780,74,89,555};
for(int item:values)
{
System.out.println(item);
}
}
}
Multi Dimensional Arrays
An array declaration can have multiple sets of square brackets. Java does not formally support multi dimensional arrays, however it does support arrays of arrays, also known as nested arrays.
The important difference between multi dimensional arrays, as in C/C++ and nested arrays, is that each array does not have to be of the same length. If you think of an array as a matrix, the matrix does not have to be a rectangle.
Declaration of arrays
Allocation
items=new int[5][5]; //legal
items=new int[5][]; //legal
items[0]=new int[5]; // 0 th row with 5 columns
items[1]=new int[7]; // 1 st row with 7 columns
initilization of nested arrays
String subjects[][]={
{"asp","jsp","dsp"},
{"c","c++","java","smalltalk"},
{"bdps","aptech"}
};
Examples:
simple nested arrays example
public class NestedArrayDemo1
{
public static void main(String[] args)
{
int values[][]; //declaration
values=new int[2][2]; //allocation
values[0][0]=1001; values[0][1]=1002;
values[1][0]=2001; values[1][1]=2002;
System.out.println(values[0][0]+"\t"+values[0][1]);
System.out.println(values[1][0]+"\t"+values[1][1]);
}
}
printing nested array elements using traditional for loop
public class NestedArrayDemo2
{
public static void main(String[] args)
{
String subjects[][]={
{"c","c++","java","small talk"},
{"html","xml","wml"},
{"asp","jsp"}
};
int rows=subjects.length; //finding rows
for(int i=0;i<rows;i++)
{
int cols=subjects[i].length;
System.out.println("Columns for "+i+" row is..."+cols);
for(int j=0;j<cols;j++)
{
System.out.print(subjects[i][j]+"\t");
}
System.out.println();
}
}
}
printing nested array elements using for-each loop (Since 1.5)
public class NestedArrayDemo3
{
public static void main(String[] args)
{
String subjects[][]={
{"c","c++","java","small talk"},
{"html","xml","wml"},
{"asp","jsp"}
};
for(String rows[]:subjects)
{
for(String cols:rows)
{
System.out.print(cols+"\t");
}
System.out.println();
}
}
}