Arrays In Java

An array is a container object that can hold a fixed number of values of a single type and the length of an array is established when the array is created. After creation, the length assigned to an array is fixed. An array can also be described as a collection of similar types of data. The advantage of using an array is that they simplify programming by replacing a lot of statements by just one or two statements. Arrays are created on dynamic memory by JVM.

Types Of Arrays

There are 3 types of arrays in java, they are as follows

  • One Dimensional Array
  • Two Dimensional Arrays
  • Three Dimensional Arrays

One Dimensional Array

A single or one dimensional array represents a row or a column of elements. For example, marks obtained by a student in 5 different subjects can be written in one dimensional array as they can be written in a row or as a column.

Creating a single dimensional array

  • We can declare a single dimensional array and directly store elements at the time of its declaration itself.
  • Another way of creating one dimensional array is by declaring the array first and allotting memory for it by using new operator.

Two Dimensional Arrays

A two-dimensional array consists of several rows and columns of data. For example, the marks obtained by a group of students can be represented by a 2D array. If we write the marks of three students it looks as follows

[[50, 55, 65, 70, 59],
 [65, 68, 73, 70, 69],
 [66, 68, 76, 63, 69]]

Creating a two-dimensional array

  • We can declare a two-dimensional array and directly store elements at the time of its declaration.
  • Another way of creating 2D array is by declaring the array first and then allotting memory for it by using new operator.

Three Dimensional Arrays

We can consider a three-dimensional array as a combination of several three-dimensional arrays. This is useful when we want to handle group of elements belonging to another group. For example, a college has 3 departments i.e. Electronics, Computers and Civil, and we want to represent the marks obtained by the students of each department in 3 different subjects, we can write these as follows

Electronics Department:

  • Student1 marks : 53, 54, 60
  • Student2 marks : 58, 64,63

Computers Department:

  • Student1 marks : 59, 57, 60
  • Student2 marks : 58, 67, 62

Civil Department:

  • Student1 marks : 63, 54, 69
  • Student2 marks : 58, 54, 53

Array Length

To know the length or size of the array, we can use the property length of an array. The arrayname.length returns an integer number which represents the size of an array. For example, take an array arr[] with size is 5 and there are three elements in it, as :

int arr[] = new int[5]; // size is 5
arr[0] = 20, arr[1] = 30, arr[2] = 40; // number of elements is 3

Command Line Arguments

Command line arguments represents the values passed to main() method. To catch and store these values, main() has a parameter, String args[] as follows

public static void main (String args[])

In the above program args[] is a one dimensional array of string type, so it can store a group of strings, passed to main() from the outside by the user. The user should pass the values from outside, at the time of running the program at command prompt. Command line represents the run command and the values given at the time of running the program. Command line arguments represents the values passed to main() method. If there are three command line arguments, then JVM allots memory for those 3 values only. If there are no command line arguments, then JVM will not allot any memory. So the size of ‘args’ array is same as the number of arguments passed to it.

Subscribe For More Content