Arrays
An Array is a collection of similar data elements, stored at contiguous memory locations and
shared by a common name.
Types of arrays
Single dimensional Arrays
Multi dimensional Arrays
Single Dimensional Arrays (1 D array)
It is an array with single subscript([])
A list of items can be given one variable name using only one subscript such a variable is
called a single-subscripted variable or a one-dimensional array.
Example-1
class ArrayDemo
{
public static void main(String args[])
{
int[] arr=new int[3];
System.out.println("Elements in an array....");
for(int i=0;i<3;i++)
{ ///i=0,1,2,3
System.out.println(arr[i]);
}
}
}
Second example
class ArrayDemo
{
public static void main(String args[])
{
int[] arr=new int[3];
arr[0]=100;
arr[1]=200;
arr[2]=300;
System.out.println("Elements in an array....");
for(int i=0;i<3;i++)
{ ///i=0,1,2,3
System.out.println(arr[i]);
}
}
}
Third example
import java.util.*;
class ArrayDemo
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int[] arr=new int[3];
System.out.println("Enter 3 elements...");
arr[0]=s.nextInt();;
arr[1]=s.nextInt();
arr[2]=s.nextInt();
System.out.println("Elements in an array....");
for(int i=0;i<3;i++)
{ ///i=0,1,2,3
System.out.println(arr[i]);
}
}
}
Another example
import java.util.*;
class ArrayDemo
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int[] arr=new int[3];
System.out.println("Enter 3 elements...");
for(int i=0;i<3;i++)
{ arr[i]=s.nextInt();
}
System.out.println("Elements in an array....");
for(int i=0;i<3;i++)
{ ///i=0,1,2,3
System.out.println(arr[i]);
}
}
}
Another example
import java.util.*;
class ArrayDemo
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
//int[] arr={10,20,30,40,50};
int[] arr=new int[]{10,20,30,40,50};
System.out.println("Elements in an array");
for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
}
}
Storing Elements into array dynamically
import java.util.Scanner;
class FindElement
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("Enter size of array:\t");
int n=s.nextInt();
int[] arr=new int[n];
System.out.println("Array Size:\t"+arr.length);
/*arr[0]=10; //arr[0]=1000+(0*4)=>1000
arr[1]=20; //arr[0]=1000+(1*4)=>1004
arr[2]=30; //arr[0]=1000+(2*4)=>1008
arr[3]=40; //arr[0]=1000+(3*4)=>1012
arr[4]=50; //arr[0]=1000+(4*4)=>1016*/
//arr.length=5
//i=0,1,2,3,4,5
System.out.println("Enter elements into array:\t");
for(int i=0;i<arr.length;i++)
{
arr[i]=s.nextInt();
//arr[0]=100
//arr[1]=100
//arr[2]=300
//arr[3]=400
//arr[4]=500
}
System.out.println("Elements in an array:\t");
for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
}
}
Output:
C:\Users\hp\Documents\ByteXL\Java\mic_ece\Programs>java FindElement
Enter size of array:
4
Array Size: 4
Enter elements into array:
100
200
300
400
Elements in an array:
100
200
300
400
Single Dimensional array example to assign values at the time of declaration and also used to find out
the whether the given element is existed or not
class SDArray2
{
public static void main(String args[])
{
java.util.Scanner s=new java.util.Scanner(System.in);
int[] arr1=new int[]{10,20,30,40,50};
//int[] arr1={10,20,30,40,50};
System.out.println("Enter element to find:\t");
int ele=s.nextInt();
for(int i=0;i<arr1.length;i++)
{
if(arr1[i]==ele)
{
System.out.printf("Element is found at index %d ",i);
break;
}
}
}
}
Output:
Finding element
import java.util.Scanner;
class FindElement
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("Enter size of array:\t");
int n=s.nextInt();
int[] arr=new int[n];
System.out.println("Enter elements into array:\t");
for(int i=0;i<arr.length;i++)
{ arr[i]=s.nextInt();
}
System.out.println("Which Element Do You Want to Find:\t");
int f=s.nextInt();//300
//i=0,1,2
boolean isFound=false;
for(int i=0;i<arr.length;i++)
{
if(arr[i]==f)
{
isFound=true;
break;
}
if(isFound)
{ System.out.println("Element is found");
}else
{ System.out.println("Element is Not found");
}
}
}
Output:
C:\Users\hp\Documents\ByteXL\Java\mic_ece\Programs>java FindElement
Enter size of array:
5
Enter elements into array:
100
200
300
400
500
Which Element Do You Want to Find:
100
Element is found
Example to find an element
class SDArray2
{
public static void main(String args[])
{
java.util.Scanner s=new java.util.Scanner(System.in);
int[] arr1=new int[]{10,20,30,40,50};
//int[] arr1={10,20,30,40,50};
System.out.println("Enter element to find:\t");
int ele=s.nextInt();
boolean existed=false;
for(int i=0;i<arr1.length;i++)
{
if(arr1[i]==ele)
{
System.out.printf("Element is found at index %d ",i);
existed=true;
break;
}
}
if(!existed)
{System.out.println("Element is not found....");
}
}
}
Output:
Enter element to find:
10
Element is found at index 0
Output-2:
Enter element to find:
100
Element is not found....
Foreach
Foreach is an extended for loop, which is used to get the element from an array or a collection object.
Example on for each
class ForEachDemo1
{
public static void main(String args[])
{
int[] arr1=new int[]{10,20,30,40,50};
System.out.println("Elements in an array.........");
for(int v:arr1)
{
System.out.println(v);
}
}
}
Foreach example to get the elements from a collection object
import java.util.*;
class ForEachDemo2
{
public static void main(String args[])
{
Vector<String> v=new Vector<String>();//collection object
v.add("Hello");
v.add("Hi");
v.add("Oye");
v.add("Yellehaaa");
v.add("Yetraaa");
System.out.println("Elements of colection object(vector object).........");
for(String mystr:v)
{
System.out.println(mystr);
}
}
}
Output:
C:\Users\hp\Documents\ByteXL\Java\mic_cse_2ndYears\Programs>java ForEachDemo2
Elements of colection object(vector object).........
Hello
Hi
Oye
Yellehaaa
Yetraaa
Storing objects in an array
class One
{
int a,b;
One(int a1,int b1)
{ a=a1;
b=b1;
}
void display()
{ System.out.println("Object state...");
System.out.println("a:\t"+a);
System.out.println("b:\t"+b);
}
}
class SDArray3
{
public static void main(String args[])
{
One o1=new One(1,2);
One o2=new One(10,20);
One o3=new One(100,200);
One [] arr=new One[3];
arr[0]=o1;
arr[1]=o2;
arr[2]=o3;
System.out.println("All Object States.....");
for(One o:arr)
{
o.display();
}
}
}
Output:
All Object States.....
Object state...
a: 1
b: 2
Object state...
a: 10
b: 20
Object state...
a: 100
b: 200
Two Dimensional Arrays
A two dimensional array is nothing but collection of 1d arrays.
We can create 2d arrays by using 2 subscripts.
If you want to store data in matrix or tabular format we create 2d arrays.
class TDArray
{
public static void main(String args[])
{
int[][] arr=new int[2][3];
System.out.println(arr);
System.out.println(arr[0]);
System.out.println(arr[1]);
System.out.printf("First row size:\t%d%n",arr[0].length);
System.out.printf("Second row size:\t%d%n",arr[1].length);
System.out.printf("%-5d",arr[0][0]);
System.out.printf("%-5d",arr[0][1]);
System.out.printf("%-5d",arr[0][2]);
System.out.println();
System.out.printf("%-5d",arr[1][0]);
System.out.printf("%-5d",arr[1][1]);
System.out.printf("%-5d",arr[1][2]);
}
}
2dArray program
import java.util.*;
class TDArray
{
public static void main(String args[])
{
int[][] arr=new int[2][3];
System.out.println("Enter elements");
Scanner s=new Scanner(System.in);
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
arr[i][j]=s.nextInt();
}
}
}
Example on 2d array
import java.util.*;
class TwoDArray1
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int[][] arr=new int[2][3];
System.out.println("arr:\t"+arr);
System.out.println("arr[0]:\t"+arr[0]);
System.out.println("arr[1]:\t"+arr[1]);
System.out.println("arr.length:\t"+arr.length);
System.out.println("arr[0].length:\t"+arr[0].length);
System.out.println("arr[1].length:\t"+arr[1].length);
//i=0,1,2
//arr.length=2
//arr[0].length=3
//arr[1].length=3
for(int i=0;i<arr.length;i++)
{ //j=0,1,2,3
for(int j=0;j<arr[i].length;j++)
{
arr[i][j]=s.nextInt();//10,20,30,40,50,60
}
}
System.out.println("Elements in 2d array:");
//i=0,1
for(int i=0;i<arr.length;i++)
{
//j=0
for(int j=0;j<arr[i].length;j++)
{
System.out.printf("%-5d",arr[i][j]);
}
System.out.printf("%n");
}
}
}
Output:
arr: [[I@27d6c5e0
arr[0]: [I@5f184fc6
arr[1]: [I@3feba861
arr.length: 2
arr[0].length: 3
arr[1].length: 3
10
20
30
40
50
60
Elements in 2d array:
10 20 30
40 50 60
JaggedArrayDemo
Jagged array is array of arrays such that member arrays can be of different sizes, i.e., we can create a
2-D arrays but with variable number of columns in each row. These type of arrays are also known as
ragged arrays.
Example on Jagged Array
class TDArray2
{
public static void main(String args[])
{
int arr[][]=new int[][]{
{10},
{10,20},
{10,20,30}
};
System.out.println("arr.length:\t"+arr.length);
//i=0,1,2
//arr.length=3
for(int i=0;i<arr.length;i++)
{
//j=0
for(int j=0;j<arr[i].length;j++)//must iterate ith row count times
{
System.out.printf("%-5d",arr[i][j]);
}
System.out.println();
}
}
}
//Jagged Arrays
//10___
//10___20___
//10___20___30___
//
Output:
C:\Users\hp\Documents\ByteXL\Java\mic_ece\Programs>java TDArray2
arr.length: 3
10
10 20
10 20 30
Getting elements from 1d array and 2d arrays using foreach
class TDArray2
{
public static void main(String args[])
{
int arr1[]={10,20,30,40,50};
int arr2[][]={
{10},
{10,20},
{10,20,30}
};
System.out.println("Single dimensional array");
for(int v:arr1)
{
System.out.printf("%-5d",v);
}
System.out.println("Two dimensional array");
for(int[] sd:arr2)
{
for(int v:sd)
{System.out.printf("%-5d",v);
}
System.out.println();
}
}
}
/*
10___
10___20___
10___20___30___
*/
Example on three dimensional array
class ThreeDArray
{
public static void main(String args[])
{
int[][][]arr={
{
{1},
{1,2},
{1,2,3}
},
{
{10},
{10,20},
{10,20,30}
},
};
for(int[][] tda:arr)
{
System.out.println(".................");
for(int[] sd:tda)
{
for(int v:sd)
{System.out.printf("%-5d",v);
}
System.out.println();
}
System.out.println(".................");
}
}
}
Output:
C:\Users\hp\Documents\ByteXL\Java\mic_ece\Programs>java ThreeDArray
.................
1
1 2
1 2 3
.................
.................
10
10 20
10 20 30
.................