+ 5

Array sort in Java

I have an array with negative positive and 3 zeros, array length =13. So I need to put the neg values and positive in 2 other array. Cout zeros but not return them. Also show doubles and how many time are they present in the initial array.

7th Nov 2019, 1:33 PM
Cristina Dan
Cristina Dan - avatar
13 Antworten
+ 4
With a basic if else : initialize 3 arrays, take input by scanner into one array. you can use for loop as you have fixed length 13. Again use loop to traverse elements, and check if greater than zero (>0) assign to 2nd array, if less than zero, assign to 3rd array. Else increment a zero counter variable. After loop completion you have 2 arrays as you required. You half done. To count how many repeated, similar to above compare array elements with subsequent element if they same, increment counter, remove the element with assigning zero instead. This can need code more with if else and loops, can be simplified with advance topics like bitset.. If you know go with that.. Hoping this gives some idea, (As you asking to start point...).. Come with code if don't get.
7th Nov 2019, 2:36 PM
Jayakrishna 🇮🇳
+ 3
int[] array = {12, 0, -22, 0, 43, 545, -4, -55, 12, 43, 0, -999, -87}
7th Nov 2019, 1:39 PM
Cristina Dan
Cristina Dan - avatar
+ 3
Jup
7th Nov 2019, 1:49 PM
Cristina Dan
Cristina Dan - avatar
+ 2
Cristina Dan If you can post the part of your code or atleast the array elements, it would be useful to help you.
7th Nov 2019, 1:36 PM
Avinesh
Avinesh - avatar
+ 2
Now my question is what have you tried and have you something to share on how you want to go about it?
7th Nov 2019, 1:51 PM
Avinesh
Avinesh - avatar
+ 2
So I tried with for loop. I was thinking to use scanner input so we can use another numbers to. For my neg, pos, double and zeros I assign them variables.
7th Nov 2019, 1:55 PM
Cristina Dan
Cristina Dan - avatar
+ 2
The problems is that I dont know in which order to write the code
7th Nov 2019, 1:56 PM
Cristina Dan
Cristina Dan - avatar
+ 1
Method has to function with other numbers to
7th Nov 2019, 1:40 PM
Cristina Dan
Cristina Dan - avatar
+ 1
Yes
7th Nov 2019, 1:46 PM
Cristina Dan
Cristina Dan - avatar
+ 1
But dont return 0
7th Nov 2019, 1:46 PM
Cristina Dan
Cristina Dan - avatar
+ 1
Cristina Dan Ok great atleast you have made an attempt. And as you said we can use a Scanner class to fill the array but keep in mind that the size of the array cannot be changed.
7th Nov 2019, 2:00 PM
Avinesh
Avinesh - avatar
0
Cristina Dan So just see whether I have got the question right. You have to separate the positive and negative values and store them in 2 separate arrays. You also have to count the number of zeros. Also check whether a number is repeating in the array and if so than how many times. Did I get the question right?
7th Nov 2019, 1:45 PM
Avinesh
Avinesh - avatar
- 1
Cristina Dan so there is nothing returned from the method? Things are being stored and displayed that's it?
7th Nov 2019, 1:48 PM
Avinesh
Avinesh - avatar