Java Programs on Array Java by Rajesh Kumar Sahanee - May 18, 2019April 14, 20200 Post Views: 5,228 Hello Friends, It’s being long time I haven’t share anything with you but Today I am going share some useful Java Programs on Array. These programs is very useful for those who are currently learning java and their current topic is array. Interested person can learn many things about array in Java after going through these programs. Program 1: Program to accept element of array size 7 and display sum of all digits Program1.java Java import java.util.*; class Program1 { public static void main(String args[]) { int sum = 0, arr[] = new int[7]; Scanner in = new Scanner(System.in); System.out.println("Enter 7 elements of Array..."); for (int i = 0; i < 7; i++) { arr[i] = in.nextInt();//Taking input from user on console sum += arr[i]; } System.out.println("Sum of numbers are " + sum); } } 123456789101112131415 import java.util.*; class Program1 { public static void main(String args[]) { int sum = 0, arr[] = new int[7]; Scanner in = new Scanner(System.in); System.out.println("Enter 7 elements of Array..."); for (int i = 0; i < 7; i++) { arr[i] = in.nextInt();//Taking input from user on console sum += arr[i]; } System.out.println("Sum of numbers are " + sum); }} Output: Program 2: Program to sort array of size 5 using Insertion Sort Program2.java Java import java.util.*; class Program2 { public static void main(String args[]) { int arr[] = new int[5]; Scanner in = new Scanner(System.in); System.out.println("Enter 5 elements of Array..."); for (int i = 0; i < 5; i++) { arr[i] = in.nextInt(); } for (int i = 1; i < 5; i++) { int value = arr[i]; int j = i - 1; while (j >= 0 && arr[j] > value) { arr[j + 1] = arr[j]; j--; arr[j + 1] = value; }//end while }//end for System.out.print("Sorted List: "); for (int i = 0; i < 5; i++) { System.out.print(arr[i] + " "); } System.out.println(); }//end main }//end class 12345678910111213141516171819202122232425262728 import java.util.*; class Program2 { public static void main(String args[]) { int arr[] = new int[5]; Scanner in = new Scanner(System.in); System.out.println("Enter 5 elements of Array..."); for (int i = 0; i < 5; i++) { arr[i] = in.nextInt(); } for (int i = 1; i < 5; i++) { int value = arr[i]; int j = i - 1; while (j >= 0 && arr[j] > value) { arr[j + 1] = arr[j]; j--; arr[j + 1] = value; }//end while }//end for System.out.print("Sorted List: "); for (int i = 0; i < 5; i++) { System.out.print(arr[i] + " "); } System.out.println(); }//end main}//end class Output: Program 3: Program to accepts a list and search user’s element Program3.java Java import java.util.*; class Program3 { public static void main(String args[]) { double s, arr[] = new double[7]; Scanner in = new Scanner(System.in); System.out.println("Enter 7 elements of Array..."); for (int i = 0; i < 7; i++) { arr[i] = in.nextDouble(); } System.out.println("Enter element to search: "); s = in.nextDouble(); search(arr, s); }//end main static void search(double arr[], double s) { int loc = 0; boolean flag = false; for (int i = 0; i < 7; i++) { if (arr[i] == s) { loc = i + 1; flag = true; break; } else { flag = false; } }//end for if (flag == true) { System.out.println("Your number at location " + loc); } else { System.out.println("Your number not Exist in list"); } }//end search }//end class 12345678910111213141516171819202122232425262728293031323334353637 import java.util.*; class Program3 { public static void main(String args[]) { double s, arr[] = new double[7]; Scanner in = new Scanner(System.in); System.out.println("Enter 7 elements of Array..."); for (int i = 0; i < 7; i++) { arr[i] = in.nextDouble(); } System.out.println("Enter element to search: "); s = in.nextDouble(); search(arr, s); }//end main static void search(double arr[], double s) { int loc = 0; boolean flag = false; for (int i = 0; i < 7; i++) { if (arr[i] == s) { loc = i + 1; flag = true; break; } else { flag = false; } }//end for if (flag == true) { System.out.println("Your number at location " + loc); } else { System.out.println("Your number not Exist in list"); } }//end search }//end class Output: Program 4: Program to accepts a list and search user’s element using Binary Search Program4.java Java import java.util.*; class Program4 { public static void main(String args[]) { double s, arr[] = new double[7]; boolean flag = false; Scanner in = new Scanner(System.in); System.out.println("Enter 7 elements of Array in ascending order..."); for (int i = 0; i < 7; i++) { arr[i] = in.nextDouble(); } //Checking if elements are not in ascending order for (int i = 0; i < 6; i++) { for (int j = i + 1; j < 7; j++) { if (arr[i] > arr[j]) { flag = false; break; } else { flag = true; } }//end inner for if (flag == false) { break; } }//outer for if (flag == true) { System.out.println("Enter element to search: "); s = in.nextDouble(); bSearch(arr, s); } else { System.out.println("You not entered sorted list program will exit"); } }//end main static void bSearch(double arr[], double s) { int min = 0, max = 6, mid = 0; while (min <= max) { mid = (min + max) / 2; if (arr[mid] == s) { break; } if (s > arr[mid]) { min = mid + 1; } else { max = mid - 1; } } if (arr[mid] == s) { System.out.println("Your number at location " + (mid + 1)); } else { System.out.println("Your number not Exist in list"); } }//end bSearch }//end class 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 import java.util.*; class Program4 { public static void main(String args[]) { double s, arr[] = new double[7]; boolean flag = false; Scanner in = new Scanner(System.in); System.out.println("Enter 7 elements of Array in ascending order..."); for (int i = 0; i < 7; i++) { arr[i] = in.nextDouble(); } //Checking if elements are not in ascending order for (int i = 0; i < 6; i++) { for (int j = i + 1; j < 7; j++) { if (arr[i] > arr[j]) { flag = false; break; } else { flag = true; } }//end inner for if (flag == false) { break; } }//outer for if (flag == true) { System.out.println("Enter element to search: "); s = in.nextDouble(); bSearch(arr, s); } else { System.out.println("You not entered sorted list program will exit"); } }//end main static void bSearch(double arr[], double s) { int min = 0, max = 6, mid = 0; while (min <= max) { mid = (min + max) / 2; if (arr[mid] == s) { break; } if (s > arr[mid]) { min = mid + 1; } else { max = mid - 1; } } if (arr[mid] == s) { System.out.println("Your number at location " + (mid + 1)); } else { System.out.println("Your number not Exist in list"); } }//end bSearch }//end class Output: Program 5: Program to accept element in char array and display them in reverse order Program5.java Java import java.util.*; import java.io.*;//imported to access IOException class Program5 { public static void main(String args[]) throws IOException { char arr[]; int n; Scanner in = new Scanner(System.in); System.out.print("Enter the size of char array: "); n = in.nextInt(); arr = new char[n]; System.out.println("Enter elements of char array... "); for (int i = 0; i < n; i++) { arr[i] = (char) System.in.read(); } System.out.println("Output is.."); for (int i = n - 1; i >= 0; i--) { System.out.print(arr[i]); } System.out.println(); }//end main }//end class 1234567891011121314151617181920212223 import java.util.*;import java.io.*;//imported to access IOException class Program5 { public static void main(String args[]) throws IOException { char arr[]; int n; Scanner in = new Scanner(System.in); System.out.print("Enter the size of char array: "); n = in.nextInt(); arr = new char[n]; System.out.println("Enter elements of char array... "); for (int i = 0; i < n; i++) { arr[i] = (char) System.in.read(); } System.out.println("Output is.."); for (int i = n - 1; i >= 0; i--) { System.out.print(arr[i]); } System.out.println(); }//end main}//end class Output: Program 6: Program to take unique input from user Program6.java Java import java.util.*; class Program6 { public static void main(String args[]) { Scanner in = new Scanner(System.in); int arr[] = new int[10]; System.out.println("Enter element 1"); arr[0] = in.nextInt(); for (int i = 1; i < 10; i++) { System.out.println("Enter element " + (i + 1)); arr[i] = in.nextInt(); for (int j = i - 1; j >= 0; j--) { if (arr[i] == arr[j]) { System.out.println("You have already entered this number."); i--; } else { continue; } }//inner for }//outer for System.out.println("Your list of number is...."); for (int i = 0; i < 10; i++) { System.out.print(arr[i] + " "); } System.out.println(); }//end main }//end class 12345678910111213141516171819202122232425262728 import java.util.*; class Program6 { public static void main(String args[]) { Scanner in = new Scanner(System.in); int arr[] = new int[10]; System.out.println("Enter element 1"); arr[0] = in.nextInt(); for (int i = 1; i < 10; i++) { System.out.println("Enter element " + (i + 1)); arr[i] = in.nextInt(); for (int j = i - 1; j >= 0; j--) { if (arr[i] == arr[j]) { System.out.println("You have already entered this number."); i--; } else { continue; } }//inner for }//outer for System.out.println("Your list of number is...."); for (int i = 0; i < 10; i++) { System.out.print(arr[i] + " "); } System.out.println(); }//end main}//end class Output: Program 7: Program to accept 10 element of int array and shift all the even number to the beginning Program7.java Java import java.util.*; class Program7 { public static void main(String args[]) { Scanner in = new Scanner(System.in); int c1 = 0, c2 = 0; int arr[] = new int[10]; int even[] = new int[10]; int odd[] = new int[10]; System.out.println("Enter 10 elements of int array.."); for (int i = 0; i < 10; i++) { arr[i] = in.nextInt(); } for (int i = 0; i < 10; i++) { if (arr[i] % 2 == 0) { even[c1] = arr[i]; c1++; } else { odd[c2] = arr[i]; c2++; } }//end for for (int i = 0; i < 10; i++) { if (i < c1) { arr[i] = even[i]; } else { arr[i] = odd[i - c1]; } } System.out.println("Arranged elements are..."); for (int i = 0; i < 10; i++) { System.out.print(arr[i] + " "); } System.out.println(); } } 12345678910111213141516171819202122232425262728293031323334353637 import java.util.*; class Program7 { public static void main(String args[]) { Scanner in = new Scanner(System.in); int c1 = 0, c2 = 0; int arr[] = new int[10]; int even[] = new int[10]; int odd[] = new int[10]; System.out.println("Enter 10 elements of int array.."); for (int i = 0; i < 10; i++) { arr[i] = in.nextInt(); } for (int i = 0; i < 10; i++) { if (arr[i] % 2 == 0) { even[c1] = arr[i]; c1++; } else { odd[c2] = arr[i]; c2++; } }//end for for (int i = 0; i < 10; i++) { if (i < c1) { arr[i] = even[i]; } else { arr[i] = odd[i - c1]; } } System.out.println("Arranged elements are..."); for (int i = 0; i < 10; i++) { System.out.print(arr[i] + " "); } System.out.println(); }} Output: Program 8: Program to Accept element of 4×4 int array and Generate Various Output Program8.java Java import java.util.*; class Program8 { public static void main(String args[]) { Scanner in = new Scanner(System.in); int choice, arr[][] = new int[4][4]; System.out.println("Enter the element of 4x4 matrix.."); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { arr[i][j] = in.nextInt(); } } do { System.out.println("0.Exit"); System.out.println("1.Display sum of each row"); System.out.println("2.Display element in matrix format"); System.out.println("3.Display element of first diognal"); System.out.println("4.Display element of lower triangle"); System.out.println("5.Display element of upper triangle"); System.out.println("6.Display transpose of matrix"); System.out.print("Enter choice: "); choice = in.nextInt(); switch (choice) { case 0: break; case 1: matSum(arr); break; case 2: matDisplay(arr); break; case 3: matDiognal(arr); break; case 4: matLowerTriangle(arr); break; case 5: matUpperTriangle(arr); break; case 6: matTranspose(arr); break; default: System.out.println("Wrong choice!"); break; } } while (choice != 0); }//end main static void matSum(int arr[][]) { int sum = 0; for (int i = 0; i < 4; i++) { sum = 0; for (int j = 0; j < 4; j++) { sum += arr[i][j]; } System.out.println("Sum of Row " + i + " is " + sum); }//end outer for }//end matsum static void matDisplay(int arr[][]) { System.out.println("Entered matrix is.."); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { System.out.print(arr[i][j] + " "); } System.out.println(); }//end outer for }//end matdisplay static void matTranspose(int arr[][]) { System.out.println("Transpose of matrix is.."); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { System.out.print(arr[j][i] + " "); } System.out.println(); }//end outer for }//end mattranspose static void matDiognal(int arr[][]) { System.out.println("Diognal of matrix is.."); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (i == j) { System.out.print(arr[i][j]); } }//end inner for System.out.println(); }//end outer for }//end matdiognal static void matLowerTriangle(int arr[][]) { System.out.println("Lower triangle of matrix is.."); for (int i = 0; i < 4; i++) { for (int j = 0; j <= i; j++) { System.out.print(arr[i][j]); } System.out.println(); }//end outer for }//end matltriangle static void matUpperTriangle(int arr[][]) { System.out.println("Upper triangle of matrix is.."); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (j >= i) { System.out.print(arr[i][j]); } else { System.out.print(" "); } }//end inner for System.out.println(); }//end outer for }//end matutriangle }//end Class 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 import java.util.*; class Program8 { public static void main(String args[]) { Scanner in = new Scanner(System.in); int choice, arr[][] = new int[4][4]; System.out.println("Enter the element of 4x4 matrix.."); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { arr[i][j] = in.nextInt(); } } do { System.out.println("0.Exit"); System.out.println("1.Display sum of each row"); System.out.println("2.Display element in matrix format"); System.out.println("3.Display element of first diognal"); System.out.println("4.Display element of lower triangle"); System.out.println("5.Display element of upper triangle"); System.out.println("6.Display transpose of matrix"); System.out.print("Enter choice: "); choice = in.nextInt(); switch (choice) { case 0: break; case 1: matSum(arr); break; case 2: matDisplay(arr); break; case 3: matDiognal(arr); break; case 4: matLowerTriangle(arr); break; case 5: matUpperTriangle(arr); break; case 6: matTranspose(arr); break; default: System.out.println("Wrong choice!"); break; } } while (choice != 0); }//end main static void matSum(int arr[][]) { int sum = 0; for (int i = 0; i < 4; i++) { sum = 0; for (int j = 0; j < 4; j++) { sum += arr[i][j]; } System.out.println("Sum of Row " + i + " is " + sum); }//end outer for }//end matsum static void matDisplay(int arr[][]) { System.out.println("Entered matrix is.."); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { System.out.print(arr[i][j] + " "); } System.out.println(); }//end outer for }//end matdisplay static void matTranspose(int arr[][]) { System.out.println("Transpose of matrix is.."); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { System.out.print(arr[j][i] + " "); } System.out.println(); }//end outer for }//end mattranspose static void matDiognal(int arr[][]) { System.out.println("Diognal of matrix is.."); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (i == j) { System.out.print(arr[i][j]); } }//end inner for System.out.println(); }//end outer for }//end matdiognal static void matLowerTriangle(int arr[][]) { System.out.println("Lower triangle of matrix is.."); for (int i = 0; i < 4; i++) { for (int j = 0; j <= i; j++) { System.out.print(arr[i][j]); } System.out.println(); }//end outer for }//end matltriangle static void matUpperTriangle(int arr[][]) { System.out.println("Upper triangle of matrix is.."); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (j >= i) { System.out.print(arr[i][j]); } else { System.out.print(" "); } }//end inner for System.out.println(); }//end outer for }//end matutriangle }//end Class Output: Program 9: Program to check default values in Array of different data types Program9.java Java class Program9 { public static void main(String args[]) { byte b[] = new byte[5]; char c[] = new char[5]; int i[] = new int[5]; float f[] = new float[5]; double d[] = new double[5]; boolean bool[] = new boolean[5]; Program9 o[] = new Program9[5]; System.out.println("byte type default value: " + b[1]);//0 System.out.println("char type default value: " + c[1]);// System.out.println("int type default value: " + i[1]);//0 System.out.println("float type default vlaue: " + f[1]);//0.0 System.out.println("double type default vlaue: " + d[1]);//0.0 System.out.println("boolean type default vlaue: " + bool[1]);//false System.out.println("object type default vlaue: " + o[1]);//null } } 1234567891011121314151617181920 class Program9 { public static void main(String args[]) { byte b[] = new byte[5]; char c[] = new char[5]; int i[] = new int[5]; float f[] = new float[5]; double d[] = new double[5]; boolean bool[] = new boolean[5]; Program9 o[] = new Program9[5]; System.out.println("byte type default value: " + b[1]);//0 System.out.println("char type default value: " + c[1]);// System.out.println("int type default value: " + i[1]);//0 System.out.println("float type default vlaue: " + f[1]);//0.0 System.out.println("double type default vlaue: " + d[1]);//0.0 System.out.println("boolean type default vlaue: " + bool[1]);//false System.out.println("object type default vlaue: " + o[1]);//null }} Output: Program 10: Program to show various points to remember while working with array Program10.java Java class Program10 { public static void main(String args[]) { int n[][] = new int[5][3]; for (int i = 0; i < n.length; i++)// here n.length gives number of row which is 5 { for (int j = 0; j < n[i].length; j++)// here n[i].length gives number of element of each row { n[i][j] = i + j; } } for (int i = 0; i < n.length; i++) { for (int j = 0; j < n[i].length; j++) { System.out.print(n[i][j] + " "); } System.out.println(); } //In multidimensional array different row can have different number of element int k[][] = new int[4][]; k[0] = new int[7]; k[1] = new int[2]; k[2] = new int[5]; k[3] = new int[4]; for (int i = 0; i < k.length; i++) { System.out.print("row " + i + " of k: "); for (int j = 0; j < k[i].length; j++) { System.out.print(k[i][j] + " "); } System.out.println(); } System.out.println("Reference of variable k: " + k);//will give reference //int s[5]= {1,2,3,4,5};//compile time error //int s[] = new int[5]{1,2,3,4,5};//compile time error int s[] = {1, 2, 3, 4, 5}; System.out.print("Values in variables: "); for (int i = 0; i < s.length; i++) { System.out.print(s[i] + " "); } System.out.println(); } } 123456789101112131415161718192021222324252627282930313233343536373839404142 class Program10 { public static void main(String args[]) { int n[][] = new int[5][3]; for (int i = 0; i < n.length; i++)// here n.length gives number of row which is 5 { for (int j = 0; j < n[i].length; j++)// here n[i].length gives number of element of each row { n[i][j] = i + j; } } for (int i = 0; i < n.length; i++) { for (int j = 0; j < n[i].length; j++) { System.out.print(n[i][j] + " "); } System.out.println(); } //In multidimensional array different row can have different number of element int k[][] = new int[4][]; k[0] = new int[7]; k[1] = new int[2]; k[2] = new int[5]; k[3] = new int[4]; for (int i = 0; i < k.length; i++) { System.out.print("row " + i + " of k: "); for (int j = 0; j < k[i].length; j++) { System.out.print(k[i][j] + " "); } System.out.println(); } System.out.println("Reference of variable k: " + k);//will give reference //int s[5]= {1,2,3,4,5};//compile time error //int s[] = new int[5]{1,2,3,4,5};//compile time error int s[] = {1, 2, 3, 4, 5}; System.out.print("Values in variables: "); for (int i = 0; i < s.length; i++) { System.out.print(s[i] + " "); } System.out.println(); }} Output: Program 11: Program to demonstrate use of toString method Program11.java Java class Program11 { public static void main(String args[]) { char name[] = {'K', 'r', 'i', 's', 'h', 'n', 'a'}; System.out.println(name);//output will be Krishna because it is calling toString method of class Program11 obj = new Program11(); System.out.println(obj);//output will be Hello because it is calling toString method of class just as above int n[] = {20, 15, 37, 82}; for (int k : n)//for each statement here k takes value of n one by one { System.out.println(k);// here k printed no n } } public String toString() { return "Hello"; } } 123456789101112131415161718 class Program11 { public static void main(String args[]) { char name[] = {'K', 'r', 'i', 's', 'h', 'n', 'a'}; System.out.println(name);//output will be Krishna because it is calling toString method of class Program11 obj = new Program11(); System.out.println(obj);//output will be Hello because it is calling toString method of class just as above int n[] = {20, 15, 37, 82}; for (int k : n)//for each statement here k takes value of n one by one { System.out.println(k);// here k printed no n } } public String toString() { return "Hello"; }} Output: Program 12: Program to demonstrate specific class type array Program12.java Java class Student { private String name; private int roll; public int getRoll() { return roll; } public String getName() { return name; } public void setRoll(int n) { this.roll = n; } public void setName(String s) { this.name = s; } } class Program12 { public static void main(String args[]) { Student obj[] = new Student[3];//It creates Student type array of size 3 initialized with null value for (int k = 0; k < obj.length; k++) { obj[k] = new Student(); obj[k].setRoll(k); obj[k].setName("Rajesh"); } for (Student z : obj) { System.out.println("Roll: " + z.getRoll()); System.out.println("Name: " + z.getName()); } } } 12345678910111213141516171819202122232425262728293031323334353637 class Student { private String name; private int roll; public int getRoll() { return roll; } public String getName() { return name; } public void setRoll(int n) { this.roll = n; } public void setName(String s) { this.name = s; }} class Program12 { public static void main(String args[]) { Student obj[] = new Student[3];//It creates Student type array of size 3 initialized with null value for (int k = 0; k < obj.length; k++) { obj[k] = new Student(); obj[k].setRoll(k); obj[k].setName("Rajesh"); } for (Student z : obj) { System.out.println("Roll: " + z.getRoll()); System.out.println("Name: " + z.getName()); } }} Output: Program 13: Program to find min and max values from an array Program13.java Java import java.io.*; class Program13 { public static void main(String args[]) throws Exception { DataInputStream dis = new DataInputStream(System.in); int n, max, min; System.out.println("Enter the size of Array"); n = Integer.parseInt(dis.readLine());//readLine function is deprecated so avoid using int arr[] = new int[n]; System.out.println("Enter elements"); for (int i = 0; i < n; i++) { arr[i] = Integer.parseInt(dis.readLine()); } min = arr[0]; max = arr[0]; for (int i = 0; i < n; i++) { if (min > arr[i]) { min = arr[i]; } if (max < arr[i]) { max = arr[i]; } } System.out.println("Smallest number: " + min); System.out.println("Largest number: " + max); } } 12345678910111213141516171819202122232425262728293031 import java.io.*; class Program13 { public static void main(String args[]) throws Exception { DataInputStream dis = new DataInputStream(System.in); int n, max, min; System.out.println("Enter the size of Array"); n = Integer.parseInt(dis.readLine());//readLine function is deprecated so avoid using int arr[] = new int[n]; System.out.println("Enter elements"); for (int i = 0; i < n; i++) { arr[i] = Integer.parseInt(dis.readLine()); } min = arr[0]; max = arr[0]; for (int i = 0; i < n; i++) { if (min > arr[i]) { min = arr[i]; } if (max < arr[i]) { max = arr[i]; } } System.out.println("Smallest number: " + min); System.out.println("Largest number: " + max); }} Output: Thank for visiting Please share if you like it