Java Array Statement
In java array is used to store more than one value simultaneously in same data
types. It will be further classified into three types.
Single dimensional array
Double dimensional array
Multi dimensional array
Single dimensional array
This type of array used to store more than one data in chain format. It is also
called as homogenous array.
Syntax:
datatype array_name[]=new datatype[size];
Ex:
Int a[]=new int[3];
a0 a1 a2
Double dimensional array
This type of array is used to store more than one value in simultaneously in
matrix format. It is also called as heterogeneous.
Syntax:
datatype array_name[][]=new datatype[row size][column size];
Ex:
Int a[][]=new int[3][3];
0,0 0,1 0,2
1,0 1,1 1,2
2,0 2,1 2,2
Multi dimensional array
This type of array combination of single and double dimensional array
Rules of array:
Initialize the array size.
You must increment the subscript value of array without increment it similar to
ordinary integer.
The incrementation of array is 0 to n-1.
In character array accept numeric. But it consider a only string not a numberic.
Advantages of arrays in java:-
Arrays store elements of the same data type and present in continuous
memory locations.
Array can store many number of elements at a time.
Arrays are commonly used in computer programs to organize data so that a
related set of values can be easily sorted or searched. For example, a search
engine may use an array to store Web pages found in a search performed by
the user.
Disadvantages of array in java:-
Arrays does not have add or remove methods.
We need to mention the size of the array. Fixed length.
So there is a chance of memory wastage.
Program: 1 (program for single dimensional array value accept from user)
import java.io.*;
class array1{
public static void main(String args[])
DataInputStream in=new DataInputStream(System.in);
try{
int a[]=new int[3];
System.out.println("Enter the First Value ");
a[0]=Integer.parseInt(in.readLine());
System.out.println("Enter the Second Value ");
a[1]=Integer.parseInt(in.readLine()); Compile:
System.out.println("Enter the third Value "); F:\jdk\bin>javac array1.java
a[2]=Integer.parseInt(in.readLine()); F:\jdk\bin>java array1
System.out.println("\n"+a[0]); Output:
System.out.println(a[1]); Enter the First Value
System.out.println(a[2]); 1
}catch(Exception e){ Enter the Second Value
System.out.println("Error"); 2
} Enter the third Value
} 3
} 1 2 3
Program: 2 (program for print the numbers using single dimensional array )
import java.io.*;
class array2
public static void main(String args[])
DataInputStream in=new DataInputStream(System.in);
try
{
int a[]=new int[10];
int i=0;
for(i=0;i<10;i++)
System.out.println("Enter the Number ");
a[i]=Integer.parseInt(in.readLine());
for(i=0;i<10;i++)
System.out.println(a[i]);
}catch(Exception e)
System.out.println("Error");
Compile:
F:\jdk\bin>javac array2.java
F:\jdk\bin>java array2
Output:
Enter the Number Enter the Number
1 8
Enter the Number Enter the Number
2 9
Enter the Number Enter the Number
3 10
Enter the Number 1 2 3 4 5 6 7 8 9 10
Enter the Number
Enter the Number
Enter the Number
Program: 3 (program for sum of 10 numbers)
import java.io.*;
class array3
public static void main(String args[])
DataInputStream in=new DataInputStream(System.in);
try
int a[]=new int[10];
int i=0,sum=0;
for(i=0;i<10;i++)
System.out.println("Enter the Value ");
a[i]=Integer.parseInt(in.readLine());
for(i=0;i<10;i++)
sum=(sum+a[i]);
System.out.println("Summation of 1-10 elements "+sum);
}catch(Exception e)
System.out.println("Error");
Compile:
F:\jdk\bin>javac array3.java
F:\jdk\bin>java array3
Output:
Enter the Value Enter the Value
1 8
Enter the Value Enter the Value
2 9
Enter the Value Enter the Value
3 10
Enter the Value Summation of 1-10 elements 55
Enter the Value
Enter the Value
Enter the Value, 7
Program: 4 (program for square of 10 elements using single dimensional
array)
import java.io.*;
class array4
public static void main(String args[])
DataInputStream in=new DataInputStream(System.in);
try{
int a[]=new int[10];
int i, sqr=0;
for(i=0;i<10;i++)
{
System.out.println("Enter "+(i+1)+" number");
a[i]=Integer.parseInt(in.readLine());
for(i=0;i<10;i++){
sqr=(a[i]*a[i]);
System.out.println(sqr);
}catch(Exception e)
System.out.println("Error");
Compile:
F:\jdk\bin>javac array4.java
F:\jdk\bin>java array4
Output:
Enter 1 number Enter 9 number
1 9
Enter 2 number Enter 10 number
2 10
Enter 3 number 1 4 9 16 25 36 49 64 81 100
3
Enter 4 number
Enter 5 number
Enter 6 number
Enter 7 number
Enter 8 number, 8
Program: 5 (Program for cube of 10 elements using single dimensional array)
import java.io.*;
class array5
public static void main(String args[])
DataInputStream in=new DataInputStream(System.in);
try{
int a[]=new int[10];
int i, sqr=0;
for(i=0;i<10;i++)
System.out.println("Enter "+(i+1)+" number");
a[i]=Integer.parseInt(in.readLine());
}
for(i=0;i<10;i++){
sqr=(a[i]*a[i]*a[i]);
System.out.println(sqr);
}catch(Exception e)
System.out.println("Error"); } } }
Compile:
F:\jdk\bin>javac array5.java
F:\jdk\bin>java array5
Output:
Enter 1 number Enter 9 number
1 9
Enter 2 number Enter 10 number
2 10
Enter 3 number 1 8 27 64 125 216 343 512 729 1000
Enter 4 number
Enter 5 number
Enter 6 number
6
Enter 7 number
Enter 8 number
Sorting
Sorting is used to arranging the array in ascending or descending order.
Types:
Selection sort:-
Bubble sort
Merge sort
Insertion sort
Quick sort
Heap sort
Which sorting method is most efficient and why?
Quicksort is one of the most efficient sorting algorithms, and this makes of it one of the most used as
well. The first thing to do is to select a pivot number, this number will separate the data, on its left are
the numbers smaller than it and the greater numbers on the right.
Program: 6(program for Selection sort in ascending order using array)
Selection sort is a simple comparison-
import java.io.*;
based sorting algorithm.
class array6 Advantages of Selection Sort:-
{ It performs very well on small lists.
public static void main(String args[]) It is an in-place algorithm. It does not
require a lot of space for sorting. Only
{ one extra space is required for holding
the temporal variable.
DataInputStream in=new DataInputStream(System.in);
Diadvantage:-
try
It performs poorly when working on
huge lists.
{
int a[]=new int[10];
int i=0,j=0,t=0;
for(i=0;i<10;i++){
System.out.println("Enter the 10 Elements : ");
a[i]=Integer.parseInt(in.readLine());
for(i=0;i<10;i++) {
for(j=i+1;j<10;j++) {
if(a[i]>a[j])
t=a[i]; 1.SWAPING LOGIC 2. SWAPING LOGIC
a[i]=a[j]; X=X + Y X=X * Y
Y= X – Y Y= X / Y
a[j]=t;
X= X - Y X= X / Y
}
for(i=0;i<10;i++) Enter the 10 Elements :
{ 7
System.out.println(a[i]); Enter the 10 Elements :
} 9
}catch(Exception e) { Enter the 10 Elements :
System.out.println("Error"); 2
} Enter the 10 Elements :
} 4
} Enter the 10 Elements :
Compile: 6
F:\jdk\bin>javac array6.java Enter the 10 Elements :
F:\jdk\bin>java array6 8
Output: Enter the 10 Elements :
Enter the 10 Elements : 10
1 1 2 3 4 5 6 7 8 9 10
Enter the 10 Elements :
Enter the 10 Elements : 5
Program: 7(program for Selection sort in descending order using array)
import java.io.*;
class array7
public static void main(String args[])
DataInputStream in=new DataInputStream(System.in);
try{
int a[]=new int[10];
int i=0,j=0,t=0;
for(i=0;i<10;i++)
System.out.println("Enter the 10 Elements : ");
a[i]=Integer.parseInt(in.readLine());
for(i=0;i<10;i++) {
for(j=i+1;j<10;j++) {
if(a[i]<a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
} Enter the 10 Elements :
} 9
} Enter the 10 Elements :
for(i=0;i<10;i++) 3
{ Enter the 10 Elements :
System.out.println(a[i]); 4
} Enter the 10 Elements :
}catch(Exception e){ 6
System.out.println("Error"); Enter the 10 Elements :
} 8
} Enter the 10 Elements :
} 10
Compile: Enter the 10 Elements :
F:\jdk\bin>javac array7.java 7
F:\jdk\bin>javac array7 10 9 8 7 6 5 4 3 2 1
Output:
Enter the 10 Elements :
Enter the 10 Elements :
2
Enter the 10 Elements : 5
Program: 8(program for Selection sort in Half ascending and Half descending
order using array)
import java.io.*;
class array8
public static void main(String args[])
DataInputStream in=new DataInputStream(System.in);
try
int a[]=new int[10];
int i=0,j=0,t=0;
System.out.println("Enter the 10 Element :");
for(i=0;i<10;i++)
a[i]=Integer.parseInt(in.readLine());
for(i=0;i<10;i++)
for(j=i+1;j<10;j++)
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
for(i=0;i<5;i++){
System.out.println(a[i]);
for(i=9;i>=5;i--){
System.out.println(a[i]);
}catch(Exception e)
System.out.println("Error");
Compile:
F:\jdk\bin>javac array8.java
F:\jdk\bin>java array8
Program: 9(program for bubble sort using array) What is bubble sort?
Bubble sort is a simple sorting algorithm
import java.util.*; used to rearrange a set of elements in
ascending or descending order. It's useful
for smaller sets of elements.
Advantage:-
in the bubble sort, elements are swapped
in place without using additional
class array9
public static void main(String args[])
Scanner sc=new Scanner(System.in);
int a[]=new int[10];
int i=0,j=0,t=0;
for(i=0;i<10;i++)
System.out.println("Enter the Values : ");
a[i]=sc.nextInt();
for(i=0;i<10;i++)
for(j=0;j<(10-i-1);j++)
if(a[j]>a[j+1])
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
for(i=0;i<10;i++)
System.out.println(a[i]);
} Enter the Values :
} 2
} Enter the Values :
Compile: 4
F:\jdk\bin>javac array9.java Enter the Values :
F:\jdk\bin>java array9 9
Output:
Enter the Values : Enter the Values :
1 6
Enter the Values : Enter the Values :
3 8
Enter the Values : Enter the Values :
5 10
Enter the Values : 1 2 3 4 5 6 7 8 9 10
Program:10 (Program for display a month using array)
class array10
public static void main(String args[]){
int month_days[]=new int[12];
month_days[0]=31;
month_days[1]=28;
month_days[2]=31;
month_days[3]=30;
month_days[4]=31;
month_days[5]=30;
month_days[6]=31;
month_days[7]=31;
month_days[8]=30;
month_days[9]=31; Compile:
month_days[10]=30; F:\jdk\bin>javac
array10.java
month_days[11]=31; F:\jdk\bin>java array10
System.out.println("April has "+month_days[3]+" days");
} Output:
April has 30 days
Program: 11(Program for display a month in another method)
class array11
public static void main(String args[])
{
int m_day[]={31,28,31,30,31,30,31,31,30,31,30,31};
System.out.println("April has "+m_day[3]+" days");
Compile:
F:\jdk\bin>javac array11.java
F:\jdk\bin>java array11
Output:
April has 30 days
Program: 12(Program for calculate an average using array)
class array12
public static void main(String args[])
double name[]={10.1,11.2,12.3,13.4,14.5};
double result=0.0;
for(int i=0;i<5;i++)
result=result+name[i];
System.out.println(result);
System.out.println("Avg is "+result/5);
Compile:
F:\jdk\bin>javac array12.java
F:\jdk\bin>java array12
Output:
61.49999999999999
Avg is 12.299999999999999
Program: 13(Program for Demonstrate a 2D array)
class array13
public static void main(String args[])
int i, j, k=0;
int two[][]=new int[4][5];
for(i=0;i<4;i++){
for(j=0;j<5;j++){
two[i][j]=k;
k++;
for(i=0;i<4;i++){ Compile:
for(j=0;j<5;j++) F:\jdk\bin>javac array13.java
{ F:\jdk\bin>java array13
System.out.print(two[i][j]+" "); Output:
} 01234
System.out.println("\n"); 56789
} 10 11 12 13 14
} 15 16 17 18 19
Program: 14(Program for Manually allocate different size 2D)
class array14
public static void main(String args[])
int three[][]=new int[4][];
three[0]=new int[1];
three[1]=new int[2];
three[2]=new int[3];
three[3]=new int[4];
int i, j, k=0;
for(i=0;i<4;i++)
for(j=0;j<i+1;j++)
three[i][j]=k;
k++;
for(i=0;i<4;i++)
for(j=0;j<i+1;j++)
System.out.print(three[i][j]+" ");
System.out.print("\n");
Compile:
F:\jdk\bin>javac array14.java
F:\jdk\bin>java array14
Output:
12
345
6789
Program: 15(Program for sorting an array)
class array15
public static void main(String args[])
int number[]={55,40,80,65,71};
int n=number.length;
System.out.println("Given List : ");
for(int i=0;i<n; i++)
{
System.out.print(" "+number[i]);
System.out.println("\n");
int i=0;
for(i=0;i<n; i++)
for(int j=i+1;j<n; j++)
if(number[i]>number[j])
int t=number[i];
number[i]=number[j];
number[j]=t;
for(i=0;i<n; i++)
System.out.println(number[i]);
Compile:
F:\jdk\bin>javac array15.java
F:\jdk\bin>java array15
Output:
Given List :
55 40 80 65 71
40 55 65 71 80