Array Usage Examples
Program to add element in Array
Program to remove element from Array
Program to sort an Array
Program to search an element in Array
Program to find min and max Element in Array
Program to compare two Arrays
Program to add element in Array
class Demo
{
public static void main(String args[])
{
int []array1=new int[5];
System.out.println("Add values in Array..");
array1[0]=120;
array1[1]=56;
array1[2]=10;
array1[3]=86;
array1[4]=2;
System.out.println("Values of array");
for(int i=0; i< array1.length;i++)
{
System.out.println("index "+i+" have "+
array1[i]+" value.");
}
}
}
Output
Add values in Array.. Values of array index 0 have 120 value. index 1 have 56 value. index 2 have 10 value. index 3 have 86 value. index 4 have 2 value.
Program to remove element from Array
Program to remove element from Array
class Demo
{
public static void main(String args[])
{
int[] array1=new int[5];
System.out.println("Add values in Array..");
array1[0]=120;
array1[1]=56;
array1[2]=10;
array1[3]=86;
array1[4]=2;
System.out.println("Values of array");
for(int i=0;i<5;i++)
{
System.out.println("index "+i+" have
"+array1[i]+" value.");
}
System.out.println("Remove element from 3rd index");
System.out.println("index 3 have "+array1[3]+" value.");
for(int i=0;i<4;i++)
{
if(i>=3)
{
array1[i]=array1[i+1];
}
}
System.out.println("New values of array");
for(int i=0;i<4;i++)
{
System.out.println("index "+i+" have
"+array1[i]+" value.");
}
}
}
Output
Add values in Array.. Values of array index 0 have 120 value. index 1 have 56 value. index 2 have 10 value. index 3 have 86 value. index 4 have 2 value. Remove element from 3rd index index 3 have 86 value. New values of array index 0 have 120 value. index 1 have 56 value. index 2 have 10 value. index 3 have 2 value.
Program to sort an Array
Program to sort an Array
import java.util.Arrays;
public class Demo
{
public static void main(String args[]) throws Exception
{
int array[] = { 2, 5, -2, 6, -3, 8, 0, -7, -9, 4 };
Arrays.sort(array);
System.out.println("Sorted array : [length:
" + array.length + "]");
for (int i = 0; i< array.length; i++)
{
if(i != 0)
{
System.out.print(", ");
System.out.print(array[i]);
}
}
}
}
Output
Sorted array : [length: 10] , -7, -3, -2, 0, 2, 4, 5, 6, 8
Program to search an element in Array
Program to search an element in Array
import java.util.Arrays;
public class Demo
{
public static void main(String args[]) throws Exception
{
int array[] = { 2, 5, -2, 6, -3, 8, 0, -7, -9, 4 };
System.out.println("Values in Array ");
for (int i = 0; i < array.length; i++)
{
if(i != 0)
{
System.out.print(", ");
}
System.out.print(array[i]);
}
int index = Arrays.binarySearch(array, 2);
System.out.println("Found 2 @ " + index);
}
}
Output
Values in Array 2, 5, -2, 6, -3, 8, 0, -7, -9, 4Found 2 @ -10
Program to find min and max Element in Array
Program to find min and max Element in Array
import java.util.Arrays;
import java.util.Collections;
public class Main
{
public static void main(String[] args)
{
Integer[] numbers = { 8, 2, 7, 1, 4, 9, 5};
int min = (int) Collections.min(Arrays.asList(numbers));
int max = (int) Collections.max(Arrays.asList(numbers));
System.out.println("Min number: " + min);
System.out.println("Max number: " + max);
}
}
Output
Min number: 1 Max number: 9
Program to compare two Arrays
Program to compare two Arrays
import java.util.Arrays;
public class Main
{
public static void main(String[] args) throws Exception
{
int[] ary = {1,2,3,4,5,6};
int[] ary1 = {1,2,3,4,5,6};
int[] ary2 = {1,2,3,4};
System.out.println("Is array 1 equal to array 2??
"+Arrays.equals(ary, ary1));
System.out.println("Is array 1 equal to array 3??
"+Arrays.equals(ary, ary2));
}
}
Output
Is array 1 equal to array 2?? true Is array 1 equal to array 3?? false


