我需要連排序和使用數組列表賠率ArrayList的排序
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
public class SortedEvensAndOdds {
public static void main(String[] args) {
Random rnd = new Random();
ArrayList<Integer> evenPrint = new ArrayList<Integer>();
ArrayList<Integer> oddPrint = new ArrayList<Integer>();
int odd[] = new int[25];
int numOdd = 0;
int even [] = new int[25];
int numEven = 0;
for (int i=0;i<25;i++) {
int num = rnd.nextInt((100 - 1) + 1) + 1;
if (num%2 ==0) {
even[numEven] = num;
numEven++;
}
else {
odd[numOdd] = num;
numOdd++;
}
}
for (int i = 0; i < even.length; i++)
evenPrint.add(even[i]);
for (int i = 0; i < even.length; i++)
oddPrint.add(odd[i]);
Collections.sort(evenPrint);
Collections.sort(oddPrint);
System.out.println("Even:");
for (int i=0;i<numEven;i++)
System.out.print(evenPrint.get(i) + " ");
System.out.println("\nOdd:");
for (int i=0;i<numOdd;i++)
System.out.print(oddPrint.get(i) + " ");
}
}
這是我的輸出
Even:
0 0 0 0 0 0 0 0 0 6 6 14 28 36 38 54
Odd:
0 0 0 0 0 0 0 0 0
如何防止所有0的
順便說一句,如果我拿出「Collections.sort()」方法將會是
Even:
16 32 22 54 90 70 50 60 40 12 60 78 86 52
Odd:
59 35 53 35 87 67 75 33 75 59 87
所以這告訴我的財產以後與分揀 去錯了,請幫忙