×

Java Programs

Java Practice

Java Array Programs: 35+ Best Examples on Java Arrays

This section contains solved programs on arrays: Java one-dimensional programs, Java two-dimensional arrays, etc. with solved code, output, and explanation. By IncludeHelp Last updated : December 23, 2023

Java Array Programs with Solutions, Outputs, and Explanations

Below are the list of best Java array programs, examples, and exercises. Practice these programs to learn Java arrays.

Read 'N' array elements, and find sum of all elements using java program.

Input/Output:

First run:
Enter the elements you want : 3
Enter the elements:
55
21
14
Sum of array elements is :90

Second run:
Enter the elements you want : 5
Enter the elements:
12
45
36
25
88
Sum of array elements is :206

In this java program, we are reading total number of elements (N) first, and then according the user input (value of N)/total number of elements, we are reading the elements. Then sorting elements of array in descending order and then printing the elements which are sorted in descending order.

Input/Output:

Enter number of elements you want : 10
Enter all the elements:
55
25
36
99
12
9
55
88
66
47
Descending Order:
99
88
66
55
55
47
36
25
12
9

In this java program, we are reading total number of elements (N) first, and then according the user input (value of N)/total number of elements, we are reading the elements. Then sorting elements of array in ascending order and then printing the elements which are sorted in ascending order.

Input/Output:

Enter the elements you want : 10
Enter all the elements:
12
25
99
66
33
8
9
54
47
36
Ascending Order:
8
9
12
25
33
36
47
54
66
99

Given two matrices and find their multiplication in third matrix and print the matrix using Java program.

Input/Output:

Enter the base the matrices : 5

Enter the elements of 1st Matrix row wise 

12 45 56 85 25
12 41 20 25 65
36 54 52 58 68 
32 12 20 12 32
45 87 45 65 35 
Enter the elements of 2nd Matrix row wise 

45 65 25 35 14
12 12 12 12 12
32 31 36 35 34
52 51 58 57 59 
63 65 69 68 64

Multiplying both the matrices...
The product of the matrices is : 
8867  9016  9511  9465  9227 
7067  7392  7447  7457  6975 
11232 11978 11476 11658 10694 
4864  5536  4568  4824  4028 
10094 10954 9974  10279 9279 

Given two matrices, we have to find and print their subtraction using Java program.

Input/Output:

Enter the base of the matrices : 3
Enter the elements of the first matrix : 
6 7 8
9 8 7
6 7 8
Enter the elements of the second matrix : 
6 6 8
8 8 6
6 6 8
Subtracting Matrices --Matrix1 - Matrix2--
Result of Matrix1 - Matrix2 is : 
0 1 0 
1 0 1 
0 1 0

A matrix in which most of the elements are '0' then it is said to be a sparse matrix. Sparse matrices are used in specific ways in computer science and have different storage and techniques related to their use.

Enter the dimensions of the matrix : 3 3
Enter the elements of the matrix : 
1 1 1
0 0 0 
1 1 1
The matrix is not a sparse matrix

Given two integer arrays and we have to find common integers (elements) using java program.

Input/Output:

Array1 : [1993, 1995, 2000, 2006, 2017, 2020]
Array2 : [1990, 1993, 1995, 2010, 2016, 2017]
Common element is : 1993
Common element is : 1995
Common element is : 2017

Given two integer arrays and we have to find common integers (elements) using java program.

Input/Output:

Array1 : [1993, 1995, 2000, 2006, 2017, 2020]
Array2 : [1990, 1993, 1995, 2010, 2016, 2017]
Common element is : 1993
Common element is : 1995
Common element is : 2017

Two string arrays are given and we have to find common strings (elements) using java program.

Input/Output:

Array1 : [C, C++, C#, JAVA, SQL, ORACLE]
Array2 : [MySQL, SQL, Android, ORACLE, PostgreSQL, DB2, JAVA]
Common element : [JAVA, ORACLE, SQL]

Given an array of integers (in a series) and we have to find its missing elements (there will be a missing element) using java program.

Input/Output:

Input array: 1, 2, 3, 4, 6, 7
Output:
Missing element is: 5

Given an array of integers and we have to find their average using java program.

Input/Output:

Enter number of elements you want in array : 10
Enter all the elements : 
65
45
25
65
84
74
96
74
15
36
Sum of the array elements is : 579
Average of the array elements is : 57.9

Given an integer array with zeros (0’s) and we have to move all zeros at the end of the array using java program.

Input/Output:

Input array: 5, 1, 6, 0, 0, 3, 9, 0, 6, 7, 8, 12, 10, 0, 2
After moving 0 at the end 
Output array: 5, 1, 6, 3, 9, 6, 7, 8, 12, 10, 2, 0, 0, 0, 0

Given an array and an element to delete and we have to delete it from array using java program.

Input/Output:

Input:
Given array (elements will be read in program): 10 20 30 40 50
Enter element to delete: 40
Output:
Array elements after deleting the element: 10 20 30 50

Given a one dimensional array and we have to print its EVEN and ODD elements separately.

Input/Output:

Input:
Given array (elements will be read in program): 10 11 12 13 14
Output:
Odd numbers in the array are : 10 12 14
Even numbers in the array are : 11 13 

Given two one-dimensional arrays and we have to merge them using java program.

Input/Output:

Input:
Array 1 (elements will be read in program): 1 2 3 4 5 6 7 8 9 10
Array 2 (elements will be read in program): 11 12 13 14 15
Output:
New array (After merging elements)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Given an array and we have to sort its elements in ascending order and print the sorted array using java program.

Input/Output:

Input:
Array (elements will be read in program): 25 54 36 12 48 88 78 95 54 55
Output:
Sorted List in Ascending Order : 
12  25  36  48  54  54  55  78  88  95  

Read number of rows and columns, array elements for two dimensional array and print in matrix format using java program.

Input/Output:

Enter number of rows: 3
Enter number of columns: 3
Enter elements 
1
2
3
4
5
6
7
8
9

Output:
Matrix is:
1 2 3 
4 5 6 
7 8 9 

Given number of rows, cols and the characters we have to create a two dimensional array and fill all elements with the given characters using java program.

Input/Output:

Input
Enter size of the Array : 5 (rows are cols are same here)
Enter first character : @
Enter second character : *
Enter third character : #

Output
# @ @ @ # 
* # @ # * 
* * # * * 
* # @ # * 
# @ @ @ # 

Given number of rows and cols of a matrix and we have to fill it with the prime numbers using java program.

Input/Output:

Input:
Rows: 3
Cols: 3

Output:
Matrix:
2	3	5
7	11	13
17	19	23

Given a matrix and we have to print its boundary elements using java program.

Input/Output:

Enter the rows : 3
Enter the columns : 4

Enter the elements : 1
Enter the elements : 2
Enter the elements : 5
Enter the elements : 6
Enter the elements : 9
Enter the elements : 8
Enter the elements : 7
Enter the elements : 3
Enter the elements : 6
Enter the elements : 5
Enter the elements : 7
Enter the elements : 4

The Boundary Elements are:
1	2	5	6	
9	 	 	3	
6	5	7	4

If the sum of the left diagonal and right diagonal of the matrix is equal then the above matrix is said to be symmetric matrix. The entries of a symmetric matrix are symmetric with respect to the main diagonal.

Given a matrix and we have to check whether it is symmetric or not using Java program?

Input/Output:

    Enter the number of elements : 3

    Input the elements of the Matrix :
    Enter the elements : 11
    Enter the elements : 11
    Enter the elements : 11
    Enter the elements : 11
    Enter the elements : 11
    Enter the elements : 11
    Enter the elements : 11
    Enter the elements : 11
    Enter the elements : 11

    The Original Matrix is : 
    11	11	11	
    11	11	11	
    11	11	11	

    The given Matrix is Symmetric
    The sum of the left diagonal = 33
    The sum of the right diagonal = 33

A matrix is said to be lower matrix if all the elements above the diagonal of the given matrix are zero. So it is necessary that the elements must be zero.

Given a matrix and we have to check whether it is Lower Triangular Matrixor not?

Input/Output:

Enter the size of the matrix : 3
Enter an element : 0
Enter an element : 0
Enter an element : 0
Enter an element : 1
Enter an element : 0
Enter an element : 0
Enter an element : 1
Enter an element : 1
Enter an element : 0
*************************
The Matrix is : 
0	0	0	
1	0	0	
1	1	0	
*************************
The matrix is Lower Triangular

Given an array with strings and integers and we have to count strings and integers using java program.

Input/Output:

Input:
Array = {"Raj", "77", "101", "99", "Jio"}

Output:
Numeric:3
Strings:2

Given an array of integers and we have to remove duplicate elements using java program.

Input/Output:

Input array elements:
1, 2, 3, 1, 2, 3, 4

Output:
Elements after removing duplicates
1, 2, 3, 4

Given an array of N integers and we have to find its second largest element using Java program.

Input/Output:

Input:
Enter number of elements: 4
Input elements: 45, 25, 69, 40

Output:
Second largest element in: 45

Given an array of N integers and we have to find its second minimum/smallest element using Java program.

Input/Output:

Input:
Enter number of elements: 4
Input elements: 45, 25, 69, 40

Output:
Second smallest element in: 40

Given an array of N integers and we have to find its minimum/smallest element using Java program.

Input/Output:

Input:
Enter number of elements: 4
Input elements: 45, 25, 69, 40

Output:
Smallest element in: 25

Given an array of integers and we have to count total negatives, positives and zeros using java program.

Input/Output:

Input:
Array elements: 20, -10, 15, 00, -85

Output:
Positive Numbers are: 2
Negative Numbers are: 2
Zeros are: 1

In this program, we will create an array of characters and access its elements using for each loop and print it on the screen.

Input/Output:

Input:
char charArray[] = {'A', 'B', 'C', 'D', 'E', 'F'};

Output:
Array elements: 
A
B
C
D
E
F

In this program, we will create an array of 10 integers then we will find the length of the array using the length property.

Input/Output:

Input:
int[] intArr = new int[10];

Output:
Length of array is: 10

Given/input an integer array, we have to find prime and non-prime numbers in the array.

Input/Output:

Input:
int intArr[] = {10, 11, 13, 15, 17, 19, 23, 25, 30};

Output:
 10 - Not Prime
 11 - Prime
 13 - Prime
 15 - Not Prime
 17 - Prime
 19 - Prime
 23 - Prime
 25 - Not Prime
 30 - Not Prime

The linear searching algorithm is used to find the item in an array, This algorithm consists the checking every item in the array until the required item is found or till the last item is in the array. The linear search algorithm is also known as a sequential algorithm.

In this program, we will create an array of integers then we will search an item into the array using linear search and print position of the item in the array.

Input/Output:

Input:
Enter array elements: 10 20 30 40 50
Enter item to search: 40

Output:
Item found at index 3.

The binary searching algorithm is used to find the item in a sorted array with the minimum number of comparisons, in this algorithm key element compares with the middle index item.

In this program, we will create an array of sorted integers then we will search an item into an array using binary search and print the position of the item in the array.

Input/Output:

Input:
int arr[] = {10, 20, 30, 40, 50};

Output:
Enter item to search: 30
Item found at index 2.

In this program, we will create an array of sorted integers then we will search an item into an array using interpolation search and print the position of an item in the array.

Input/Output:

Input:
int arr[] = {10, 20, 30, 40, 50};

Output:
Enter item to search: 30
Item found at index 2.

In the Selection Sort technique, we find the smallest element and swap the smallest elements with the corresponding element to sort into ascending order.

In this program, we will create an array of integers then we will sort an array element in ascending order using selection sort.

Input/Output:

Input:
int arr[] = {14, 49, 79, 87, 78};

Output:
Sorted Array in ascending order: 
14 49 78 79 87
 

In the Selection Sort technique, we find the largest element and swap the largest elements with the corresponding element to sort into descending order.

In this program, we will create an array of integers then we will sort array elements in descending order using selection sort.

Input/Output:

Input:
int arr[] = {14, 49, 79, 87, 78};

Output:
Sorted Array in ascending order: 
14 49 78 79 87

In the bubble sort sorting technique, we compare adjacent elements and swap if they are in the wrong order. This process repeats until no more swaps are needed.

In this program, we will create an array of integers then we will sort array elements in ascending order using bubble sort.

Input/Output:

Input:
int arr[] = {14, 49, 79, 87, 78};

Output:
Sorted Array in ascending order: 
14 49 78 79 87

In the bubble sort sorting technique, we compare adjacent elements and swap if they are in the wrong order. This process repeats until no more swaps are needed.

In this program, we will create an array of integers then we will sort array elements in descending order using bubble sort.

Input:
int arr[] = {14, 49, 79, 87, 78};

Output:
Sorted Array in descending order: 
87 79 78 49 14

In this program, we will create an array of integers then we will sort array elements in ascending order using quicksort.

Input/Output:

Input:
int arr[] = {14, 49, 79, 87, 78};

Output:
Sorted Array in ascending order: 
14 49 78 79 87


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.