有關 Random random = new Random();
/*
?*?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類型的返回值呢?到底該怎么解釋?謝謝各位慕友!
2017-07-27
nextInt返回的是int類型的,
random.nextInt(1000)+"",這個部分是講它轉化為一個String類型的方法。網上也有將int轉化為String類型的幾個方法,你可以看一下。
http://blog.csdn.net/memray/article/details/7312817/
2018-04-21
random.nextInt(1000)返回的是int類型,你加了一個 + "" ,就是在一個空字符串前面拼貼了你的返回值,總體還是String類型的。應該是這樣吧