DEV Community

Mercy
Mercy

Posted on

Arrays In Java

Hey friendsđź‘‹, Today we are talking about arrays in Java. Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value. There are two types of arrays in Java, the one-dimensional array and the multi-dimensional array.

An array is an object so it stores reference to the data it stores. Arrays can store two types of data:

  • A collection of primitive data types
  • A collection of objects

An array of primitives stores a collection of values that constitute the primitive values themselves. An array of objects stores a collection of values, which are in fact heap-memory addresses or pointers.

The members of an array are defined in contiguous memory locations and hence offer improved access speed.

The following code creates an array of primitive data:

public class Main {
    public static void main(String[] args)
    {

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

        // size of array
        int n = arr.length;

        // traversing array
        for (int i = 0; i < n; i++)
            System.out.print(arr[i] + " ");
    }
}
Enter fullscreen mode Exit fullscreen mode

An array of int primitive data type and another of String objects

Image description

A one-dimensional array is an object that refers to a collection of scalar values. A two-dimensional array is referred to as a multidimensional array. A two-dimensional array refers to a collection of objects in which each of the objects is a one-dimensional array.

Image description

Single-Dimensional Array in Java

  • A 1D Array in Java is a linear array that allows the storage of multiple values of the same data type.
  • It's a collection of data that stores elements of the same type in a sequentially allocated space in memory.
  • Single-dimensional arrays can be utilized to store both simple and complex data types, anything from strings, integers, and booleans to custom-made classes depending on the user's requirements.

Multi-Dimensional Array in Java

  • Multi Dimensional Array in Java is an array of arrays, i.e. it is an array object that has multiple dimensions.
  • Multi-dimensional arrays are useful when dealing with a large amount of data since they give the ability to store and access data from a single variable but with multiple levels of hierarchy.
  • This multi-dimensional array can be expanded to a certain number of dimensions such as two dimensions, three dimensions, etc.

Image description

The number of bracket pairs indicates the depth of array nesting. Java doesn’t impose any theoretical limit on the level of array nesting. The square brackets can follow the array type or its name.

An array type can be any of the following:

  • Primitive data type
  • Interface
  • Abstract class
  • Concrete class

Combining array declaration, allocation, and initialization

int intArray[] = {0, 1};
String[] strArray = {"Summer", "Winter"};
int multiArray[][] = { {0, 1}, {3, 4, 5} };
Enter fullscreen mode Exit fullscreen mode

The above

  • Doesn’t use the keyword new to initialize an array
  • Doesn’t specify the size of the array
  • Uses a single pair of braces to define values for a one-dimensional array and multiple pairs of braces to define a multidimensional array

All the previous steps of array declaration, allocation, and initialization can be combined in the following way, as well:

int intArray2[] = new int[]{0, 1};
String[] strArray2 = new String[]{"Summer", "Winter"};
int multiArray2[][] = new int[][]{ {0, 1}, {3, 4, 5}};
Enter fullscreen mode Exit fullscreen mode

Unlike the first approach, the preceding code uses the keyword new to initialize an array. If you try to specify the size of an array with the preceding approach, the code won’t compile. Here are a few examples:

int intArray2[] = new int[2]{0, 1};
String[] strArray2 = new String[2]{"Summer", "Winter"};
int multiArray2[][] = new int[2][]{ {0, 1}, {3, 4, 5}};
Enter fullscreen mode Exit fullscreen mode

Key Points

  1. Multidimensional arrays in Java are implemented as arrays of arrays, not as true matrices.
  2. Array lengths can differ for each row (jagged arrays).
  3. You can access elements using indexes like arrayName[rowIndex][columnIndex].
  4. The length property provides the number of rows or the size of a specific row.

I will get into detail in the coming article tomorrow

Top comments (0)