Java Arrays: Single and Multi-Dimensional Array

07 Feb 2023
Beginner
476 Views

Java Arrays: Single and Multi-Dimensional Array

Introduction

If you're just getting started with programming, learning about Java Arrays can be a great place to start. They provide a simple way for programmers to store and manipulate multiple values in an organized fashion. With some practice and knowledge of the fundamentals, you'll soon have command over this powerful tool of Java programming - effectively managing large amounts of data and performing complex operations like sorting and searching. So let's get started!

What is an Array?

An array is a type of data structure that is used to store the elements of a particular type. For example, an integer array will hold only the value of any integer, but a character array will hold the characters only. The structure of an array is linear and holds similar elements in "contiguous memory addresses".

Pros and Cons of Using Array

Advantages of using Java arrays

  • Arrays help to increase code optimization
  • It helps in ordering and indexing data structure to store a particular type of element
  • It can also store variables and objects of class
  • Every element of an array is held in a definite memory location

Disadvantages of using Java arrays

  • An array can only hold the same type of elements
  • Removing and adding elements in very tough

Types of Array in Java

There are two types of arrays in Java:

  1. Single-dimensional array in Java
  2. Multi-dimensional array in Java

Define Single-Dimensional Array in Java

Single-Dimensional Array in Java is basically a linear array that allows its user to store 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. In memory, a one or single-dimensional array holds the data in a linear list. The index of the memory runs from the array size of 0 to -1. Single-Dimensional Arrays are initialized just like regular variables, but instead of assigning a single value the programmers can pass several values separated by commas or define an array with static size. Single-Dimensional Arrays are one of the handy tools contained within Java. They are useful for organizing data quickly and efficiently as well as giving us access to stored data using looping structures.

Define Multi-Dimensional Array in Java

Multi-Dimensional Array in Java is basically an array of arrays, which means that 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. Multi-Dimensional Arrays in Java can hold any type of item, i.e. two-dimensional arrays can hold int values or objects such as strings and objects. Multi-Dimensional Arrays also have several methods available to help search and arrange the data within the array, making them very flexible and efficient when dealing with complex tasks.

How to Declare and Initialize an Array in Java?

The syntax for one-dimensional array in Java:

arrayName = new DataTye[length 1][length 2]....[length N];(OR)

DataType[1st Dimension][2nd Dimension]....[Nth Dimension] arrayName = new DataTye[length 1][length 2]....[length N];

Example

//declaration
int iArray1[] = new int[4];

//initialization
iArray1[0] = 32;
iArray1[1] = 13;
iArray1[2] = 56;
iArray1[3] = 78;

The syntax for multi-dimensional array in Java:

Datatype [][]Array_Name = new Datatype[row_size][col_size(optional)]; (OR)

Datatype Array_Name[] = [{Value separated by comma},{Value separated by comma},....}];

Example

//declaration
int matrix[] = new int[2][2];

//initialization
matrix[0][0] = 1;
matrix[0][1] = 2;
matrix[1][0] = 3;
matrix[1][1] = 4;

Accessing Elements of Array in Java

There are three types of Accessing Element:

  1. Direct access to an element
  2. Accessing elements using a loop
  3. Accessing elements using for-each loop

Direct access to an element

This access method gives the user the authority to access the exact element directly of an array by using an index number.

Syntax

Array_Name[index]

Example

//directly accessing the second element of the array using the index number
iArray[1];

Accessing elements using for loop

In this access method, any particular array can be accessed by using iteration statements such as, for loop

Example

class MainDemo {

    public static void main(String[] args) {

      // create an array
      int[] numbers = {1, 4, 5, 8, 7};

      // using for loop go through the array
      for(int i = 0; i < numbers.length; i++) {

        System.out.println(age[i]);

      }
    }
  }

Accessing elements using for-each loop

In this method, the for-each loop can also be used for accessing the elements of the array

Example

class MainDemo {

    public static void main(String[] args) {

      // create an array
      int[] numbers = {1, 2, 4, 6, 8};

      // using for-each loop to go through each element
      for(int i : numbers) {

        System.out.println(i);

      }
    }
  }

Assigning an Array Reference to Another Array

An array can be used as a reference type of variable and can be used to assign an already existing array.

Example

class MainDemo
{

    public static void main(String[] args)

    {
      // create an array

      int[] arr = {1, 2, 4, 6, 8};
      int[] arr1;
      arr1 = arr; //assigning arr array reference to arr1

      System.out.println(arr1[3]);

    }
   }

Output

Java -cp /tmp/ovXXlntsc7 MainDemo

6

Summary

This article gives an idea of the definition, pros, and cons of the array. Moreover, it also covers what is a single-dimensional array and what is a multi-dimensional array in java. It also includes the methods of declaration and initialization of an array.

Accept cookies & close this