JAVA入门 - ArrayList - 产生随机数后去除重复
- 更新:2017-05-03 15:36:54
- 首发:2016-04-19 20:01:18
- 源代码
- 4911
三个简单的JAVA练习题。
- 建立一个元素类型java.util.Date的线性表,并在表中加入3个日期对象。
- 定义一个代表人员的类Person,含有三个数据成员(姓名,性别,年龄)。用TreeSet<Person>存储三个对象。
- 产生0-99的随机数100个,去掉重复的,还有多少个相互不同的数?
注意:因为虚拟机版本问题,在Win和Mac平台中有一小点不同。虽然Java是跨平台的,但不同版本的Java虚拟机对语法的要求还是有略微区别,理论上直接编译都是可以的,但强迫症的轶哥见不得IDE报错。
Win平台Main.java:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class Main {
public static void main(String[] args) throws ParseException {
//第一题
ArrayList<Date> list_one = new ArrayList<>();
list_one.add(new Date());//添加当前日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String strDate = "2016-4-19";
Date date = sdf.parse(strDate);//按照格式添加指定字符串日期
list_one.add(date);
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
list_one.add(sdf2.parse("2016-4-19 14:09:32"));//添加包含时分秒的日期
System.out.println(list_one);
//第二题
TreeSet<Person> t = new TreeSet<>();
t.add(new Person("张三", "男", 15));
t.add(new Person("李四", "女", 35));
t.add(new Person("王五", "男", 5));
System.out.println(t);
//第三题
Random r = new Random();
List<Integer> list = new ArrayList<>();
int i;
while (list.size() <= 100) {
i = r.nextInt(99);//生成0-99的随机数
//if(!list.contains(i)){ 如果这样判断,可以在添加的时候就去除重复的数
list.add(i);
//}
}
List<Integer> listTemp = new ArrayList<>();
Iterator<Integer> it = list.iterator();
while (it.hasNext()) {
int a = it.next();
if (listTemp.contains(a)) {
it.remove();
} else {
listTemp.add(a);
}
}
System.out.println("去掉重复的还剩下:" + list.size() + "个");
}
}
Mac平台Main.java:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class Main {
public static void main(String[] args) throws ParseException {
//第一题
ArrayList<Date> list_one = new ArrayList<Date>();
list_one.add(new Date());//添加当前日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String strDate = "2016-4-19";
Date date = sdf.parse(strDate);//按照格式添加指定字符串日期
list_one.add(date);
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
list_one.add(sdf2.parse("2016-4-19 14:09:32"));//添加包含时分秒的日期
System.out.println(list_one);
//第二题
TreeSet<Person> t = new TreeSet<Person>();
t.add(new Person("张三", "男", 15));
t.add(new Person("李四", "女", 35));
t.add(new Person("王五", "男", 5));
System.out.println(t);
//第三题
Random r = new Random();
List<Integer> list = new ArrayList<Integer>();
int i;
while (list.size() <= 100) {
i = r.nextInt(99);//生成0-99的随机数
//if(!list.contains(i)){ 如果这样判断,可以在添加的时候就去除重复的数
list.add(i);
//}
}
List<Integer> listTemp = new ArrayList<Integer>();
Iterator<Integer> it = list.iterator();
while (it.hasNext()) {
int a = it.next();
if (listTemp.contains(a)) {
it.remove();
} else {
listTemp.add(a);
}
}
System.out.println("去掉重复的还剩下:" + list.size() + "个");
}
}
不分平台 Person.java :
class Person implements Comparable<Person>{
private String name;
private String sex;
private int age;
Person(String name, String sex, int age){
this.name = name;
this.sex = sex;
this.age = age;
}
public String toString(){
return this.name + " " + this.sex + " " + this.age;
}
@Override
public int compareTo(Person o) {
return 1;
}
}
执行结果截图:
除特别注明外,本站所有文章均为原创。原创文章均已备案且受著作权保护,未经作者书面授权,请勿转载。
打赏
交流区(3)
学习了!牛逼哥
2016年4月22日 03:03回复
就是ArrayList尖括号里不同吗
2016年4月26日 13:14回复
恩。。语法上是这样。
2016年4月27日 09:46回复
如遇问题,手动修改上述代码中的版本号为最新版即可。
😄 祝您顺利!
那个设备管理器没有手动添加过时设备了,我直接就重新装了一遍
dcm
我心不死,又重新回来搞这个。
上面的都做好了,windows hello的安装程序闪退(就是闪退做不了),后面还把我sdk客户端里面的Camera Explorer又找不到我的照相机了,我哭死。
能提供一下思路吗,作者大大