+3

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.

11/7/2019 1:33:41 PM

Cristina Dan

13 Answers

New Answer

+3

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.

+1

Cristina Dan If you can post the part of your code or atleast the array elements, it would be useful to help you.

+1

int[] array = {12, 0, -22, 0, 43, 545, -4, -55, 12, 43, 0, -999, -87}

+1

+1

Now my question is what have you tried and have you something to share on how you want to go about it?

0

Method has to function with other numbers to

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?

0

0

But dont return 0

0

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.

0

The problems is that I dont know in which order to write the code

0

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.

-1

Cristina Dan so there is nothing returned from the method? Things are being stored and displayed that's it?