In the previous article, we have seen
Java Program to Move An Array Element From One Array Position to Another Position
In this article we will see how we can replace array elements based on some specific replacement condition using Java programming language.
Java Program to Replace Array Elements Based on Specific Replacement Condition
Prerequisite:
See below articles to know more about Array in Java, array declaration, array instantiation and array initialization.
- How to Declare an Array in Java?
- How to Instantiate an Array in Java?
- How to Initialize an Array in Java?
In this problem statement it is given that we have to replace the array elements based on specific replacement condition.
So the specified conditions are
- If last digit of the array element is 5 then replace the element with -5.
- If the array element is divisible by 3 then replace the element with -3.
- If the array element is divisible by 3 and last element is 5 then replace the element with -8.
Let’s see different ways to replace array elements based on above replacement condition.
Method-1: Java Program to Replace Array Elements Based on Specific Replacement Condition By Static Initialization of Array Elements
Approach:
- Declare an integer array say ‘
arr[]
‘ along with array elements. - Traverse the array and replace the array elements based on the replacement condition.
- Then print the result array.
Program:
import java.util.Scanner; public class Main { public static void main(String[] args) { //Array declared with elements int[] arr = {10,15,25,18,35,45,22,30,55}; //calling processArray() method to replace the array elemnts processArray(arr); } // processArray() user defined method which will replace the array elements //based on the replacement condition public static void processArray(int []arr) { //Replacing Array elements for (int i=0; i < arr.length; i++) { if(arr[i]%10 == 5 && arr[i]%3 == 0) { arr[i]=-8; } else if(arr[i]%10 == 5) { arr[i]=-5; } else if(arr[i]%3 == 0) { arr[i]=-3; } } System.out.println("After replacement array elements are:"); for (int i=0; i < arr.length; i++) { System.out.println(arr[i]+"\t"); } } }
Output: After replacement array elements are: 10 -8 -5 -3 -5 -8 22 -3 -5
Method-2: Java Program to Replace Array Elements Based on Specific Replacement Condition By Dynamic Initialization of Array Elements
Approach:
- Declare an integer array say ‘
arr[]
‘ . - Take the input of array elements from the user by using Scanner class.
- Then traverse the array and replace the array elements based on the replacement condition.
- Then print the result array.
Program:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter size of array:"); int size=sc.nextInt(); int[] arr = new int[size]; System.out.println("Enter values of array:"); for(int i=0; i < arr.length; i++) { int num=sc.nextInt(); arr[i] = num; } //calling processArray() method to replace the array elemnts processArray(arr); } // processArray() user defined method which will replace the array elements //based on the replacement condition public static void processArray(int []arr) { //Replacing Array elements for (int i=0; i < arr.length; i++) { if(arr[i]%10 == 5 && arr[i]%3 == 0) { arr[i]=-8; } else if(arr[i]%10 == 5) { arr[i]=-5; } else if(arr[i]%3 == 0) { arr[i]=-3; } } System.out.println("After replacement array elements are:"); for (int i=0; i < arr.length; i++) { System.out.println(arr[i]+"\t"); } } }
Output: Enter size of array: 8 Enter values of array: 10 18 25 22 15 35 45 20 After replacement array elements are: 10 -3 -5 22 -8 -5 -8 20
Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.
Related Java Programs: