3. 有一个数组 String arr[]={"abc","bad","abc","aab","bad","cef","jhi"};
a. 创建一个ArrayList,将数组里面的元素添加进去,但元素不能重复 (该数组不变)
b. 创建一个 TreeSet,将数组里面的元素按升序序存入该集合里,并且不能去重
c. 将数组里面的所有元素存入到项目根目录下的String.txt文件中
题目2中用到的产生双色球的工具类已经写好,可以直接使用
public class DoubleColorBallUtil {
//产生双色球的代码
public static String create() {
String[] red = {"01","02","03","04","05","06","07","08","09","10",
"11","12","13","14","15","16","17","18","19","20","21","22","23",
"24","25","26","27","28","29","30","31","32","33"};
/*//创建红球
for(int i=0;i<red.length;i++) {
char[] ch = {'0','0'};
String s = Integer.toString(i+1);//"1"
char[] num = s.toCharArray();//{'1'}
System.arraycopy(num, 0, ch, ch.length-num.length, num.length);
String ball = new String(ch);
red[i] = ball;
}*/
//System.out.println(Arrays.toString(red));//打印01-33
//创建蓝球
String[] blue = "01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16".split(",");
boolean[] used = new boolean[red.length];
Random r = new Random();
String[] all = new String[7];
for(int i = 0;i<6;i++) {
int idx;
do {
idx = r.nextInt(red.length);//0-32
} while (used[idx]);//如果使用了继续找下一个
used[idx] = true;//标记使用了
all[i] = red[idx];//取出一个未使用的红球
}
all[all.length-1] = "99";
//System.out.println(Arrays.toString(all));
Arrays.sort(all);
all[all.length-1] = blue[r.nextInt(blue.length)];
return Arrays.toString(all);
}
}
|
|