
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Increment Negative and Decrement Positive Numbers by 1 in an Array in Java
In Java, Array is an object. It is a non-primitive data type which stores values of similar data type.
As per the problem statement we have to increment all negative numbers by 1 and decrement all positive numbers by 1.
Let’s explore the article to see how it can be done by using Java programming language.
To Show You Some Instances
Instance-1
Suppose the original array is {12, 21, -31, -14, 56, 16, 17, -18, 9}
After updating the array element are: -30 -17 -13 8 11 15 16 20 55 (sorted)
Instance-2
Suppose the original array is {19, 11, -21, 14, 6, -16, 17, -18, 8}
After updating the array element are: -30 -17 -13 8 11 15 16 20 55 (sorted)
Instance-3
Suppose the original array is {1, 5, -3, 4, -5, 16, 17, -12, -9}
After updating the array element are: -11 -8 -4 -2 0 3 4 15 16 (sorted)
Algorithm
Step 1 − Declare and initialize an integer array.
Step 2 − Sort array in ascending order
Step 3 − Initialize for loop to print original array elements
Step 4 − Initialize another for loop to print updated array elements by adding 1 to negative array element and subtracting 1 from positive array elements.
Step 5 − Print the elements of the array.
Syntax
To get the length of an array (number of elements in that array), there is an inbuilt property of array i.e length.
Below refers to the syntax of it −
array.length
Where, ‘array’ refers to the array reference.
Multiple Approaches
We have provided the solution in different approaches.
By Using Static Initialization of Array
By Using User Defined Method
Let’s see the program along with its output one by one.
Approach-1: By Using Static Initialization of Array
Example
In this approach, array elements will be initialized in the program. Then as per the algorithm increment all negative numbers by 1 and decrement all positive numbers by 1.
import java.util.*; public class Main { //main method public static void main(String[] args){ //Declare and initialize the array elements int[] arr = { 1, 5, -3, 4, -5, 16, 17, -12, -9 }; //sorting array in ascending order System.out.print("Original array after sorting them in ascending order: "); System.out.print("\n"); //for each loop to print original array elements for (int e : arr) System.out.print(e + " "); System.out.print("\n"); System.out.print("Array after incrementing negative and decrementing positive numbers by 1: "); System.out.print("\n"); //for loop to print array updated elements for (int e : arr){ if(e < 0){ e = e + 1; System.out.print(e + " "); } else { e = e - 1; System.out.print(e + " "); } } } }
Output
Original array after sorting them in ascending order: 1 5 -3 4 -5 16 17 -12 -9 Array after incrementing negative and decrementing positive numbers by 1: 0 4 -2 3 -4 15 16 -11 -8
Approach-2: By Using User Defined Method
Example
In this approach, array elements will be initialized in the program. Then call a user defined method by passing the array as parameter and inside method as per the algorithm increment all negative numbers by 1 and decrement all positive numbers by 1.
import java.util.*; public class Main{ //main method public static void main(String[] args){ //Declare and initialize the array elements int[] arr = { 19, 11, -21, 14, 6, -16, 17, -18, 8 }; //calling user defined method func(arr); } //user defined method public static void func(int arr[]){ //sorting array in ascending order Arrays.sort(arr); System.out.print("Original array after sorting them in ascending order: "); System.out.print("\n"); //for each loop to print original array elements for (int e : arr) System.out.print(e + " "); System.out.print("\n"); System.out.print("Array after incrementing negative and decrementing positive numbers by 1: "); System.out.print("\n"); //for loop to print array updated elements for (int e : arr){ if(e < 0){ e = e + 1; System.out.print(e + " "); } else { e = e - 1; System.out.print(e + " "); } } } }
Output
Original array after sorting them in ascending order: -21 -18 -16 6 8 11 14 17 19 Array after incrementing negative and decrementing positive numbers by 1: -20 -17 -15 5 7 10 13 16 18
In this article, we explored different approaches to increment all negative numbers by 1 and decrement all positive numbers by 1 in an array by using Java programming language.
- Related Articles
- Increment and decrement operators in Java
- Interesting facts about Increment and Decrement operators in Java
- Split an array of numbers and push positive numbers to JavaScript array and negative numbers to another?
- Find Count of Positive, Negative and Zero Elements in an Array in Java
- Increment ++ and decrement -- Operators in C++
- Increment and Decrement Operators in C#
- Increment and Decrement Operators in Python?
- Increment ++ and Decrement -- Operator Overloading in C++
- Removal of negative numbers from an array in Java
- Find count of positive and negative array elements in Java
- What are the restrictions on increment and decrement operators in java?
- Reversing negative and positive numbers in JavaScript
- Python Increment and Decrement Operators
- Positive, negative and zeroes contribution of an array in JavaScript
- Distinguish positive and negative numbers.
