Category Archives: Java Array Programs

Java Array Programs

Java Program to Print 2D array

In this tutorial, we will learn to create a Java Program to Print 2D array using Java programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following Java programming topics:

  • Java Operators.
  • Basic Input and Output function in Java.
  • Class and Object in Java.
  • Basic Java programming.
  • If-else statements in Java.
  • For loop in Java.

What is 2D Array[]?

Multidimensional Arrays Or 2D array defined as in simple words as “array of arrays”.

For example:
The array int[][] a = new int[5][5] can store a total of (5*5) = 25 elements.

Java Program to Print 2D array

In this program we would simply print a “2d” array .first of we would declare variables and called a function to display all the value of array[].let have a look at the code

Output

In the above program, we have first declared and initialized a set variables required in the program.

  • arr[] = it will hold array values.
  • n= for iteration 

After declaring variables we pass the array to function Printing_String().

 

where we simply print  the array value using for loop.

 

Java Program to Print Odd and Even Numbers from an array

In this tutorial, we will learn to create a Java Program to Print Odd and Even Numbers from an array using java programming.

Prerequisites

Before starting with this tutorial, we assume that you are best aware of the following Java programming concepts:

  • Java Operators.
  • Basic Input and Output function in Java.
  • Class and Object in Java.
  • Basic Java programming.
  • If-else statements in Java.
  • For loop in Java.

What is Even and Odd Number.

A number which is  divisible by 2 is called even number (number%2==0) after dividing 2 remainder remain zero.
A number which is not divisible by 2 is called an odd number (number%2 !=0) after dividing 2 remainder remains.

Java Program to Print Odd and Even Numbers from an array

In this program we will find number in an array are even  or odd using if else statement and loop. We would first declared and initialized the required variables. Next,  we will find that number is even or odd.lets have a look at the code.

Output

Even and odd in an array.

In the above program, we have first declared and initialized a set variables required in the program.

  • arr= it will hold array values
  • i=  for iteration .

After declaring variables we initiate values in a an array[].

Algorithm

  1. Declare an array and an element in array[].
  2. within the loop find element even or odd applying condition separate for even a shown in image below.

  1. Print the even elements by checking the conditions using for loop.

  1. within the loop find element even or odd applying condition separate for odd a shown in image below.

this is the source code of the Java Program to Print the Odd & Even Numbers in an Array.

Java Program to Remove Duplicate Element in an array

In this tutorial, we will learn to create a Java Program to Remove Duplicate Element in an array using java programming.

Prerequisites

Before starting with this tutorial, we assume that you are best aware of the following Java programming concepts:

  • Java Operators.
  • Basic Input and Output function in Java.
  • Class and Object in Java.
  • Basic Java programming.
  • If-else statements in Java.
  • For loop in Java.

Java Program to Remove Duplicate Element in an array

In this program we will find duplicate element in an array if-else statement and loop. We would first declared and initialized the required variables. Next, we will find repeated  number in an array.lets have a look at the code.

Output

In the above program, we have first declared and initialized a set variables required in the program.

  • arr= it will hold array values
  • number and size=  it will hold length of an array.
  • i and j for iteration
  • temp = it will hold temporary value of an array.

After declaring variables we initiate values in  an array[].

and with the help length we find the length of an array.

Now we will pass arr[] and length to remove() method where we remove duplicate values within the array[].

Steps to remove duplicate elements

  1. Create a temp array temp[] to store unique values of elements.
  2. travel through array and copy all the unique elements of arr[] to temp[]. and count  unique elements.
  3. Copy j elements from temp[] to arr[].

and finally print sorted array.

Java to Program Find Smallest Number in an array

In this tutorial, we will learn to create a Java to Program Find 2nd Smallest Number in an array using java programming.

Prerequisites

Before starting with this tutorial, we assume that you are best aware of the following Java programming concepts:

  • Java Operators.
  • Basic Input and Output function in Java.
  • Class and Object in Java.
  • Basic Java programming.
  • If-else statements in Java.
  • For loop in Java.

Java to Program Find  Smallest Number in an array

In this program we will find smallest element in an array using nested for loop. We would first declared and initialized the required variables. Next, we will find smallest number in an array. Lets have a look at the code.

Output

Smallest in an array

In the above program, we have first declared and initialized a set variables required in the program.

  • arr[]= it will hold array values
  • l=  it will hold length of an array.
  • i and j for iteration
  • temp = it will hold temporary value of an array.

After declaring variables we initiate values in  an array[]

with the help of length function we  will find the length of an array .

after that with the help or nested for loop we will find smallest element in array as shown in image below

Smallest in an array

  • Here first we assign first value of an array to variable temp.
  • Compare the first two elements of the array.
  • If the value of first element is greater than the second exchange them.
  • Then, compare 2 value and 3 value elements if the second element value is greater than the 3 value swap them.
  • Repeat this till the end of the array[].
  • and finally print smallest in an array.

Java Program to Find Largest Number in an array

In this tutorial, we will learn to create a Java Program to Find Largest Number in an array using java programming.

Prerequisites

Before starting with this tutorial, we assume that you are best aware of the following Java programming concepts:

  • Java Operators.
  • Basic Input and Output function in Java.
  • Class and Object in Java.
  • Basic Java programming.
  • If-else statements in Java.
  • For loop in Java.

Java Program to Find Largest Number in an array

In this program we will find smallest element in an array using nested for loop. We would first declared and initialized the required variables. Next, we will find smallest number in an array. Lets have a look at the code.

Output

Largest in an array

In the above program, we have first declared and initialized a set variables required in the program.

  • arr[]= it will hold array values
  • l=  it will hold length of an array.
  • i and j for iteration
  • temp = it will hold temporary value of an array.

After declaring variables we initiate values in  an array[]

with the help of length function we  will find the length of an array .

after that with the help or nested for loop we will find smallest element in array as shown in image below

Largest in an array

  • Here first we assign first value of an array to variable temp.
  • Compare the first two elements of the array.
  • If the value of first element is smaller than the second exchange them.
  • Then, compare 2 value and 3 value elements if the second element value is smaller than the 3 value swap them.
  • Repeat this till the end of the array[].
  • and finally print largest in an array.

Java Program to Find 2nd Largest Number in an array

In this tutorial, we will learn to create a Java Program to Find 2nd Largest Number in an array using java programming.

Prerequisites

Before starting with this tutorial, we assume that you are best aware of the following Java programming concepts:

  • Java Operators.
  • Basic Input and Output function in Java.
  • Class and Object in Java.
  • Basic Java programming.
  • If-else statements in Java.
  • For loop in Java.

Java Program to Find 2nd Largest Number in an array

In this program we will find smallest element in an array using nested for loop. We would first declared and initialized the required variables. Next, we will find smallest number in an array. Lets have a look at the code.

Output

2nd Large

In the above program, we have first declared and initialized a set variables required in the program.

  • a[]= it will hold array values
  • l=  it will hold length of an array.
  • i and j for iteration
  • temp = it will hold temporary value of an array.

After declaring variables we initiate values in  an array[]

here we will call a function where we fin second greatest in an array[].

2nd Largest in an array

    • Compare the first two elements of the array
    • If the first element is greater than the second exchange them.
    • Then, compare 2nd and 3rd elements if the second element is greater than the 3rd swap them.
    • Repeat this till the end of the array.
    • and at last function return a[size-2];
    • which is the second largest in an array[].

and finally we will print the result it means the second largest in an  array[].

Java Program to Find 3rd Largest Number in an array

In this tutorial, we will learn to create a Java Program to Find 3rd Largest Number in an array using java programming.

Prerequisites

Before starting with this tutorial, we assume that you are best aware of the following Java programming concepts:

  • Java Operators.
  • Basic Input and Output function in Java.
  • Class and Object in Java.
  • Basic Java programming.
  • If-else statements in Java.
  • For loop in Java.

Java Program to Find 3rd Largest Number in an array

In this program we will find smallest element in an array using nested for loop. We would first declared and initialized the required variables. Next, we will find smallest number in an array. Lets have a look at the code.

Output

3rd Largest in an array[].

In the above program, we have first declared and initialized a set variables required in the program.

  • a[]= it will hold array values
  • l=  it will hold length of an array.
  • i and j for iteration
  • temp = it will hold temporary value of an array.

After declaring variables we initiate values in  an array[]

here we will call a function where we fin second greatest in an array[].

3rd Largest in an array

    • Compare the first two elements of the array
    • If the first element is greater than the second exchange them.
    • Then, compare 2nd and 3rd elements if the second element is greater than the 3rd swap them.
    • Repeat this till the end of the array.
    • and at last function return a[size-3];
    • which is the second largest in an array[].

and finally we will print the result it means the second largest in an  array[].

Java Program to sort the elements of an array in descending order

In this tutorial, we will learn to create a Java Program to sort the elements of an array in descending order using java programming.

Prerequisites

Before starting with this tutorial, we assume that you are best aware of the following Java programming concepts:

  • Java Operators.
  • Basic Input and Output function in Java.
  • Class and Object in Java.
  • Basic Java programming.
  • If-else statements in Java.
  • For loop in Java.

Sorting in a an array[]

Sorting elements means to arrange elements within the array in a certain specific order. either Ascending(increasing) or descending(decreasing) order.

Java Program to sort the elements of an array in ascending order

In this program we will take size of an array  and sort the elements of an array using nested for loop. We would first declared and initialized the required variables. Next, we will sort the elements in an array. Lets have a look at the code.

Output

Sorted elements in an array.

In the above program, we have first declared and initialized a set variables required in the program.

  • arr[]= it will hold array values
  • number =  it will hold length of an array.
  • i =  for iteration.
  • temp = it will hold temporary value of an array.

After declaring variables we initiate values in  an array[]

Elements will be sorted in Descending order .As shown in the image below.

Algorithm to sort array in descending order.

  1. Start the algo
  2. Declare an array[].
  3. Taking size and element of an array from user.
  4. Declared a temp variable to store the elements for swapping.

  1. Here first for loop will hold the elements and traverse through all the elements.

  1. Second for loop to compare with the remaining elements.
  2. Sort the elements by comparing.
  3. Display the updated array.
  4. Stop

The above program shows  sort an array in descending order using for loops .

Java Program to sort the elements of an array in ascending order

In this tutorial, we will learn to create a Java Program to sort the elements of an array in ascending order using java programming.

Prerequisites

Before starting with this tutorial, we assume that you are best aware of the following Java programming concepts:

  • Java Operators.
  • Basic Input and Output function in Java.
  • Class and Object in Java.
  • Basic Java programming.
  • If-else statements in Java.
  • For loop in Java.

Algorithm

  • STEP 1: START
  • 2: Ask user to enter size of an array.
  • 3:Then take elements of an  array
  • 4: iterate each value through loop.
  • 5 for (int i = 0; i < number; i++)
  •             {
  •            for (int j = i + 1; j < number; j++)
  •            {
  •            if(arr[i]>arr[j]) then
    temp = arr[i]
    arr[i]=arr[j]
    arr[j]=temp
  •            }
  •           }
  • STEP 6:  PRINT “Elements of array sorted in ascending order”
  • STEP 7: for (int i = 0; i < number – 1; i++)
  •              {
  •             System.out.print(arr[i] + “,”);
  •               }
  •              System.out.print(arr[number – 1]);
  • STEP 8: END

Java Program to sort the elements of an array in ascending order

In this program we will take size of an array  and sort the elements of an array using nested for loop. We would first declared and initialized the required variables. Next, we will sort the elements in an array. Lets have a look at the code.

Output

Sorted elements in an array.

In the above program, we have first declared and initialized a set variables required in the program.

  • arr[]= it will hold array values
  • l=  it will hold length of an array.
  • i and j for iteration
  • temp = it will hold temporary value of an array.

After declaring variables we initiate values in  an array[]

Elements will be sorted in a way that the smallest element will show on extreme left which in this case is 1. The largest element will show at extreme right which in this case is 63.As shown in the image below.

Steps to change elements.

  1. Compare elements with each other.
  2. Use nested for loop to keep track.
  3. Swap the elements if the first element is greater than the second element.
  4. this process followed till all the elements in array are sorted.

and finally we will print the sorted elements.

Java Program to right rotate the elements of an array

In this tutorial, we will learn to create a Java Program to right rotate the elements of an array using java programming.

Prerequisites

Before starting with this tutorial, we assume that you are best aware of the following Java programming concepts:

  • Java Operators.
  • Basic Input and Output function in Java.
  • Class and Object in Java.
  • Basic Java programming.
  • If-else statements in Java.
  • For loop in Java.

Rotate an array right means?

Array is said to be right rotated if elements in an array are moved to its right >> by one(1) position.

 

Example:

Array=> [1, 2, 3, 4, 5].
Rotate Rights=> [2, 3, 4, 5, 1].

Java Program to right rotate the elements of an array

In this program we will shift right element in an array using  for loop. We would first declared and initialized the required variables. Next, we will shift element of an array. Lets have a look at the code.

Output

Right rotated array[]

In the above program, we have first declared and initialized a set variables required in the program.

  • a[]= it will hold array values
  • rotby=  it will hold rotation position.
  • i       = for iteration
  • last = it will hold length of an array[].

After declaring variables we initiate values in  an array[]

after that we will call rightRotate(a,rotby) where we pass two arguments one array and another is rotate by value i.e how many time rotate the array to its right.

In an array, if rotby is “1” then, elements of an array moved to its right by one position it mean first element of array take second(2) position, the second element will be moved to the third position and so on. as shown down below->

Array=> [1, 2, 3, 4, 5].
Rotate Rights=> [2, 3, 4, 5, 1].

Finally print right rotated array .

Java Program to print sum of all the items of the array

In this tutorial, we will learn to create a Java Program to print sum of all the items of the array  using Java programming.

Prerequisites

Before starting with this tutorial, we assume that you are the best aware of the following Java programming topics:

  • Java Operators.
  • Basic Input and Output function in Java.
  • Class and Object in Java.
  • Basic Java programming.
  • If-else statements in Java.
  • For loop in Java.

What is An array?

The array is a collection of similar data types. An array can store multiple values with different indexes in memory by using a single variable. The array can be both single and multidimensional.

Program description to Find Sum of all Elements in an Array.

In this program, we will first take the input array size and the elements from the user. Then we will add all the elements of the array using the for loop. At last, we will print that sum of elements of the array using the print function.

Output:-

In the above program, we have first initialized the required variable.

  • sum = it will hold the integer value for the sum of numbers.
  • arry[] = it will hold the integer value of the input elements.
  • i = it will hold the integer value.
  • sz = it will hold the integer value of the input.

Input the number of elements and the elements of the array from the user.

Program Logic Code to find the sum of an array.

Printing output sum of the array elements.

Java Program to print number of elements present in an array

In this tutorial, we will learn to create a Java to Program Find 2nd Smallest Number in an array using java programming.

Prerequisites

Before starting with this tutorial, we assume that you are best aware of the following Java programming concepts:

  • Java Operators.
  • Basic Input and Output function in Java.
  • Class and Object in Java.
  • Basic Java programming.
  • If-else statements in Java.
  • For loop in Java.

Algorithm

  • STEP 1: start
  • 2: declare and initiate array = {1,2,3,4,5}.
  • 3: Find length using length function ex=> a.length
  • 4: exit.

Java Program to print number of elements present in an array

In this program we will find number of element in an array using length function. We would first declared and initialized the required variables. Next, we will find length of an array element in an array. Lets have a look at the code.

Output

In the above program, we have first declared and initialized a set variables required in the program.

  • a[]= it will hold array values.

After declaring variables we initiate values in  an array[].

In this our program, we will count  the element and print number of elements present in an array.

As we can see that number of elements present in an array can be found by calculating the length of given array. We can clearly see that length of a function is 5 so here we will use length function to calculate length.

So Length in above array is 5. Then number of elements present in the array are 5.

Finally we will print the result as shown in image above..