Array in Java

Array is a collection of similar types of multiple data. In java variable and object are of single space which means only one value can be stored at a time. If  we have to store more than one value than multiple variables have to assigned which is so boaring and consume more time to assign. So, to overcome the problem all most of programming language use array data structure. Array can store multiple data in a single variable. Each of the space that hold the value is known as element and these elements are arranged in next to one another in contiguous area of memory space. So, we can easily access the elements by assigning the numerical index to the main reference point. In java, the index starts from  0. So the the index of element is 0. example:-

int[] a={1,2,3,4,5};

here, a[0]=1; a[1]=2 and so on.

Declaration of array

we generally declare array variable to use same type of data. Secondly we can declare array object also. Java treat array as an object. When we declare an array object, the number if elements are fixed. The syntax can be of different type. according to our need. Example:-

int a[];  it means a variable is an array, only declaration of array.

int[] a, b; it means all the declared integer are array object.

int a[], b; it means variable a is an array  object and the variable b is only array declaration

you can use any datatypes to make an array variable and and array object.

we can copy and clone the elements of array object.

Multi-dimensional Array

Multi-dimensional array is created using more than two set of square brackets.
example:-

int a[][];
string st[][];

public class MultiDimensional {
public static void main(String[] args){
int a[][]=new int[3][3];
a[0][0]=1;
a[0][1]=2;
a[0][2]=3;
a[1][0]=4;
a[1][1]=5;
a[1][2]=6;
a[2][0]=7;
a[2][1]=8;
a[2][2]=9;

System.out.println(a[0][0]+"\t"+a[0][1]+"\t"+a[0][2]);
System.out.println(a[1][0]+"\t"+a[1][1]+"\t"+a[1][2]);
System.out.println(a[2][0]+"\t"+a[2][1]+"\t"+a[2][2]);
}

}

Copy from one array to another

public class Copy {
public static void main(String args[]){
int [] copyFrom={0,1,2,3,4,5,6,7,8,9};
int [] copyTo=new int[7];
System.arraycopy(copyFrom,2,copyTo,0,7);
int i;
System.out.println("content of copy to ;");
for(i=0;i<copyTo.length;i++){
System.out.println(copyTo[i]+" ");
}
}
}

Clone from one array to anoter

public class CloneArray {
public static void main(String[] args){
int[] a, b;
int i;
a=new int[5];
a[0]=9;
a[1]=3;
a[2]=2;
a[3]=8;
a[4]=7;
System.out.println("the length of array a is "+a.length);
System.out.println("the value of a[0] is "+a[0]);
System.out.println("the value of a[1] is "+a[1]);
System.out.println("the value of a[2] is "+a[2]);
System.out.println("the value of a[3] is "+a[3]);
System.out.println("the value of a[4] is "+a[4]);
b=a.clone();
System.out.println("the length of b is "+b.length);
System.out.println("the content of array is ");
for(i=0;i<5;i++){
System.out.println(b[i]);
}
}

}

Comments

Popular posts from this blog

Logical operator in JAVA

Class and Object in Java

Loop Structure