In the previous article, we have seen Java Program to sort array alternatively based on highest average of even or odd elements of the array
In this article we are going to see how to replace all the negative integers with zero and all the positive integers with one using Java programming language.
Java Program to Replace all the Positive Elements of the Array with 1 and all the Negative Elements with 0
Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.
In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.
Declaration of an array:
dataType[] arrayName; (or) //Declaring an array dataType []arrayName; (or) dataType arr[];
Instantiation of an Array:
arrayName = new datatype[size]; //Allocating memory to array
Combining both Statements in One:
dataType[] arrayName = new dataType[size] //Declaring and Instantiating array
Initialization of an Array:
arrayName[index-0]= arrayElement1 //Initializing the array ... arrayName[index-s]= arrayElementS
Combining all Statements in One:
dataType arrayName[ ]={e1,e2,e3}; //declaration, instantiation and initialization
Let’s see different ways to replace all the negative integers with zero and all the positive integers with one.
Method-1: Java Program to Replace all the Positive Elements of the Array with 1 and all the Negative Elements with 0 By Static Initialization of Array Elements
Approach:
- Declare and initialize an array.
- Iterate over the array.
- If the current element is negative replace it with 0.
- Else, if it is positive replace it with 1.
Program:
public class Main { public static void main(String[] args) { //array declared int[] arr = { 12, 32, 45, -23, -1 }; // printing the array System.out.print("Original array: "); printArray(arr); // iterating over the array for (int i = 0; i < arr.length; i++) { // if the element is negative replace it with 0 if (arr[i] < 0) { arr[i] = 0; } // if the element is positive replace it with 1 else { arr[i] = 1; } } // printing the array System.out.print("Modified array: "); //calling printArray() method printArray(arr); } //method to print the array public static void printArray(int[] arr) { for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println(); } }
Output: Original array: 12 32 45 -23 -1 Modified array: 1 1 1 0 0
Method-2: Java Program to Replace all the Positive Elements of the Array with 1 and all the Negative Elements with 0 By Dynamic Initialization of Array Elements
Approach:
- Create scanner class object.
- Ask user for length of the array.
- Initialize the array with given size.
- Ask the user for array elements.
- Iterate over the array.
- If the current element is negative replace it with 0.
- Else, if it is positive replace it with 1.
Program:
import java.util.Scanner; public class Main { public static void main(String[] args) { // create scanner class object Scanner sc = new Scanner(System.in); // take input from user for array size System.out.print("Enter the size of array: "); int n = sc.nextInt(); // initialize array with size n int[] arr = new int[n]; // take input from user for array elements System.out.print("Enter array elements: "); for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } // printing the array System.out.print("Original array: "); printArray(arr); // iterating over the array for (int i = 0; i < arr.length; i++) { // if the element is negative replace it with 0 if (arr[i] < 0) { arr[i] = 0; } // if the element is positive replace it with 1 else { arr[i] = 1; } } // printing the array System.out.print("Modified array: "); printArray(arr); } //method to print the array public static void printArray(int[] arr) { for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println(); } }
Output: Enter the size of array: 5 Enter array elements: 1 -2 -3 4 5 Original array: 1 -2 -3 4 5 Modified array: 1 0 0 1 1
Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.
Related Java Programs: