Arrays are nicely organized lists of data. Array is a collection of the homogeneous data types.

Think of a numbered list that starts at zero and extends one line every time you add something to the list. The arrays are useful for any number of situations because they’re treated as a single hunk of data. The array is one of the most important part in a any of the programming language.

For instance, if you wanted to store a bunch of high scores for that you want to do that with an array.

Initially, you might want to have a list of 10 items and You could in theory use the following code to store each score.

int score1;
int score2;
int score3;
int score4;
int score5;
int score6;
int score7;
int score8;
int score9;
int score10;

To make matters worse, if you needed to process each value, you’d need to create a set of code that deals with each variable by name.

To check if the score2 is higher than the score1 you’d need to write a function. Specifically to check those two variables before switching them. Thank goodness for arrays.

There are two types of array:

  • Single Dimensional Array
  • Multidimensional Array

Single Dimensional Array in java:

Syntax to Declare an Array in java:

  • dataType[] arr; (or)
  • dataType []arr; (or)
  • dataType arr[];

Instantiation of an Array in java:

  • arrayRefVar = new datatype[size];

Example of single dimensional java array:
Let’s see the simple example of java array and where we are going to declare, instantiate, initialize and traverse an array.

class Testarray{
public static void main(String args[]){
int a[]=new int[5];//declaration and instantiation
a[0]=10;//initialization
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;
//printing array
for(int i=0;i<a.length;i++)//length is the property System.out.println(a[i]);
}}


Output:

10
20
70
40
50

Multidimensional arrays in java:

In such case the data is stored in row and column based index or also known as matrix form.

Syntax to Declare Multidimensional Array in java:

  • dataType[][] arrayRefVar; (or)
  • dataType [][]arrayRefVar; (or)
  • dataType arrayRefVar[][]; (or)
  • dataType []arrayRefVar[];

Example to instantiate Multidimensional Array in java:

  • int[][] arr=new int[3] [3];//3 row and 3 column

Example to initialize Multidimensional Array in java

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

Example of Multidimensional java array:

Let’s see the simple example to declare, instantiate, initialize and print the
2 Dimensional array.

class Testarray3
{
public static void main(String args[])
{
//declaring and initializing 2D array
int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
//printing 2D array
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}

Output:

1 2 3
2 4 5
4 4 5

Arrays example

There are other articles are which gives you more information about the array. If you want to know about that article then click here.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to Top