Mark As Completed Discussion

Arrays

Arrays

Arrays in Java are the most primitive way of aggregating elements of the same datatypes.

So how does an array work?

TEXT/X-JAVA
1String [] lang = {"Java", "C++", "Python"};

Array types are signalized using the brackets [] and created using the curly brackets. Looking at the snippet, you can see that declaring and assigning values to an array in java is different from the way we do with single value variables.

  1. The data type of the array, String is declared followed by square brackets, [], which is the symbol for arrays in java that is used to represent an index in an array.

  2. This is followed by the name of the array,lang.

  3. The assignment operator, =.

  4. And finally the values to be stored in the array which is surrounded by a set of curly braces, {"Java", "C++", "Python"};

We use curly braces to write out a series of related values to be stored in an array in java. However, curly braces are not the only way to store values in an array.

TEXT/X-JAVA
1String [] lang = new String[5];
2
3lang[0] = "Java";
4
5lang[1] = "C++";
6
7lang[2] = "Python";

Notice how on the right hand side, we used the keyword new. This keyword is used to create objects in java.

More about arrays

  • Arrays are very useful when you have a large number of variables and you have to initialize them.

  • Arrays index starting from 0 which means that if there n numbers present in the array then the index of the last element of the array is n-1.

  • The size of the array must be initialized by an int value.

Single Dimensional(1-D array)

A Single Dimensional array is a type of linear array in which elements are stored in a continuous row.

Single dimensional arrays have two components: data type of the array(int, char, float, etc) and name(int [] arr or int arr [].

The declaration establishes the variable arr is an array variable but no array actually exists. It simply tells the compiler that the arr variable will hold an array of type integer. To link arr to an actual physical array of integers, we would have to instantiate the array.

TEXT/X-JAVA
1//Creating and Initializing an array of size 5
2
3int [] arr; // array declaration
4
5arr = new int [5]; //allocating memory to array
6
7int [] arr = new int [5]; //declaring and instantiating array at the same time
8
9
10int [] arr2 = new int []{1,2,3,4,5}; //initializing array

The elements in the array allocated by the keyword new will automatically be initialized to zero for number times, null for referential types, and false for boolean types.

Accessing a Specific Element in an Array

As mentioned before, we can access the specific element by its index within square brackets. Let's say we want to print the month May from the example below.

TEXT/X-JAVA
1public class Example {
2    public static void main(String[] args) {
3        String[] months = new String[] {
4            "January",
5            "February",
6            "March",
7            "April",
8            "May",
9            "June",
10            "July",
11            "August",
12            "September",
13            "October",
14            "November",
15            "December"
16        };
17
18        System.out.println(months[4]);
19    }
20}

Since May is at the 4th index, then having months[4] inside the print, statement would output May.

But what if we wanted to access all the elements of the array? Well, we could use a for loop to traverse all the elements in the array.

TEXT/X-JAVA
1for( int i = 0; i<months.length; i++){
2
3  System.out.println(" Month in " + i + "index is " + months[i]);
4}

The for loop starts at 0 and ends at months.length-1 since we use i<months.length, which shows that we want to stop at one less than the actual length of the array. If we write i ≤ months.length, then the JVM(Java Virtual Machine) would throw an ArrayOutOfBoundException, which shows that the index of an array is negative or greater than or equal to the length of an array which is known as the illegal index of an array.

Multidimensional or 2-D array is an array where elements are stored in rows and columns.