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("Positi ve numbers :" + positive+ "\t");
}
System.out.println();
System.out.println("Array 2 :");
for (int negative: array1) {
if (negative >= -1) {
}else{ System.out.println("Negati ve numbers :" +negative);
}
}
System.out.println();
for (int i = 0; i < array1.length -1; i++) {
if (array1[/i + 1 ] == array1) {
System.out.println("Duplic ate 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!!!
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("Positi
}
System.out.println();
System.out.println("Array 2 :");
for (int negative: array1) {
if (negative >= -1) {
}else{ System.out.println("Negati
}
}
System.out.println();
for (int i = 0; i < array1.length -1; i++) {
if (array1[/i + 1 ] == array1) {
System.out.println("Duplic
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!!!
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 ?
why not try
go modern (Java 8 Streams) unless you need to implement sorting algorithm yourself..
good luck
List<Integer> list = Arrays.asList(array1);
list.stream()
.sorted()
.forEach(i -> System.out.println(i));
go modern (Java 8 Streams) unless you need to implement sorting algorithm yourself..
good luck
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;
}
}
}
}
Actually, I missed this bucketing requirement before, but there you go now:
[0, 0, 12, 12, 23, 43, 43, 545]
[-999, -87, -55, -22, -4]
[0, 12, 43]
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);
this gives you output:[0, 0, 12, 12, 23, 43, 43, 545]
[-999, -87, -55, -22, -4]
[0, 12, 43]
@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
ASKER
Thank you all
@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!)
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thx
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?