In this article we are going to cover all top 29 array program in java, In previous post we have see the Counting program
This Articles Contents
Write an array program in java to add the element of the array.
package com.onurdesk.array.program;
public class AddAllArrayElements {
/**
* @author Onurdesk
*
* Thanks for visiting https://onurdesk.com
like us on facebook :
* https://facebook.com/onurdesk
*/
public static void main(String[] args) {
int[] arr = { 74, 784, 909 };
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum = sum + arr[i];
}
System.out.println(sum);
System.out.println("Thanks for visiting https://onurdesk.com");
}
}
Write an array program in java to add the element taken by the user of the array.
package com.onurdesk.array.program;
import java.util.Scanner;
public class AddElementsInArray {
/**
* @author Onurdesk
*
* Thanks for visiting https://onurdesk.com like us on facebook :
* https://facebook.com/onurdesk
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter an arrray size");
int size = sc.nextInt();
System.out.println("Enter the elements of an array");
int array[] = new int[size];
for (int i = 0; i < size; i++) {
array[i] = sc.nextInt();
}
int sum = 0;
for (int i : array) {
sum = sum + i;
}
System.out.println("sum of array elements are " + sum);
System.out.println("Thanks for visiting https://onurdesk.com");
}
}
Write an array program in java to sort the array element and search an element using binary search.
package com.onurdesk.array.program;
import java.util.Arrays;
import java.util.Scanner;
public class ArrayElementPositionUsingBinarySearch {
/**
* @author Onurdesk
*
* Thanks for visiting https://onurdesk.com like us on facebook :
* https://facebook.com/onurdesk
*/
public static int binarySearch(int ary[], int key) {
int start = 0;
int end = ary.length - 1;
while (start <= end) {
int mid = (start + end) / 2;
if (key == ary[mid]) {
return mid;
}
if (key < ary[mid]) {
end = mid - 1;
} else {
start = mid + 1;
}
}
return -1;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of an array");
int len = sc.nextInt();
int arry[] = new int[len];
System.out.println("Enter array elements ");
for (int i = 0; i < len; i++) {
arry[i] = sc.nextInt();
}
Arrays.sort(arry);
System.out.println("After sorting elements are :");
for (int a : arry) {
System.out.println(a);
}
System.out.println("Enter key value");
int k = sc.nextInt();
System.out.println("element position is on index number is " + binarySearch(arry, k));
System.out.println("Thanks for visiting https://onurdesk.com");
}
}
Write an array program in java to Reverse the array element.
package com.onurdesk.array.program;
public class ArrayInReverseOrder {
/**
* @author Onurdesk
*
* Thanks for visiting https://onurdesk.com like us on facebook :
* https://facebook.com/onurdesk
*/
public static void main(String[] args) {
int a[] = { 1, 2, 3, 4, 5 };
reverseOrder(a);
System.out.println();
System.out.println("Thanks for visiting https://onurdesk.com");
}
public static void reverseOrder(int[] a) {
for (int i = a.length - 1; i >= 0; i--) {
System.out.print(a[i] + " ");
}
}
}
Write an array program in java for Array List Demo.
package com.onurdesk.array.program;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.RandomAccess;
public class ArrayListDemo {
/**
* @author Onurdesk
*
* Thanks for visiting https://onurdesk.com like us on facebook :
* https://facebook.com/onurdesk
*/
public static void main(String[] args) {
ArrayList a = new ArrayList();
a.add("A");
a.add(10);
a.add('A');
a.add(null);
System.out.println(a);
a.remove(2);
System.out.println(a);
a.add(2, "M");
System.out.println(a);
a.add("n");
System.out.println(a);
ArrayList a1 = new ArrayList();
LinkedList a2 = new LinkedList();
System.out.println(a1 instanceof Serializable); // true
System.out.println(a2 instanceof Cloneable); // true
System.out.println(a1 instanceof RandomAccess); // true
System.out.println(a2 instanceof RandomAccess); // false
List l1 = Collections.synchronizedList(a);
System.out.println("Thanks for visiting https://onurdesk.com");
}
}
Write a an array program in java for Array List Example.
package com.onurdesk.array.program;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
public class ArrayListExample {
/**
* @author Onurdesk
*
* Thanks for visiting https://onurdesk.com like us on facebook :
* https://facebook.com/onurdesk
*/
public static void main(String[] args) {
ArrayList<Integer> al2 = new ArrayList<>();
System.out.println("capcity " + al2.size());
ArrayList<Integer> al1 = new ArrayList<>(22);
System.out.println("capcity " + al1.size());
ArrayList<Integer> al = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 89));
al.add(9);
al.add(1, 90);
al.add(3);
al.remove(6);
System.out.println(al.size());
System.out.println(al);
System.out.println(al);
Iterator<Integer> itr = al.iterator();
while (itr.hasNext()) {
System.out.println(itr.next());
}
System.out.println(al.contains(88));
Object[] obj = al.toArray();
System.out.println(Arrays.toString(obj));
for (Object ch : obj) {
System.out.println(ch);
}
System.out.println("getting Object " + al.get(6));
al.removeAll(al);
System.out.println(al);
System.out.println("Thanks for visiting https://onurdesk.com");
}
}
Write a program in java to perform array operation using bubble sort.
package com.onurdesk.array.program;
import java.util.Scanner;
public class ArrayOperationsUsingBubbleSort {
/**
* @author Onurdesk
*
* Thanks for visiting https://onurdesk.com like us on facebook :
* https://facebook.com/onurdesk
*/
public static void bubSort(int a[], int n) {
for (int i = 0; i < n - 2; i++) {
for (int j = 0; j <= n - 2 - i; j++) {
if (a[j] > a[j + 1]) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
System.out.println("After Sorting elements are ");
for (int b : a) {
System.out.println(b);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of an array");
int len = sc.nextInt();
int arry[] = new int[len];
System.out.println("Enter array elements ");
for (int i = 0; i < len; i++) {
arry[i] = sc.nextInt();
}
bubSort(arry, len);
System.out.println("smallest elemet is :" + arry[0]);
System.out.println("largest element is :" + arry[len - 1]);
System.out.println("Enter which element you want");
int k = sc.nextInt();
for (int i = 0; i < len; i++) {
if (k == arry[i]) {
System.out.println("element is found in " + k + "th position");
} else {
System.out.println("element is not found");
}
}
System.out.println("Thanks for visiting https://onurdesk.com");
}
}
Write an array program in java for array example.
package com.onurdesk.array.program;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ArraysExample {
/**
* @author Onurdesk
*
* Thanks for visiting https://onurdesk.com like us on facebook :
* https://facebook.com/onurdesk
*/
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("apple");
list.add("orange");
list.add("banana");
list.add("guvava");
System.out.println(list);
String[] s = { "first", "second", "third", "fourth" };
System.out.println(s);
System.out.println(Arrays.toString(s));
String[] s1 = { "fifth", "sixth", "seventh" };
String[][] s2 = { s, s1 };
System.out.println(Arrays.deepToString(s2));
System.out.println("Thanks for visiting https://onurdesk.com");
}
}
Write an array program in java for arrays of array.
package com.onurdesk.array.program;
import java.util.Arrays;
public class ArraysOfArrays {
/**
* @author Onurdesk
*
* Thanks for visiting https://onurdesk.com like us on facebook :
* https://facebook.com/onurdesk
*/
public static void main(String[] args) {
int[] a = { 1, 4, 55 };
int[] b = { 23, 4, 5 };
System.out.println(a);
System.out.println(Arrays.toString(a));
System.out.println(b);
System.out.println(Arrays.toString(b));
int[][] c = { a, b };
System.out.println(c);
System.out.println(Arrays.deepToString(c));
// int[][] d = { b, c };
// System.out.println(Arrays.deepToString(d));
System.out.println("Thanks for visiting https://onurdesk.com");
}
}
Write an array program in java to add elements of the array.
package com.onurdesk.array.program;
public class ArraysParctice {
/**
* @author Onurdesk
*
* Thanks for visiting https://onurdesk.com like us on facebook :
* https://facebook.com/onurdesk
*/
public static void main(String[] args) {
System.out.println(sum(new int[] { 1, 3, 4, 6 }));
System.out.println("Thanks for visiting https://onurdesk.com");
}
public static int sum(int[] x) {
int tot = 0;
for (int x1 : x) {
tot += x1;
}
return tot;
}
}
Write an array program in java to find common element in array.
package com.onurdesk.array.program;
import java.util.Arrays;
public class CommonElementsInArray {
/**
* @author Onurdesk
*
* Thanks for visiting https://onurdesk.com like us on facebook :
* https://facebook.com/onurdesk
*/
public static void main(String[] args) {
int[] arry1 = { 1995, 1998, 1986, 1985, 1999, 2000 };
int[] arry2 = { 1996, 1992, 1988, 2000, 1955, 1999 };
System.out.println("Array1 ;" + Arrays.toString(arry1));
System.out.println("Array2 ;" + Arrays.toString(arry2));
for (int i = 0; i < arry1.length; i++) {
for (int j = 0; j < arry2.length; j++) {
if (arry1[i] == arry2[j]) {
System.out.println("Common elements are " + arry1[i]);
}
}
}
System.out.println("Thanks for visiting https://onurdesk.com");
}
}
Write an array program in java to find common integer of array.
package com.onurdesk.array.program;
import java.util.Arrays;
public class CommonIntegersArrays {
/**
* @author Onurdesk
*
* Thanks for visiting https://onurdesk.com like us on facebook :
* https://facebook.com/onurdesk
*/
public static void main(String[] args) {
int[] arr1 = { 4, 7, 3, 9, 2 };
int[] arr2 = { 3, 2, 12, 9, 40, 32, 4 };
commonIntegers(arr1, arr2);
}
public static void commonIntegers(int[] arr1, int[] arr2) {
Arrays.sort(arr1);
System.out.println(Arrays.toString(arr1));
Arrays.sort(arr2);
System.out.println(Arrays.toString(arr2));
for (int i = 0; i < arr1.length; i++) {
for (int j = 0; j < arr2.length; j++) {
if (arr1[i] == arr2[j]) {
System.out.print(arr1[i] + " ");
}
}
}
System.out.println("Thanks for visiting https://onurdesk.com");
}
}
Write an array program in java to find common string in array.
package com.onurdesk.array.program;
import java.util.Arrays;
import java.util.HashSet;
public class CommonStringsInArray {
/**
* @author Onurdesk
*
* Thanks for visiting https://onurdesk.com like us on facebook :
* https://facebook.com/onurdesk
*/
public static void main(String[] args) {
// enter string value.
String[] array1 = { "C", "C++", "C#", "JAVA", "SQL", "ORACLE" };
String[] array2 = { "MySQL", "SQL", "Android", "ORACLE", "PostgreSQL", "DB2", "JAVA" };
// print both the string.
System.out.println("Array1 : " + Arrays.toString(array1));
System.out.println("Array2 : " + Arrays.toString(array2));
HashSet<String> set = new HashSet<String>();
for (int i = 0; i < array1.length; i++) {
for (int j = 0; j < array2.length; j++) {
if (array1[i].equals(array2[j])) {
set.add(array1[i]);
}
}
}
// return common elements.
System.out.println("Common element : " + (set));
System.out.println("Thanks for visiting https://onurdesk.com");
}
}
Write an array program in java to copy elements of one array into another array.
package com.onurdesk.array.program;
public class CopyAllElementsToAnotherArray {
/**
* @author Onurdesk
*
* Thanks for visiting https://onurdesk.com like us on facebook :
* https://facebook.com/onurdesk
*/
public static void main(String[] args) {
int[] array1 = { 3, 4, 5, 6, 7, 8, 9 };
int[] array2 = new int[array1.length];
for (int i = 0; i < array1.length; i++) {
array2[i] = array1[i];
System.out.print(array2[i] + " ");
}
System.out.println();
System.out.println("Thanks for visiting https://onurdesk.com");
}
}
Write an array program in java to copy arrays.
package com.onurdesk.array.program;
import java.util.Arrays;
public class CopyOfArrays {
/**
* @author Onurdesk
*
* Thanks for visiting https://onurdesk.com like us on facebook :
* https://facebook.com/onurdesk
*/
public static void main(String[] args) {
String[] s = { "alex", "smith", "robin", "ward" };
String[] s1 = s.clone();
System.out.println(Arrays.toString(s));
System.out.println(Arrays.toString(s1));
System.out.println("Thanks for visiting https://onurdesk.com");
}
}
Write an array program in java to find duplicate elements in array.
package com.onurdesk.array.program;
public class DuplicateElementsInArray {
/**
* @author Onurdesk
*
* Thanks for visiting https://onurdesk.com like us on facebook :
* https://facebook.com/onurdesk
*/
public static void main(String[] args) {
int a[] = { 1, 4, 5, 6, 7, 8, 3, 1, 4, 5 };
duplicateElements(a);
System.out.println("Thanks for visiting https://onurdesk.com");
}
public static void duplicateElements(int[] a) {
System.out.println("Duplicate Elements are...");
for (int i = 0; i < a.length; i++) {
for (int j = i + 1; j < a.length; j++) {
if (a[i] == a[j])
System.out.print(a[i] + " ");
}
}
}
}
Write an array program in java to find the largest element in array.
package com.onurdesk.array.program;
public class LargestElementInArray {
/**
* @author Onurdesk
*
* Thanks for visiting https://onurdesk.com like us on facebook :
* https://facebook.com/onurdesk
*/
public static void main(String[] args) {
int a[] = { 24, 3, 5, 46, 56, 78, 12, 3, 44 };
largestElement(a);
System.out.println("Thanks for visiting https://onurdesk.com");
}
public static void largestElement(int a[]) {
int max = a[0];
for (int i = 0; i < a.length; i++) {
if (a[i] > max) {
max = a[i];
}
}
System.out.println("Maxmium element in array is " + max);
}
}
Write an array program to print the elements of the array.
package com.onurdesk.array.program;
import java.util.Arrays;
public class PrintArrays {
/**
* @author Onurdesk
*
* Thanks for visiting https://onurdesk.com like us on facebook :
* https://facebook.com/onurdesk
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int a[] = { 1, 34, 55 };
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]);
}
System.out.println();
System.out.println("Thanks for visiting https://onurdesk.com");
}
}
Write an array program to print the content of the array.
package com.onurdesk.array.program;
import java.util.Arrays;
public class PrintContentOfArray {
/**
* @author Onurdesk
*
* Thanks for visiting https://onurdesk.com like us on facebook :
* https://facebook.com/onurdesk
*/
public static void main(String[] args) {
String[] arr = { "apple", "banana", "orange" };
System.out.println(Arrays.toString(arr));
System.out.println("Thanks for visiting https://onurdesk.com");
}
}
Write an array program to print 2D array.
package com.onurdesk.array.program;
import java.util.Arrays;
public class Prrint2DArray {
/**
* @author Onurdesk
*
* Thanks for visiting https://onurdesk.com like us on facebook :
* https://facebook.com/onurdesk
*/
public static void main(String[] args) {
int[][] a = { { 2, 3 }, { 5, 4 }, { 2, 4, 7 } };
// with out method
System.out.println(Print2DArray(a));
System.out.println("Thanks for visiting https://onurdesk.com");
}
public static String Print2DArray(int a[][]) {
StringBuilder sb = new StringBuilder();
sb.append("{");
for (int i = 0; i < a.length; i++) {
sb.append(Arrays.toString(a[i]) + " ,");
}
sb.deleteCharAt(sb.length() - 1);
sb.deleteCharAt(sb.length() - 1);
sb.append("}");
return sb.toString();
}
}
Write an array program to remove duplicate elements of array.
package com.onurdesk.array.program;
import java.util.Scanner;
public class RemoveDuplicateElementFromArray {
/**
* @author Onurdesk
*
* Thanks for visiting https://onurdesk.com like us on facebook :
* https://facebook.com/onurdesk
*/
public static void main(String[] args) {
/* Array instantiation */
int[] arr_elements = new int[20];
/*
* initial_element variable initialize by 0 and point to the first element of
* the array
*/
/* next_element points to next element of array */
int j, k;
int i;
/* Create Scanner Object */
Scanner sc = new Scanner(System.in);
/* Display array size for user */
System.out.print("Enter array size: ");
int arr_size = sc.nextInt();
/* Display message for array element */
System.out.println("Read Array Elements From User :");
/* Loop to take input array elements */
for (i = 0; i < arr_size; ++i) {
System.out.print("Enter array elements of index " + i + ": ");
arr_elements[i] = sc.nextInt();
}
/* Display array before removing duplicate element */
System.out.println("Before removing duplicate element array are :");
/* Loop for displaying array elements */
for (i = 0; i < arr_size; ++i) {
System.out.println(arr_elements[i]);
}
/*
* Get new line System.out.println(); /* Display array after removing duplicate
* array element
*/
System.out.println("After removing duplicate element array are :");
for (j = 0; j < arr_size; ++j) {
for (k = j + 1; k < arr_size;) {
/*
* if initial_element matches to next_element then take next _element and
* matches till end
*/
if (arr_elements[j] == arr_elements[k]) {
for (int temp = k; temp < arr_size; ++temp) {
arr_elements[temp] = arr_elements[temp + 1];
}
arr_size = arr_size - 1;
} else
k++;
}
}
/* Loop to display array after removing duplicate element */
for (i = 0; i < arr_size; ++i)
System.out.println(arr_elements[i]);
System.out.println("Thanks for visiting https://onurdesk.com");
}
}
Write an array program to reverse array.
package com.onurdesk.array.program;
public class ReverseAnArray {
/**
* @author Onurdesk
*
* Thanks for visiting https://onurdesk.com like us on facebook :
* https://facebook.com/onurdesk
*/
public static void main(String[] args) {
int a[] = { 1, 2, 6, 2, 5, 12, 5, 12, 1, 5, 12 };
System.out.println("Original array");
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
System.out.println("Reverse array");
for (int i = a.length - 1; i > 0; i--) {
System.out.println(a[i]);
}
System.out.println("Thanks for visiting https://onurdesk.com");
}
}
Write an array program to reverse array (Type2).
package com.onurdesk.array.program;
import java.util.Arrays;
public class ReverseArrayElements {
/**
* @author Onurdesk
*
* Thanks for visiting https://onurdesk.com like us on facebook :
* https://facebook.com/onurdesk
*/
public static void main(String[] args) {
int[] arr = { 1, 2, 3, 5, 6, 7, 8, 9 };
int size = arr.length;
System.out.println("Length of array is " + size);
int[] rev = new int[size];
for (int i = 0; i < size; i++) {
rev[i] = arr[size - 1 - i];
}
System.out.println("Actual array is " + Arrays.toString(arr));
System.out.println("Reverse array is " + Arrays.toString(rev));
System.out.println("Thanks for visiting https://onurdesk.com");
}
}
Write an array program to find smallest number in array.
package com.onurdesk.array.program;
/**
* @author Onurdesk
*
* Thanks for visiting https://onurdesk.com like us on facebook :
* https://facebook.com/onurdesk
*/
public class SmallestElementInArray {
/**
* @param args
*/
public static void main(String[] args) {
int a[] = { 24, 3, 5, 46, 56, 78, 12, 3, 44 };
smallestElementInArray(a);
}
public static void smallestElementInArray(int a[]) {
int min = a[0];
for (int i = 0; i < a.length; i++) {
if (a[i] < min) {
min = a[i];
}
}
System.out.println("Smallest element in array is " + min);
System.out.println("Thanks for visiting https://onurdesk.com");
}
}
Write an array program to sort array in descending order.
package com.onurdesk.array.program;
import java.util.Scanner;
class DesendingOrder {
/**
* @author Onurdesk
*
* Thanks for visiting https://onurdesk.com like us on facebook :
* https://facebook.com/onurdesk
*/
/**
* @param a
* @param n
*/
/**
* @param a
* @param n
*/
public static void desendingOrder(int[] a, int n) {
int temp = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (a[i] < a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
System.out.println("Descending Order");
for (int c : a) {
System.out.println(c);
}
}
}
public class SortArrayDesendingOrder {
public static void main(String[] args) {
DesendingOrder d = new DesendingOrder();
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of an array");
int size = sc.nextInt();
System.out.println("Enter array elements....");
int[] arr = new int[size];
for (int i = 0; i < size; i++) {
arr[i] = sc.nextInt();
}
d.desendingOrder(arr, size);
System.out.println("Thanks for visiting https://onurdesk.com");
}
}
Write an array program to sort the array in ascending order.
package com.onurdesk.array.program;
import java.util.Scanner;
public class SortArrayInAscendingOrder {
public static void arrayAscending(int[] arr, int n) {
int temp = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
System.out.println("Ascending Order:");
for (int i = 0; i < n; i++) {
System.out.println(arr[i]);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of an array");
int n = sc.nextInt();
System.out.println("enter array elemnts");
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
arrayAscending(arr, n);
}
}
Write an array program to convert a string into a string array.
package com.onurdesk.array.program;
import java.util.Arrays;
public class StingIntoStringArray {
public static void main(String[] args) {
String s = "I am wirting coding";
String[] words = s.split("6");
System.out.println(s);
System.out.println(words);
System.out.print(Arrays.toString(words));
}
}
Write an array program to convert string array into string.
package com.onurdesk.array.program;
public class StringArrayToString {
public static void main(String[] args) {
String[] arr = { "om prakash", "subani", "naveen" };
String s1 = String.join(" ", arr);
String s2 = String.join(",", arr);
System.out.println(arr);
System.out.println(s1);
System.out.println(s2);
}
}
Write an array program to Sum of Array.
package com.onurdesk.array.program;
public class SumOfArray {
public static void main(String[] args) {
int a[] = { 24, 3, 5, 46, 56, 78, 12, 3, 44 };
sumOfElement(a);
}
public static void sumOfElement(int a[]) {
int sum = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] > sum) {
sum += a[i];
}
}
System.out.println("Sum Of element are " + sum);
}
}