|
1鱼币
package grammar;
import java.util.BitSet;
import java.util.Random;
public class LearnGrammar {
public static void main(String[] args){
int[] a = new int[100];
init(a);
disp(a);
System.out.println();
sort(a);
}
private static void sort(int[] a) {
// TODO Auto-generated method stub
BitSet bs = new BitSet(100);
for (int i = 0; i < 80; i++) {
bs.set(a[i],true);
}
for (int i = 0; i < bs.length(); i++) {
if(bs.get(i))
System.out.printf("%d ", i);
}
}
private static void disp(int[] a) {
// TODO Auto-generated method stub
for (int i = 0; i < 80; i++) {
System.out.printf("%d ", a[i]);
}
}
private static void init(int[] a) {
// TODO Auto-generated method stub
for(int i = 0;i < a.length; i++){
a[i] = i + 1;
}
Random random = new Random();
for (int i = 0; i < 80; i++) {
int index = random.nextInt(100 - i) + i;
swap(a,index,i);
}
}
private static void swap(int[] a, int index, int i) {
// TODO Auto-generated method stub
int temp = a[index];
a[index] = a[i];
a[i] = temp;
}
}
|
最佳答案
查看完整内容
这一句就会进行排序。
bs.set(a,true);
其实这一句还可以直接简写成bs.set(a);
具体的你查一下官方的文档吧。
|