Arrays in Java - H Y R Tutorials

Arrays in Java

Share This

Arrays in Java

Arrays are a fundamental data structure in programming, and they are used to store and manipulate collections of elements of the same type that is stored in contiguous memory locations.

In Java, arrays are objects and are created using the "new" keyword. 

There are two types of arrays in Java:

  • Single-Dimensional Arrays
  • Multi-Dimensional Arrays

Single-Dimensional Arrays

A single-dimensional array, also known as a one-dimensional array, is a collection of elements of the same type that are arranged in a single row. Each element in the array is accessed using an index that starts at 0 and ends at the length of the array minus one. To create a single-dimensional array in Java, you can use the following syntax:

datatype[] arrayname = new datatype[length];

Here, "datatype" is the data type of the elements in the array, "arrayname" is the name of the array, and "length" is the number of elements in the array. For example, the following code creates a single-dimensional array of integers with five elements:

int[] numbers = new int[5];

Multi-Dimensional Arrays

A multi-dimensional array is an array of arrays. Each element in a multi-dimensional array is itself an array. Multi-dimensional arrays can be two-dimensional, three-dimensional, or even higher-dimensional. To create a multi-dimensional array in Java, you can use the following syntax:

datatype[][] arrayname = new datatype[length1][length2];

Here, "datatype" is the data type of the elements in the array, "arrayname" is the name of the array, "length1" is the number of rows in the array, and "length2" is the number of columns in each row. For example, the following code creates a two-dimensional array of integers with two rows and three columns:

int[][] seats = new int[2][3];

Arrays in Java are powerful and versatile data structures that are widely used in programming. Whether you are working with single-dimensional arrays or multi-dimensional arrays, understanding how to create, access, and manipulate arrays is essential for any Java programmer.


If you want to learn more about this topic, I highly recommend checking out this insightful video(Single-Dimensional arrays) and video(Multi-Dimensional arrays) on YouTube. It provides a comprehensive overview and covers everything you need to know.