
- 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
Arrange Negative Array Elements Before Positive Elements 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 arrange all elements of array such that negative elements are present before positive elements.
If a number is greater than 0 then it is called as positive number, if less than 0 then it is called as negative number.
Note − The array must be an integer array.
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 {1, 2, -3, -4, 5, -6, 7, -8, 9}.
After rearranging the array, the result will be −
-8 -6 -4 -3 1 2 5 7 9
Instance-2
Suppose the original array is {8, 2, -6, 4, -11, 15, 27, -8, -9}
After rearranging the array, the result will be −
-11 -9 -8 -6 2 4 8 15 27
Instance-3
Suppose the original array is {12, 21, -31, -14, 56, 16, 17, -18, 9}
After rearranging the array, the result will be −
-31 -18 -14 9 12 16 17 21 56
Algorithm
Step 1 − Declare and initialize an integer array.
Step 2 − Sort the array in ascending order
Step 3 − Initialize for loop to print array elements
Step 4 − 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 arrange all elements of array such that negative elements are present before positive elements.
import java.util.*; public class Main { //main method public static void main(String[] args){ //Declare and initialize the array elements int[] arr = { 12, 21, -31, -14, 56, 16, 17, -18, 9 }; System.out.println("Original array: \n"+Arrays.toString(arr)); //sorting array in ascending order Arrays.sort(arr); System.out.println("Updated array: "); //for each loop to print array elements for (int e : arr) System.out.print(e + " "); } }
Output
Original array: [12, 21, -31, -14, 56, 16, 17, -18, 9] Updated array: -31 -18 -14 9 12 16 17 21 56
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 arrange all elements of array such that negative elements are present before positive elements.
import java.util.*; public class Main{ //main method public static void main(String[] args){ //Declare and initialize the array elements int[] arr = { 8, 2, -6, 4, -11, 15, 27, -8, -9 }; System.out.println("Original array: \n"+Arrays.toString(arr)); //call a user defined method negative_first(arr); } //user defined method public static void negative_first(int[] arr){ //sorting array in ascending order Arrays.sort(arr); System.out.println("Updated array: "); //for loop to print array elements for (int e : arr) System.out.print(e + " "); } }
Output
Original array: [8, 2, -6, 4, -11, 15, 27, -8, -9] Updated array: -11 -9 -8 -6 2 4 8 15 27
In this article, we explored how to arrange all elements of array such that negative elements are present before positive elements by using Java programming language.
- Related Articles
- Find count of positive and negative array elements in Java
- Find Count of Positive, Negative and Zero Elements in an Array in Java
- Positive elements at even and negative at odd positions (Relative order not maintained) in C++
- Java Program to convert positive int to negative and negative to positive
- Increment Negative and Decrement Positive Numbers by 1 in an Array in Java
- Check if array elements are consecutive in O(n) time and O(1) space (Handles Both Positive and negative numbers) in Python
- Sort subset of array elements in Java
- Find Array Elements Which are Greater than its Left Elements in Java?
- How to negate the positive elements of an integer array in C#?
- Can you assign an Array of 100 elements to an array of 10 elements in Java?
- Reduce a multi-dimensional array and add elements along negative axis in Numpy
- How to Alter Two Array Elements in Java
- Search elements in a sorted object array in Java
- Java program for Multiplication of Array elements.
- Positive, negative and zeroes contribution of an array in JavaScript
