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.
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,
Arrays are of different types:
A two-dimensional array is the simplest of multidimensional arrays. The exact limit of dimensions, if any, is determined by the compiler in use.
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.
Syntax:
type array-name[] =new type[size] ;
where,
note: An array must be declared or created before it can be used. This is a two-step process:
example:
double x[];
x=new double[5];
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);
}
}
}
1
2
3
4
5
6
7
8
9
10
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]));
}
}
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.
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:
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
Example:
int sales[][]= new int[5][10];
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();
}
}
}
1 2 3
4 5 6
7 8 9
Note: also read about the Jump Statements
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
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…