Powered by Blogger.

Different types of Arrays in Java

>> Sunday, July 27, 2014

What is  an Array: Array is a data structure which can store collection of elements with same data type.

Why to use Array: In Real time, if you want to store different marks of a student, you no need to declare all the variables to store the marks like, marks1,marks2,marks3,..e.t.c based on their subjects count. But Instead of declaring n variables simply you can declaring an array to store all these values of same datatype.
Types of Arrays in Java_JavabynataraJ
Array Declaration, Instantiation and Initialization: In java arrays are differentiated in two ways. One dimentional array and two dimentional array or multidimentional array. Now we can take a look of declaring one dimentional arry below.
int[] marks; //preferred way
or
int marks[];
Instantiation of an array is providing the size of an array to insert values into it. Based on the array size the number of elements can be inserted.The index of an array starts with 0 ends with n-1. We can declared with different datatypes.Once the size is instantiated then it can't be changed.
marks = new int[6];        //Creating an array with size 6
here index of marks array starts from 0 to 5.Initialization of an array is to assigning the values of same datatype to an array.
int[] marks = {94,96,85,89,96,82,91};
or
int marks[] = {94,96,85,89,96,82,91};
or
int[] marks = new int[6];

marks = {94,96,85,89,96,82,91};
Possible Exceptions: Most common exception while working with Arrays is ArrayIndexOutOfBoundsException.If you access an array with a negative index, or with a number greater than or equal to its size of an array.

Read Array values dynamically using BufferedReader
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int[] marks = new int[6];
System.out.println("Enter the Students marks:");
// Read the marks dynamically
for(int i = 0;i<marks.length;i++){
 String str = br.readLine();
 marks[i]=Integer.parseInt(str);
}
Iterate or display the values of an array we have two methods.
  1. Normal for loop 
  2. for-each loop or enhanced for loop
Using Normal for loop iteration:
int[] marks = {90,80,95,85,70,75};
// Write the students marks
System.out.println("Display Students marks:");
for(int i = 0;i<marks.length;i++){
 System.out.println(marks[i]+" ");
}
Using for-each(Enhanced for) loop:
int[] marks = {90,80,95,85,70,75};
//array iteration using for-each loop
for(int i: marks){
 System.out.println(i);
}
Output:
90
80
95
85
70
75
Two dimentional or Multi dimentional array: Multidimentional array contains arrays inside an array.This looks like matrix(table) with rows and columns. To declare this we have to specify size of the row and column. Instantiate the array with 2rows and 3 columns. Using the below logic we can do matrix addition and multiplication too.
int[][] matrix = new int[2][3];
Initialize with values of arrays and iterate
//multi dimentional array
int[][] matrix = {{2,3,4},{5,6,7}};
for(int row=0;row<matrix.length;row++){
 for(int col=0;col<matrix[row].length;col++){
  System.out.print(matrix[row][col]+" ");
 }
 System.out.println();
}
Output:
2 3 4 
5 6 7 
Can i declare an array of different data types?
Yes. You can declare an array with different data types using Object Class. Because objects in java extends to Object class. Checkout the below example.
Object[] obj = new Object[]{10,20,"A","Hi"};
for(Object o: obj){
 System.out.println(o+" is a "+o.getClass());
}
Output:
10 is a class java.lang.Integer
20 is a class java.lang.Integer
A is a class java.lang.String
Hi is a class java.lang.String

Reference Books:

Related Posts Plugin for WordPress, Blogger...
© javabynataraj.blogspot.com from 2009 - 2014. All rights reserved.