![]() |
How To Use Java Array Explain With Examples |
How To Use Java Array Explain With Examples
Array Definition: A java is a collection of data of the same data type.
It can be used to store groups of data simultaneously.
Java array can store data of same data type means an integer array can store only integer value, character array can store only character value and so on.
We can not fetch knowledge from an array directly thus we tend to use index purposes.
Java array could be an assortment of comparable styles of parts that have a contiguous memory location.
Java array is an Associate in the Nursing object that contains components of an identical information kind. it's an information structure wherever.
we have a tendency to store similar components. we will store solely a hard and fast set of components during a Java array.
Array in java is index-based, the primary component of the array is to hold on at the zero indexes.
Two types of Array in java
1. Single Dimensional ArrayAdvantages of Array in Java
Code Optimization: Java array makes the code optimized, we can retrieve or sort data efficiently.
Random access: we can get any knowledge placed at an index position.
Disadvantages of Array in Java
Size Limit: java array can store only the fixed size of elements in the array.
Syntax of Java Array
int ar[]=new int[5];
int ar[]=new int[5];
Here ar is the name of an array.
int is the data type of array.
The size of an array is five suggests that we can store most five values during this array.
How To Initialization of array in Java?
int ar[]={10,20,30,40,50};
int ar[]=new int[5];
ar[0]=50;
ar[1]=40;
ar[2]=30;
ar[3]=20;
ar[4]=10;
int ar[]={10,20,30,40,50};
int ar[]=new int[5];
ar[0]=50;
ar[1]=40;
ar[2]=30;
ar[3]=20;
ar[4]=10;
How To Use Array in Java Program?
class Icoderweb
{
public static void main(String[] args)
{
int ar[]={80,77,14,78,01};
System.out.println("Value is ar[0]:"+ar[0]);
System.out.println("Value is ar[1]:"+ar[1]);
System.out.println("Value is ar[2]:"+ar[2]);
System.out.println("Value is ar[3]:"+ar[3]);
System.out.println("Value is ar[4]:"+ar[4]);
}
}
*****OUTPUT*****
Value is ar[0]:10
Value is ar[1]:50
Value is ar[2]:80
Value is ar[3]:40
Value is ar[4]:60
class Icoderweb
{
public static void main(String[] args)
{
int ar[]={80,77,14,78,01};
System.out.println("Value is ar[0]:"+ar[0]);
System.out.println("Value is ar[1]:"+ar[1]);
System.out.println("Value is ar[2]:"+ar[2]);
System.out.println("Value is ar[3]:"+ar[3]);
System.out.println("Value is ar[4]:"+ar[4]);
}
}
*****OUTPUT*****
Value is ar[0]:10
Value is ar[1]:50
Value is ar[2]:80
Value is ar[3]:40
Value is ar[4]:60
How To Use Loop Array in Java Program?
class Icoderweb
{
public static void main(String[] args)
{
int ar[]={80,77,14,78,01};
for(int i=0;i<5;i++;)
System.out.println("Value is ar["+i+"]:"+ar[i]);
}
}
*****OUTPUT*****
Value is ar[0]:80
Value is ar[1]:77
Value is ar[2]:14
Value is ar[3]:78
Value is ar[4]:01
class Icoderweb
{
public static void main(String[] args)
{
int ar[]={80,77,14,78,01};
for(int i=0;i<5;i++;)
System.out.println("Value is ar["+i+"]:"+ar[i]);
}
}
*****OUTPUT*****
Value is ar[0]:80
Value is ar[1]:77
Value is ar[2]:14
Value is ar[3]:78
Value is ar[4]:01
How To Use User input Method Array in Java Program?
import java.util.Scanner;
class Icoderweb
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
int ar[]=new int[5];
int i;
for(i=0;i<=4;i++)
{
System.out.println("Enter element:"+(i+1));
ar[i]=in.nextInt();
}
for(i=0;i<=4;i++)
System.out.println("Value is ar["+i+"]:"+ar[i]);
}
}
*****OUTPUT*****
Enter element 1:80
Enter element 2:77
Enter element 3:14
Enter element 4:78
Enter element 5:01
Value is ar[0]:80
Value is ar[1]:77
Value is ar[2]:14
Value is ar[3]:78
Value is ar[4]:01
import java.util.Scanner;
class Icoderweb
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
int ar[]=new int[5];
int i;
for(i=0;i<=4;i++)
{
System.out.println("Enter element:"+(i+1));
ar[i]=in.nextInt();
}
for(i=0;i<=4;i++)
System.out.println("Value is ar["+i+"]:"+ar[i]);
}
}
*****OUTPUT*****
Enter element 1:80
Enter element 2:77
Enter element 3:14
Enter element 4:78
Enter element 5:01
Value is ar[0]:80
Value is ar[1]:77
Value is ar[2]:14
Value is ar[3]:78
Value is ar[4]:01
Two Dimensional Array
Array: java is a collection of data of the same data type. It can be used to store groups of data simultaneously.
Java array can store data of same data type means an integer array can store only integer value, character array can store only character value and so on.
We can not fetch data from an array directly therefore we use index point.
Java array is a collection of similar types of elements that have a contiguous memory location.
Java array is an object which contains elements of a similar data type. It is a data structure where we store similar elements. We can store only a fixed set of elements in a Java array.
Array in java is index-based, the first element of the array is stored at the 0 indexes.
Syntax of Two Dimensional Java Array
int ar[][]=new int[3][3];
int ar[][]=new int[3][3];
Here ar is the name of an array.
int is the data type of array.
The size of an array is 3x3 means we can store a maximum of 9 values in this array.
How To Initialization 2-D array in Java?
int ar[][]=new int[3][3];
ar[0][0]=80;
ar[0][1]=77;
ar[0][2]=14;
ar[1][0]=78;
ar[1][1]=01;
ar[1][2]=96
ar[2][0]=90;
ar[2][1]=40;
ar[2][2]=23;
int ar[][]=new int[3][3];
ar[0][0]=80;
ar[0][1]=77;
ar[0][2]=14;
ar[1][0]=78;
ar[1][1]=01;
ar[1][2]=96
ar[2][0]=90;
ar[2][1]=40;
ar[2][2]=23;
How To Use 2-D Array in Java Program?
import java.util.Scanner;
class icoderweb
{
public static void main(String[] args)
{
int a[][]={{80,77,14},{78,01,96},{90,40,23}};
System.out.println("value at a[0][0]:"+a[0][0]);
System.out.println("value at a[0][1]:"+a[0][1]);
System.out.println("value at a[0][2]:"+a[0][2]);
System.out.println("value at a[1][0]:"+a[1][0]);
System.out.println("value at a[1][1]:"+a[1][1]);
System.out.println("value at a[1][2]:"+a[1][2]);
System.out.println("value at a[2][0]:"+a[2][0]);
System.out.println("value at a[2][1]:"+a[2][1]);
System.out.println("value at a[2][2]:"+a[2][2]);
}
}
*****OUTPUT*****
value at a[0][0]:80
value at a[0][1]:77
value at a[0][2]:14
value at a[1][0]:78
value at a[1][1]:01
value at a[1][2]:96
value at a[2][0]:90
value at a[2][1]:40
value at a[2][2]:23
import java.util.Scanner;
class icoderweb
{
public static void main(String[] args)
{
int a[][]={{80,77,14},{78,01,96},{90,40,23}};
System.out.println("value at a[0][0]:"+a[0][0]);
System.out.println("value at a[0][1]:"+a[0][1]);
System.out.println("value at a[0][2]:"+a[0][2]);
System.out.println("value at a[1][0]:"+a[1][0]);
System.out.println("value at a[1][1]:"+a[1][1]);
System.out.println("value at a[1][2]:"+a[1][2]);
System.out.println("value at a[2][0]:"+a[2][0]);
System.out.println("value at a[2][1]:"+a[2][1]);
System.out.println("value at a[2][2]:"+a[2][2]);
}
}
*****OUTPUT*****
value at a[0][0]:80
value at a[0][1]:77
value at a[0][2]:14
value at a[1][0]:78
value at a[1][1]:01
value at a[1][2]:96
value at a[2][0]:90
value at a[2][1]:40
value at a[2][2]:23
How To Use Loop. 2-D Array in Java Program?
class icoderweb
{
public static void main(String[] args)
{
int a[][]={{80,77,14},{78,01,96},{90,40,23}};
for(int i=0;i<=2;i++)
{
for(int j=0;j<=2;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println("");
}
}
}
*****OUTPUT*****
80 77 14
78 01 96
90 40 23
class icoderweb
{
public static void main(String[] args)
{
int a[][]={{80,77,14},{78,01,96},{90,40,23}};
for(int i=0;i<=2;i++)
{
for(int j=0;j<=2;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println("");
}
}
}
*****OUTPUT*****
80 77 14
78 01 96
90 40 23
How To Use User input Method 2-D Array in Java Program?
import java.util.Scanner;
class icoderweb
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
int a[][]=new int[3][3];
int i,j;
System.out.println("Enter 9 Value");
for(i=0;i<=2;i++)
for(j=0;j<=2;j++)
a[i][j]=in.nextInt();
System.out.println("Matrix Data below");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println("");
}
}
}
*****OUTPUT*****
Enter 9 Value
80
77
14
78
01
96
90
40
23
Matrix Data below
80 77 14
78 01 96
90 40 23
import java.util.Scanner;
class icoderweb
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
int a[][]=new int[3][3];
int i,j;
System.out.println("Enter 9 Value");
for(i=0;i<=2;i++)
for(j=0;j<=2;j++)
a[i][j]=in.nextInt();
System.out.println("Matrix Data below");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println("");
}
}
}
*****OUTPUT*****
Enter 9 Value
80
77
14
78
01
96
90
40
23
Matrix Data below
80 77 14
78 01 96
90 40 23