13hc
2017-07-27 21:45
/*
* 3.对其他类型泛型的List集合进行排序,以Student为例
*/
public void testSort3() {
List<Student> studentList = new ArrayList<Student>();
Random random = new Random();//要用到随机数,就先创建一个Random类型的对象studentList.add(new Student(random.nextInt(1000)+"","Mike")); studentList.add(new Student(random.nextInt(1000)+"","Angela")); studentList.add(new Student(random.nextInt(1000)+"","Lucy"));
请教各位慕友,在Student类中定义了含参构造器中的参数id是一个String类型,想问一下random.nextInt(1000)的返回值也是一个String类型吗?
如果是的话,那前面
/*1.通过Collections.sort()方法,对Integer泛型的List集合进行排序
* 创建衣蛾Intger泛型(泛型不能用基本类型,要就得使用对应的包装类)的List集合,插入十个100以内的不重复的随机整数
* 调用Collections.sort()方法对其进行排序
*/
public void testSort1() {
List<Integer> integerList = new ArrayList<Integer>();
//插入十个100以内的不重复整数
Random random = new Random();
Integer k;
for (int i =0;i<10;i++) {
do {
k = random.nextInt(100);
}while (integerList.contains(k));
integerList.add(k);
System.out.println("成功添加整数:"+k);
}Integer类型的k怎么可以接收String类型的返回值呢?到底该怎么解释?谢谢各位慕友!
nextInt返回的是int类型的,
random.nextInt(1000)+"",这个部分是讲它转化为一个String类型的方法。网上也有将int转化为String类型的几个方法,你可以看一下。
http://blog.csdn.net/memray/article/details/7312817/
random.nextInt(1000)返回的是int类型,你加了一个 + "" ,就是在一个空字符串前面拼贴了你的返回值,总体还是String类型的。应该是这样吧
Java入门第三季
409775 学习 · 4546 问题
相似问题