An array is a group of homogeneous variables that are referred to by a common name. Arrays can be created of any dimension(One Dimension or Many Dimension). Every element within an array is accessed by its index.
Advantages
Code Optimization: It makes the code optimized, we can retrieve or sort the data efficiently.
Random access: We can get any data located at an index position.
Disadvantages
Size Limit: We can store only the fixed size of elements in the array. It doesn’t grow its size at runtime. To solve this problem, collection framework is used in Java which grows automatically.
One Dimensional Array
In one dimension array, the number of row is always one where as there may have n number of columns.
import java.util.*;
import java.lang.*;
import java.io.*;
class Codewindow
{
public static void main (String[] args) throws java.lang.Exception
{
int arr[]=new int[5];
arr[0]=10;
arr[1]=20;
arr[2]=30;
arr[3]=50;
arr[4]=40;
for(int i=0; i<arr.length;i++)
System.out.println(arr[i]);
}
}
//Code Contribution: CodeWindow
Output:
10
20
30
50
40
Different ways to define One Dimension Array
import java.util.*;
import java.lang.*;
import java.io.*;
class Codewindow
{
public static void main (String[] args)
{
#type1
int type1[]=new int[5];
#type2
int type2[];
type2=new int[5];
#type3
int type3[]={1,2,3,4,5};
}
}
//Code Contribution: CodeWindow
!!WARNING!!
If the array size is 5, then you can access only index 0 to index 4. If you wish to access out of this range then you will get a “java.lang.ArrayIndexOutOfBoundsException“.
import java.util.*;
import java.lang.*;
import java.io.*;
class Codewindow
{
public static void main (String[] args)
{
int arr[]=new int[5];
arr[0]=10;
arr[1]=20;
arr[2]=30;
arr[3]=50;
arr[4]=40;
System.out.println(arr[5]);
}
}
//Code Contribution: CodeWindow
Error:
Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 5
at Codewindow.main(Main.java:16)
Different ways to print Array elements
import java.util.*;
import java.lang.*;
import java.io.*;
class Codewindow
{
public static void main (String[] args)
{
int arr[]=new int[5];
arr[0]=10;
arr[1]=20;
arr[2]=30;
arr[3]=50;
arr[4]=40;
//Way:: 1
for(int i=0; i<arr.length;i++)
System.out.println(arr[i]);
System.out.println();
//Way:: 2
for(int i:arr)
System.out.println(i);
}
}
//Code Contribution: CodeWindow
Output:
10
20
30
50
40
10
20
30
50
40
Multi-Dimensional Array
import java.util.*;
import java.lang.*;
import java.io.*;
class Codewindow
{
public static void main (String[] args)
{
int arr[][]=new int[2][3];
int k=0;
for(int row=0; row<2;row++)
for(int col=0; col<3;col++)
arr[row][col]=++k;
for(int row=0; row<2;row++)
{
for(int col=0; col<3;col++)
System.out.print(arr[row][col]);
System.out.println();
}
}
}
//Code Contribution: CodeWindow
Output:
123
456
Ragged / Jagged Array
import java.util.Arrays;
class CodeWindow
{
public static void main(String[] args)
{
// Declaration
int[][] arr = new int[3][];
// Initialization
arr[0] = new int[] { 1, 2, 3, 4 , 5 };
arr[1] = new int[] { 4, 5, 6, 7 };
arr[2] = new int[] { 8, 9, 10, 11 };
// print the array elements
for (int[] row: arr) {
System.out.println(Arrays.toString(row));
}
}
}
//Code Contribution: Code Window