Java Array

Java Array

  • An array is a collection of similar types of data having contiguous memory allocation.
  • The indexing of the array starts from 0., i.e 1st element will be stored at the 0th index, 2nd element at 1st index, 3rd at 2nd index, and so on.
  • The size of the array can not be increased at run time therefore we can store only a fixed size of elements in array.
  • Use Case: Storing marks of 5 students
Java Array

Accessing Array Elements : 

Array elements can be accessed as follows,

/* marks[0] = 100       //Note that index starts from 0
marks[1] = 70
.                                  
.
marks[4] = 98 */

So in a nut shell, this is how array works:

  1. int[] marks; //Declaration!
  2. marks = new int[5];           //Memory allocation!
  1. int[] marks = new int[5]; //Declaration + Memory allocation!
  2. int[] marks = {100,70,80,71,98} // Declare + Initialize!

Note : Array indices start from 0 and go till (n-1) where n is the size of the array.

Array length : 

Unlike C/C++, we don’t need to use the sizeof() operator to get the length of arrays in Java because arrays are objects in Java therefore we can use the length property.

marks.length //Gives 5 if marks is a reference to an array with 5 elements

Displaying an Array : 

An array can be displayed using a for loop:

for (int i=0; i<marks.length; i++)
{
            Sout(marks[i]);    //Array Traversal
}

Quick Quiz: Write a Java program to print the elements of an array in reverse order.

Example

package com.company;

public class cwh_26_arrays {
    public static void main(String[] args) {
        /* Classroom of 500 students - You have to store marks of these 500 students
        You have 2 options:
        1. Create 500 variables
        2. Use Arrays (recommended)
        */
        // There are three main ways to create an array in Java
        // 1. Declaration and memory allocation
        // int [] marks = new int[5];

        // 2. Declaration and then memory allocation
        // int [] marks;
        // marks = new int[5];
        // Initialization
        // marks[0] = 100;
        // marks[1] = 60;
        // marks[2] = 70;
        // marks[3] = 90;
        // marks[4] = 86;

        // 3. Declaration, memory allocation and initialization together
        int [] marks = {98, 45, 79, 99, 80};

        // marks[5] = 96; - throws an error
    

Multidimensional 2-D Array

Multidimensional Arrays are an Array of Arrays. Each elements of an M-D array is an array itself. Marks in the previous example was a 1-D array.

Multidimensional 2-D Array

A 2-D array can be created as follows:

int [][] flats = new int[2][3]          //A 2-D array of 2 rows + 3 columns

We can add elements to this array as follows

flats[0][0] = 100
flats[0][1] = 101
flats[0][2] = 102
// … & so on!

This 2-D array can be visualized as follows:

Similarly, a 3-D array can be created as follows:

String[][][] arr = new String [2][3][4]

package com.company;

public class cwh_28_multi_dim_arrays {
    public static void main(String[] args) {
        int [] marks; // A 1-D Array
        int [][] flats; // A 2-D Array
        flats = new int [2][3];
        flats[0][0] = 101;
        flats[0][1] = 102;
        flats[0][2] = 103;
        flats[1][0] = 201;
        flats[1][1] = 202;
        flats[1][2] = 203;

        // Displaying the 2-D Array (for loop)
        System.out.println("Printing a 2-D array using for loop");
        for(int i=0;i<flats.length;i++){
            for(int j=0;j<flats[i].length;j++) {
                System.out.print(flats[i][j]);
                System.out.print(" ");
            }
            System.out.println("");
        }

    }
Was this article helpful?
YesNo

Leave a Comment

Your email address will not be published. Required fields are marked *