Categories: Java

Java Arrays

An array is a collection of variables of the same type that are referenced by a common name. Arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.

Need for Arrays:

The primary reason for arrays’ existence is that they allow for the storage of multiple elements and values under a single name. For the storage of different values, different variables do not need to be created, and they can be stored in different indices of the same object.

Therefore, arrays are very much useful in a case where quite many elements of the same data types need to be stored and processed.

For instance,

  • we declare an array of 10 elements – arr[10].
  • then, the elements of these arrays will be referred to as array-name[n], where n is the element number in the array.
  • the element numbers in [] are called subscripts or indices. The subscript, or index, of an element, designates its position in the array’s ordering.
  • Java array numbering starts from 0.
  • The data type of array elements is known as the base type of the array.
Types of arrays:

Arrays are of different types:

  1. One-dimensional array: comprises finite homogenous elements
  2. Multidimensional array: comprises elements, each of which is itself an array.

A two-dimensional array is the simplest of multidimensional arrays. The exact limit of dimensions, if any, is determined by the compiler in use.

Single dimensional array:

The simplest form of an array is a single-dimensional array. The array is given a name and its elements are referred to by their subscripts or indices.

Declaring Arrays:

Syntax:

type array-name[] =new type[size] ;

where,

  • type declares the base type of the array
  • array-name specifies the name with which the array will be referenced.
  • size defines how many elements the array will hold.

note: An array must be declared or created before it can be used. This is a two-step process:

  • First, declare a reference to an array.
  • Then, create the array with the new operator.

example:

double x[];
x=new double[5];
Array example: Printing elements of the array

class Demo
{
public static void main(String[] args)
  {
    int[] arr = new int[10];
    for(int i=0;i<10;i++)
        {        
                arr[i]=i+1;
        }
        for(int x : arr)
        {        
                System.out.println(x);
        }
   }
}
 
Output:
1
2
3
4
5
6
7
8
9
10
  • here, we input the value of i+1 in the array element at ith index.
  • then printed the elements.
  • one can do so without using loops, yet it might be lengthy with big data.
Accessing array elements:

class Demo
{
public static void main(String[] args)
  {
    int[] arr = new int[10];
    for(int i=0;i<10;i++)
        {        
                arr[i]=i+1;
        }
              
    System.out.println("element at 1st index= "+arr[0]);
    System.out.println("\nsum of element at 4th and 5th index = "+(arr[3]+arr[4]));
        
   }
}
 
Output:
element at 1st index= 1
sum of element at 4th and 5th index = 9

here, we randomly accessed the array elements using the index numbers.

Multi-dimensional array;

A multidimensional array resembles a single-dimensional array in appearance. Unlike a single-dimensional array, which can only have one row, it can have multiple rows and columns.

It is a tabular representation of data that stores data in rows and columns.

Let’s take a look at a two-dimensional array:

Two-dimensional array:

The simplest form of a multidimensional array, the two-dimensional array, is an array having single-dimension arrays as its elements. It is an array in which each element is itself an array, a 1-D array. For instance, an array arr[M][N] is an M by N table with M rows and N columns containing M*N elements.

syntax:

type array-name[][]=new type [rows][columns]

where

  • type is the base data type.
  • array-name is the name of an array.
  • rows, the first index, refers to the number of rows in the array
  • columns, the second index. refers to the number of columns in the array.

Example:

int sales[][]= new int[5][10];
Program: two-D array

class Demo
{
public static void main(String[] args)
  {
    int[][] arr = new int[3][3];
    int k=1;
    for(int i=0;i<3;i++)
        {       for(int j=0;j<3;j++)
          {        
                arr[i][j]=k;
                k++;
          }
        }
             
              for(int i=0;i<3;i++)
        {       for(int j=0;j<3;j++)
          {        
               System.out.print(arr[i][j]+" ");
          }
          System.out.println();
        } 
               
            
   }
}
 
Output:
1 2 3 
4 5 6 
7 8 9 

Note: also read about the Jump Statements

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

2 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago