Avatar of Boncz Szilard
Boncz Szilard
 asked on

Java array sort

Here but not working correct me pls
Thank you!!!

public static void main(String[] args) {

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

    System.out.println("Array 1 :");

    Arrays.sort(array1);

    for (int positive: array1) {

        if (positive >= -1)
            System.out.println("Positive numbers :" + positive+ "\t");
        }
        System.out.println();
        System.out.println("Array 2 :");

        for (int negative: array1) {
            if (negative >= -1) {

            }else{ System.out.println("Negative numbers :" +negative);
            }

        }
        System.out.println();

        for (int i = 0; i < array1.length -1; i++) {
             if (array1[/i + 1 ] == array1) {
                 System.out.println("Duplicate element found :" + array1);
                 i = i + 1;

             }    
}
}
}




And a ineed this aoutputs


Starter array: 12 23 -22 0 43 545 -4 -55 43 12 0 -999 -87

Positive array: 12 23 0 43 545 43 12 0

Negative array: -22 -4 -55 -999 -87

Duplicate array :12 0 43

Thx!!!
JavaJava EE

Avatar of undefined
Last Comment
Boncz Szilard

8/22/2022 - Mon
James Bilous

check your if statements for checking for positive and negative.

Does a number being positive really mean its greater than or equal to -1?

Does a number being negative really also mean its greater than or equal to -1?

Also, you have your for loop check for negatives nested inside of your for loop check for positives. Should this be the case?
krakatoa

I offered to help on this exact question in your other previous question, and asked you to please put your code inside code tags, to help us read it more easily. Are you not able to follow these guidelines ?
Radek Baranowski

why not try

List<Integer> list = Arrays.asList(array1); 		

list.stream()
        .sorted()
        .forEach(i -> System.out.println(i));

Open in new window


go modern (Java 8 Streams) unless you need to implement sorting algorithm yourself..

good luck
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Boncz Szilard

ASKER
public static void main(String[] args) {

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

    System.out.println("Array 1 :");

    Arrays.sort(array1);

    for (int positive: array1) {

        if (positive >= -1)
            System.out.println("Positive numbers :" + positive+ "\t");
        }
        System.out.println();
        System.out.println("Array 2 :");

        for (int negative: array1) {
            if (negative >= -1) {

            }else{ System.out.println("Negative numbers :" +negative);
            }

        } 
        System.out.println();

        for (int i = 0; i < array1.length -1; i++) { 
             if (array1[/i + 1 ] == array1) {
                 System.out.println("Duplicate element found :" + array1);
                 i = i + 1;

             }     
}
} 
}

Open in new window

Radek Baranowski

Actually, I missed this bucketing requirement before, but there you go now:

List<Integer> list = Arrays.asList(array1);
		
List<Integer> positiveList = new ArrayList<Integer>();
List<Integer> negativeList = new ArrayList<Integer>();
List<Integer> duplicateList = new ArrayList<Integer>();
		
list.stream()
        .sorted()
        .forEach(i -> {	
        			if ((positiveList.contains(i) || negativeList.contains(i)) && !duplicateList.contains(i)) duplicateList.add(i);
        			if (i >= 0) {positiveList.add(i); System.out.println("found pos");}
        			if (i < 0) negativeList.add(i); 
        			});
		
		System.out.println(positiveList);
		System.out.println(negativeList);
		System.out.println(duplicateList);

Open in new window

this gives you output:

[0, 0, 12, 12, 23, 43, 43, 545]
[-999, -87, -55, -22, -4]
[0, 12, 43]
James Bilous

@Radek Just a side note - its actually not recommended to effect outside elements in streams as they are not thread safe and this could violate stateless requirements. It is better practice to filter and collect than to add to lists within a stream. Check out "side effects" in the java 8 documentation: https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Boncz Szilard

ASKER
Thank you all
Radek Baranowski

@James, thanks for this excellent observation! I'm actually an enthusiast of streams right now, and I seem to forget about some fundamentals yet :)

In this very case you just get what you want in one pass, while using Collect (which is final) would make you pass the given array three times. I guess you could  ensure thread-safety by some further trickery like synchronizing, or am I wrong? (whole different discussion I know but an exciting one!)
ASKER CERTIFIED SOLUTION
James Bilous

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Boncz Szilard

ASKER
thx
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy