Java Arrays


An array is a group of typed variables denoted by a common name. Any type of array can be created and has one or more dimensions. An array is accessed by the index of a particular element. Provides a convenient way to group information about arrays.


One-Dimensional(1D) Arrays

A one-dimensional array is basically a list of typed variables. To create an array, you first need to create the desired type of array variable. A simple form of one-dimensional array declaration -

type var_name[];

Here, type declares the element type of the array. Component Type Determines the data type of each component of the array. So the element type for the array determines what kind of data the array will contain. For example, the following declares an array named week_days with the type "array of int":

int week_days[];

The size of the number of elements inside an array is given by the integer number in "[ ]".

String arr[]=new String[10];

Here are some ways in which we can insert values in an array:

String arr[]={"Sunday","Monday","Tuesday"};

In the Array initialization above, a string array is taken and three values are initialized directly and the values are separated from each other with a comma (,).

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

In the Array initialization above, an integer array is taken and five values are initialized directly and the values are separated from each other with a comma (,).

Taking input from the user

If you want to take input from the user for this array, you have to use any loop, it can be while, do ... while, for loop. The following code shows the input from the user in a string array.

Note : Input can be taken from the user with any loop except for each loop.

Program
import java.util.Scanner;
public class Main
{
	public static void main(String[] args) 
	{
	    Scanner sc=new Scanner(System.in);
	    System.out.println("How many element do you want?");
	    int element=sc.nextInt();
	    String arr[]=new String[element];
	    System.out.println("Enter the element");
	    for(int i=0;i<element;i++)
	    {
	        arr[i]=sc.next();
	    }
	    System.out.println("Your first positioned element are "+arr[0]);
	}
}
Run

Accessing the elements of the array

The index of the array is the first element '0' position then the second element '1' is inserted consecutively.

If single value is to be taken from this array then array index can be used. And if you want to take multiple values from the array, you have to use any loop.

Program
int num[]={1,2,3,4,5};
//only second position data shows
System.out.println("The second position data = "+num[1]); //output 2
   
//shows all values
System.out.println("All values shows");
for(int i=0;i<5;i++)
{
   System.out.println(num[i]);
}
Run

Changing an array element

If you want to change the value of any one of these elements, you need its index number.

Program
int num[]={1,2,3,4,5};
System.out.println("First position Unchanged value = "+num[0]);
//changing value
num[0]=7;
System.out.println("First position Changed value = "+num[0]);
Run

Array Length

To find out how many values are in an array, use the length property.

Program
int num[]={1,2,3,4,5};
System.out.println("Elements = "+num.length);
Run

Multidimensional Arrays(2D)

In Java, multidimensional arrays are applied as multiple arrays within an array. To declare a multidimensional array variable you must specify each additional indicator using another set of square brackets. For example, the following declares a two-dimentional array variable called 2D.

int arr[][]=new int[10][10];

To get the elements of a two-dimensional array, we used another For loop inside a For loop.

Program
public class Main
{
	public static void main(String[] args) 
	{
	    int arr[][]={{1,2,3},{4,5,6}};
	    for(int i=0;i<arr.length;i++)
	    {
	        for(int j=0;j<arr[i].length;j++)
	        {
	            System.out.println(arr[i][j]);
	        }
	    }
	}
}
Run