What are Arrays in Java?

In this tutorial, we answer the question i.e. what are arrays in Java? We will understand its concept, and use and learn how to implement it in a Java program.

What are arrays in Java?

An array in Java is a group of elements that are of the same data type. It has a name and each element of an array can be accessed by using its index value. The index value of an array begins with 0.

what are arrays in java

Syntax

<Data Type> <Variable Name>[];

Example

int numbers[];

What to know about arrays?

  • For an array, the memory is always allocated in a contiguous manner.
  • To create an array, we have to specify the size of the array.
  • Arrays can store both primitive and non-primitive types of data. But an array can store elements of the same data type.

Consider an array,
int num[] = new int[5];
The above array is of the int data type. The elements of the int data type can only be stored in the num array. Any other element of another data type will cause an error or runtime error if stored in the array.

How to create an array?

Well, there are different ways to create an array in Java. The most common type of array creation is:

Creating an array by directly assigning values

Arrays can be created in compile-time by declaring them and specifying their size.
Example:
String numbers [] = {5,3,6,3,6,2,2,1,0,9};
// This will create an array of size 10 and store the given numbers in each index position of the array in the given order.

Creating an array by specifying its size

Example:
String alphabets [] = new String [26];
//This creates an array named ‘alphabets’ which can hold 26 elements of String data type.

Creating arrays at runtime

The string can be created by specifying a size during the runtime of a program.
Example:
Scanner sc = new Scanner(System.in);
int size = sc.nextInt();
String names [] = new String [size];
// The user will be prompted for entering an integer value which will then be used to create an array of that size.

How to access array elements in Java?

An element in an array can be accessed using the index value of that element. The first element can be accessed using index 0 and the second element can be accessed using index 1 and so on.

If we try to access an index of an array that is either negative or does not exist, then the program will raise an exception i.e. IndexOutOfBoundException. So be careful to mention the correct index values in the programs.

Example:

class prog {
    public static void main(String agmts[]) {
        String[] whatwedo= {"We", "Make", "Content", "For", "You"};

        //Printing the value at index 2
        System.out.println("String at index 2 is: " + whatwedo[2]);

        //Changing the value at index 2
        whatwedo[2] = "UseMyNotes";

        //Printing the new value at index 2
        System.out.println("String at index 2 is: " + whatwedo[2]);
    }
}

/* Output
Value at index 2 is: Content
Value at index 2 is: UseMyNotes
*/

Types of Arrays in Java

Generally, there are just two types of arrays in Java, they are:

  1. One-dimensional arrays
  2. Two-dimensional arrays

One-dimensional Array

An array that is a linear type array is a one-dimensional array. It just takes one index value to access that element.

one-dimensional array

Syntax:

<Data type> <Variable Name> [];

Example:

String welcome[] = {“Hey”, “This”, “Is”, “Use”, “My”, “Notes”};

Multi-dimensional Array

An array that requires two or more indices to access an element is known as a multi-dimensional array.

multi-dimensional array

Syntax:

<Data Type> <Variable Name> [size 1] [size 2]….[size N];

Example:

String table [3][3] = {{1,2,3}, {4,5,6}, {7,8,9}};

In the above example, we see that each index of an array has 3 more elements. So, if we access table [1][2] we will get element 6. We are using two index values to access an element of the above array.