Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Thursday 23 March 2017

Java Array

 Array

Array is a collection of similar types of elements or data that have contiguous memory location.

Array is an object in java. In java, array is represented via an object. Java array is a data structure where we store similar data types. Java array is fixed in size i. e you cannot increase the size of array at runtime. You can store only fixed set of elements in a java array.

Java array is indexed based, first elements of the array is stored at 0 index.
Array indexe


Advantage of Java Array

  • Code Optimization : There is no need to declare a lot of variable of same data types. We can retrieve and sort data easily.
  • Random Access : In an array, we can get any data located at any index position.
  • Performance : The performance of an array is good because you can search any element in an array on the basis of index.


Disadvantage of Java Array

  • Size Limit : Once an array has got a memory then that memory can't increase of decrease at runtime.
To solve this problem there is ''collection'' framework in java. Collection framework is an alternate of java array.


Types of Array

There are two types of array in java.

  1. Single Dimensional Array (e.g dataType[] var;)
  2. Multi Dimensional Array (e.g dataType[][] var;)

Single Dimensional Array in Java

Array Declaration in Java

Single dimensional array declaration.
dataType[] var;
        or
dataType []var;
        or
dataType var[];


Instantiation of an Array in Java

Array in java is an object, Hence we can create object by new keyword.

dataType var[] = new dataType[size];//instantiation and size of array 


Example of Single Dimensional Array in Java

In the below example, we are going to declare, instantiate, initialize and traverse an array.

For example:

class Numbers
{
public static void main(String args[])
{
int a[] = new int[5];//declaration and instantiation 

a[0] = 1;//initialization
a[1] = 2;
a[2] = 3;
a[3] = 4;
a[4] = 5;

//print array

for(int i = 0; i<a.length; i++)//length is the property of array
{
System.out.println(a[i]);
}
}
}

output :1
              2
              3
              4
              5


Java Array Declaration, Instantiation, and Initialization

You can declare, instantiate and initialization an array in one step e.g

int a[] = {10,20,30,40,50};//declaration, instantiation, initialization 

For example

class Numbers
{
public static void main(String args[])
{
int number[] = {99,100,80,40};//declaration,instantiation,initialization
for(int i = 0; i<number.length; i++)
{
System.out.println(number[i]);
}
}
}

output: 99
             100
             80 
             40


Accessing Array Elements in Java

You can access the elements of array by using the index value of the element.

For example:

class Test
{
public static void main(String args[])
{
int a[] = {2,4,6,8,1};
System.out.println(a[3]);
}
}

output: 8


Multidimensional Array in Java

In the multidimensional array, data is stored in row and column based index(also known as matrix form).


Multidimensional Array Declaration in Java

dataType[][] var;
          or
dataType [][]var;
          or
dataType var[][];
          or 
dataTytpe []var[];


Instantiation of Multidimensional Array in Java

int var[][] = new int[3][3];//3 rows and 3 column 

Initialization of Multidimensional Array in Java

var[0][0] = 1;
var[0][1] = 2;
var[0][2] = 3;
var[1][0] = 4;
var[1][1] = 5;
var[1][2] = 6;
var[2][0] = 7;
var[2][1] = 8;
var[2][2] = 9;


Example of Multidimensional Array in Java

In the below example, we are going to declare, instantiate, initialize and print multidimensional array in java.

For example:

class Numbers
{
public static void main(String args[])
{
int a[][] = { {1,2,3}, {4,5,6},{7,8,9}};//dec..insta..initiz

//print 2d array

for(int i =0; i<3; i++)
{
for(int j =0; j<3; j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}


output: 1 2 3
             4 5 6
             7 8 9


How to find class name of Java Array

Array is an object in java. For array object , proxy class is created whose name can be obtained by getClass() and getName() methods on the object.

For example:

class ArrayName
{
public static void main(String args[])
{
int arr[] = {1,2,3};
class c = arr.getClass();
String s = c.getName();
System.out.println(s);
}
Share:

4 comments:

  1. Beutiful blog for java beginners

    ReplyDelete
  2. for(int i =0; i<3; i++)
    {
    for(int j =0; j<3; j++)
    {
    System.out.print(a[i][j]+" ");
    }
    System.out.println();
    }

    Helllo This Massiv doesnt print
    output: 1 2 3
    4 5 6
    7 8 9
    It is not right

    ReplyDelete
    Replies
    1. There is no error in this code please execute code again.

      Delete
  3. Thanks for sharing this informative blog java training in OMR

    ReplyDelete

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate