Category Archives: Java Array Programs

Java Array Programs

Java Program to reverse an array

Java Program to Reverse an Array.

In this tutorial, we will learn to create a Java program that will reverse an Array Using Java programming.

Prerequisites

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

  • Operators in Java Programming.
  • Basic Input and Output function in Java Programming.
  • Basic Java programming.
  • For loop in Java programming.
  • Arithmetic operations in Java Programming.

Reversing an Array:-

In this program, First, we will first take the size of the array from the user. Then we will take the elements of the array from the user. Then we will copy elements of the array. After that, we will reverse the array using the for-loop. Then we will print the reverse of the original array to the user.

With the help of this program, we can be reverse an Array.

Program Code:-

Output:-

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

  • array[] = it will hold the integer value.
  • i = it will hold the integer value.
  • j = it will hold the integer value.
  • size = it will hold the integer value.

Taking the size of the array and the elements.

Program Code to Reverse an Array.

Printing reversed array to the user.

Java Program to Count Duplicate Elements in Array

Java Program to Count Duplicate Elements in Array

In this tutorial, we will learn to create a Java program that will count the duplicate elements of an array using Java programming.

Prerequisites

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

  • Operators in Java Programming.
  • Basic Input and Output function in Java Programming.
  • Basic Java programming.
  • For loop in Java programming.
  • Arithmetic operations in Java Programming.

Algorithm:-

Printing the duplicate Elements in an Array.

In this program, First, we will first take the size of the array from the user. Then we will take the elements of the array from the user. After that, we will use the conditional statements and the loops to check the duplicate elements of the array. Then we will be using a for loop to print the duplicate elements of the array.

With the help of this program, we can be print the duplicate elements of the array.

Program Code:-

Output:-

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

  • no = it will hold the integer value.
  • i = it will hold the integer value.
  • j = it will hold the integer value.
  • temp = it will hold the integer value.

Taking the size of the array from the user.

Taking the input elements for the array.

Finding the duplicate number of elements of the array.

Printing output duplicate number of elements of the array.

Java Program to Add the elements of an Array

Java Program to Add the elements of an Array

In this tutorial, we will learn to create a Java program that will add the elements of an Array using Java programming.

Prerequisites

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

  • Operators in Java Programming.
  • Basic Input and Output function in Java Programming.
  • Basic Java programming.
  • For loop in Java programming.
  • Arithmetic operations in Java Programming.

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 add the Elements of 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 a for-loop. At last, we will print all the elements of the array to the user.

With the help of this program, we can add the Elements of an Array.

Program Code:-

Output:-

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

  • arry[] = it will hold the integer value of the input elements.
  • i = it will hold the integer value.
  • Size = it will hold the integer value of the input.

Input size of the array from the user.

Taking the elements of the array from the user.

Program Logic Code for the adding of the elements of the array.

Printing output array after adding the element to the array.

Java Program to Remove Element From Array

Java Program to Remove the elements of an Array

In this tutorial, we will learn to create a Java program that will remove the elements of an Array using Java programming.

Prerequisites

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

  • Operators in Java Programming.
  • Basic Input and Output function in Java Programming.
  • Basic Java programming.
  • For loop in Java programming.
  • Arithmetic operations in Java Programming.

Program description to find Remove the Elements of an Array.

In this program, we will first take the input array size and the elements from the user. Then we will take a position to remove an element of the array using a for-loop. At last, we will print the array to the user using the print function.

With the help of this program, we can Remove the Elements of an Array.

Program Code:-

Output:-

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

  • ps = it will hold the integer value of the input elements.
  • i = it will hold the integer value.
  • n = it will hold the integer value of the input.

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

Program Logic Code for the Remove of the elements of the array.

Printing the array after deletion of the element.

Java Program to Add Element to Array

Java Program to Add Element to Array

In this tutorial, we will learn to create a Java program that will add the elements to an Array using Java programming.

Prerequisites

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

  • Operators in Java Programming.
  • Basic Input and Output function in Java Programming.
  • Basic Java programming.
  • For loop in Java programming.
  • Arithmetic operations in Java Programming.

Algorithm:-

Program description to find add the Elements of 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 in the array using a for-loop. At last, we will print the added array to the user.

With the help of this program, we can add the Elements of an Array.

Program Code:-

 

Output:-

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

  • ele = it will hold the integer value.
  • len = it will hold the integer value.
  • i = it will hold the integer value.

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

Program Logic Code for the adding of the element in the array.

Printing array after adding the element in the array.

Java Program to Insert Element at Specific Position in Array

Java Program to Insert Element at Specific Position in Array

In this tutorial, we will learn to create a Java program that will insert an Element at a Specific Position in an Array using Java programming.

Prerequisites

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

  • Operators in Java Programming.
  • Basic Input and Output function in Java Programming.
  • Basic Java programming.
  • For loop in Java programming.
  • Arithmetic operations in Java Programming.

Algorithm:-

Program description:-

In this program, We will first take the size and the elements of the array. Then We will take the new element and its position from the user. At last, we will add the element in the array and will print that to the user.

With the help of this program, we can insert an Element at a Specific Position in the Array.

Program Code:-

Output:-

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

  • positionn = it will hold the integer value for the position.
  • n = it will hold the integer value.
  • i = it will hold the integer value.

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

Taking the new element and its position from the user to add in the array.

Printing array after adding the element in the array.

Java Program to Merge Two Sorted Arrays

Java Program to Merge Two Sorted Arrays

In this tutorial, we will learn to create a Java program that will Merge Two sorted Arrays using Java programming.

Prerequisites

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

  • Operators in Java Programming.
  • Basic Input and Output function in Java Programming.
  • Basic Java programming.
  • For loop in Java programming.
  • Arithmetic operations in Java Programming.

Algorithm:-

Merging Two Arrays.

In this program, First, we will declare arrays one and two for the program. Then we will find the combined length for the merged array. After that, we will merge both the array. Then we will print the elements of the merged two sorted arrays to the user with the help of the print function.

With the help of this program, we can merge Two Sorted Arrays in an Array.

Program Code:-

Output:-

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

  • x = it will hold the integer value.
  • y = it will hold the integer value.
  • z = it will hold the integer value.

Finding the size of the arrays.

Program Code to Merge Two Arrays.

Printing output of the program.

Java Program to Merge Two Arrays

Java Program to Merge Two Arrays

In this tutorial, we will learn to create a Java program that will Merge Two Arrays using Java programming.

Prerequisites

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

  • Operators in Java Programming.
  • Basic Input and Output function in Java Programming.
  • Basic Java programming.
  • For loop in Java programming.
  • Arithmetic operations in Java Programming.

What is Merging?

The merging means is adding two or more arrays into one. Merging can be done with a simple Java programming code.

Merging Two Arrays.

In this program, First, we will declare arrays one and two for the program. Then we will find the combined length for the merged array. After that, we will merge both the array. Then we will print the elements of the merged array to the user with the help of the print function.

With the help of this program, we can merge Two Arrays in an Array.

Program Code:-

Output:-

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

  • x = it will hold the integer value.
  • y = it will hold the integer value.
  • z = it will hold the integer value.

Finding the size of the arrays.

Program Code to Merge Two Arrays.

Printing output of the program.

Java Program to Compare Two Arrays

Java Program to Compare Two Arrays

In this tutorial, we will learn to create a Java program that will Compare Two Arrays using Java programming.

Prerequisites

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

  • Operators in Java Programming.
  • Basic Input and Output function in Java Programming.
  • Basic Java programming.
  • For loop in Java programming.
  • Arithmetic operators in Java Programming.

What is Compare?

In comparing, we compare two or more things point to point and find the common things or the differences in them.

Compare Two Arrays.

In this program, First, we will declare arrays one and two for the program. Then we will compare them from each other. Then, if they satisfy the conditional statements, they will print that the arrays are the same.  If not then satisfy the conditional statements then will print that the arrays are not the same. With the help of this program, we can Compare Two Arrays in an Array.

Program Code:-

 

Output:-

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

  • arr1  = it will hold the integer array.
  • arr2  = it will hold the integer array.
  • arr3  = it will hold the integer array.
  • arr4  = it will hold the integer array.

Condition to check for comparison.

Printing if the arrays are the same.

Printing if the arrays are not the same.

Java Program to find Sum of Two Arrays Elements

Java Program to find Sum of Two Arrays Elements

In this tutorial, we will learn to create a Java program that will Find the Sum of Two Arrays Elements using Java programming.

Prerequisites:-

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

  • Operators in Java Programming.
  • Basic Input and Output function in Java Programming.
  • Basic Java programming.
  • For loop in Java programming.
  • Arithmetic operations in Java Programming.

Program description to Find Sum of two array Elements.

In this program, we will first take the input array size of both arrays then we will check the size of both arrays. Now we will take the elements of both the array. Then we will add all the elements of both the array using the for a loop. At last, we will print that sum of both the arrays element using the print function.

With the help of this program, we can Find the Sum of Two Arrays Elements.

Program Code:-

Output:-

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

  • arry1[] = it will hold the integer value of the input elements.
  • arry2[] = it will hold the integer value of the input elements.
  • i = it will hold the integer value.
  • arry3[] = it will hold the integer value sum of the array.

Input size of the array 1 and 2 from the user.

Taking the elements of the array from the user.

Program Code to find the sum of two array elements.

Printing output sum of the two array elements.

Java Program to Find the Sum of Array

Java Program to Find the Sum of Array

In this tutorial, we will learn to create a Java program that will Find the Sum of an Array using Java programming.

Prerequisites:-

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

  • Operators in Java Programming.
  • Basic Input and Output function in Java Programming.
  • Basic Java programming.
  • For loop in Java programming.
  • Arithmetic operations in Java Programming.

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.

With the help of this program, we can Find the Sum of all Elements in an Array.

Program Code:-

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 Find Length of Array

Java Program to Find Length of Array

In this tutorial, we will learn to create a Java program that will Find the Length of an Array in Java programming.

Prerequisites

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

  • Operators in Java Programming.
  • Basic Input and Output function in Java Programming.
  • Basic Java programming.
  • For loop in Java programming.
  • Conditional Statements in Java programming.
  • Array in Java programming.

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.

Algorithm:-

Find Length or Size of an Array:-

As we all know, the array is a collection of similar data type elements. In an array, only one variable is declared which can store multiple values. First, will take the number of elements of an array from the user. Then will Find the Length or Size of an Array. And at last, we will print the Length or Size of an Array.

With the help of this program, we can print the Length or Size of an Array.

Program Code:-

Output:-

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

  • i = it will hold the integer value.
  • arr[] = it will hold the integer value.

Finding the length of the array.

Printing the length of the array.

Printing the elements of the array.