问答详情
源自:6-6 学生选课---尝试对学生序列排序

这单元定义的student是student(String id,String name),而为什么老师在添加学生类过程中确是studentlist.add(new student(1+""+"小明"),为什么多添加了一个整型的参数却没有报错呢?

我认为正确的传参方式:

public void testStudentsort()

{

List<student>studentlist= new ArrayList<student>();

studentlist.add(new student("1","小明"));

studentlist.add(new student("2","小红"));

studentlist.add(new student("3","小绿"));

for (student student : studentlist) {

System.out.println("学生"+student.name);

}

}

老师添加学生类的代码

public void testStudentsort()

{

List<student>studentlist= new ArrayList<student>();

studentlist.add(new student(1+"","小明"));

studentlist.add(new student(2+"","小红"));

studentlist.add(new student(3+"","小绿"));

for (student student : studentlist) {

System.out.println("学生"+student.name);

}

}


提问者:福工刘德华 2017-11-05 20:41

个回答

  • 慕用0435620
    2017-11-05 22:03:42
    已采纳

    studentlist.add(new student(1+"","小明");  

     (1+"")这就是一个字符串啊,数字+""就转成字符串了,等同于"1"。


  • 爱开飞机的Kean
    2018-12-16 16:03:41

    int转String  :

    1. int i =1;

      String is =i+"";

    2. int i =1;

      String is =i.valueOf(i);

  • 饕餮土豆
    2017-11-05 22:02:58

    int + String 等于 String