Java Design Pattern
Introduction to Java 10
Introduction to Java 11
Introduction to Java 12

Java Array Program

Largest.java
class Largest {
 	public static void main(String args[]) {
  		int a[] = {
   			3,
		   	5,
		   	6,
		   	2,
		   	4
  		};
  		int i, large;
  		large = a[0];
  		for (i = 1; i <= 4; i++) {
		if (a[i] > large)
			large = a[i];
		}
  		System.out.println("the largest item is:" + large);
 	}
}
Output

the largest item is:6


Sort.java
import java.util.Scanner;
class Sort {
	public static void main(String args[]) {
		int i, j, temp, size;
		Scanner s = new Scanner(System.in);
		System.out.println("Enter the size of the array");
		size = s.nextInt();
		int a[] = new int[size];
		System.out.println("Enter the array elements");
		for (i = 0; i < size; i++) {
			a[i] = s.nextInt();
		}
		for (i = 0; i < size; i++) {
			for (j = 0; j < size - i - 1; j++) {
				if (a[j] > a[j + 1]) {
					temp = a[j];
					a[j] = a[j + 1];
					a[j + 1] = temp;
				}
			}
		}
		System.out.println("The array elements are in sorted order");
		for (i = 0; i < size; i++) {
			System.out.println(a[i]);
		}
	}
}
Output:

Enter the size of the array
5
Enter the array elements
3
6
1
9
7
The array elements are in sorted order
1
3
6
7
9


Search.java
import java.util.Scanner;
class Search
{
	public static void main(String args[])
	{
		int i,item,loc=-1,size;
		Scanner s= new Scanner(System.in);
		System.out.println("Enter the size of the array");
		size=s.nextInt();
		int a[]=new int[size];
		System.out.println("Enter the array elements");
		for(i=0;i<size;i++)
		{
			a[i]=s.nextInt();
		}
		System.out.println("Enter the item to be searched");
		item=s.nextInt();
		for(i=0;i<size;i++)
		{
			if(item==a[i])
			{
				loc=i;
				break;
			}
		}
		System.out.println("index location is"+loc);
	}
}
Output:

Enter the size of the array
5
Enter the array elements
2
5
3
7
9
Enter the item to be searched
5
Index location is 1.

About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.


We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc






 PreviousNext