Java Arrays

October 21, 2018 Off By ravi24kant

Introduction to Java Arrays

Imagine you’re storing some information about 200 employees. To store information of so many employees separately, you will need 200 variables. But no human can create and manage so many variables. You have to think 200 different names. And this will make your program even bigger. Arrays are used to avoid this problem.
An array is a collection of values of similar data type. Array lets you create a similar group of information. As you can arrange the salary of employees, you can store all the employees’ salary.

You can create an array of any type. Such as int, float, char etc. The type of variable you create can be put into the same type of values. When you create an array, its size also defines. The size of the array can be stored in the same values. Array stores all the values by indexing. Every index represents a different variable.

array_name[0]; // first variable
array_name[1];// second variable
..
..
array-name[n-1]; // last variable

Index of arrays starts with zero, so if your array’s size is n then store at last element (n -1) position.
Elements -> 4  6  9  10  22
Index       -> 0  1   2    3  4

Here the size of the array is 5 but the last element of the array is being stored at 22 4th position because the array starts with an index zero.

Creating Java Arrays

Creating arrays in Java is very easy. This is like creating an object of a class. An example of this is being given below.

type arrayName = new type[arraySize];

type              -> This type is the type of array you want to create.
arrayName -> This is the name of the array. They should be unique.
new              -> The new keyword has been used to allocate memory to Array.
arraySize    -> This is the size of the array.

Let’s define it with any integer. Or shows how many elements you store in the array.

int salary = new int[10];

Initializing Arrays

You can initialize arrays in 2 ways. In the first method, you can initialize an index of java just like the variable. An example of this is being given below.

salary[0] = 25000;
salary[1] = 10000;
..
..
salary[9] = 30000;

Users can also input values

Scanner s = new Scanner(System.in);
salary[0] = s.nextInt();

This method is very time consuming. In the second way, you use a loop and input all the values together.

Scanner s = new Scanner(System.in);

for(int i=0;i<10;i++)
{
salary[i] = s.nextInt();
}

Displaying Arrays

You can print values of arrays in 2 ways. You can also print the value of single index and print the entire array as well.

System.out.println(salary[0]); // prints value of first index

for(int i =0; i<10; i++)
{
System.out.println(salary[i]); // prints all array
}

Multidimensional Java Arrays

So far, the example you saw in the array is all one dimensional array. Arrays can have multiple dimensions. Now will talk about 2 dimensional arrays. You create row and column size to create two dimensional arrays. This is like creating a table. In it first square brackets represent rows and columns of other brackets.

Creating Two Dimensional Array

int TwoDiem [][] = new int [3][4];

Initializing Two Dimensional Arrays

Scanner s = new Scanner(System.in);

System.out.println(“Enter the array elements”);
for(int i=0; i<3;i++)
{
for(int j=0;j<4;j++)
{
TwoDiem[][] = s.nextInt();
}
}

Displaying Two Dimensional Arrays

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