【发布时间】:2021-12-17 11:07:27
【问题描述】:
为什么我们不能只将mat、cou、stat、cg 插入ArrayList 的括号中?为什么要在括号中插入stu的对象?
ArrayList arrList = new ArrayList ();
for ( int k = 1, k<101 , k++)
{
System.out.print ( "Matric : ") ;
String mat = sc.nextLine;
System.out.print ( "Course: ") ;
String cou = sc.nextLine;
System.out.print ( "Status: ") ;
String stat = sc.nextLine;
System.out.print ( "cgpa : ") ;
String cg = sc.nextDouble;
Student stu = new Student ( mat , cou, stat,cg);
ArrList add(stu);
}
【问题讨论】:
-
因为不是
<String, String, String, String>的ArrayList是<Student>的ArrayList,所以只能添加Student类的实例,它不是原始字符串。 -
欢迎来到 SO。这不是您编写的有效 java。你能修复代码中的所有语法错误吗?否则很难理解你想要达到的目标。
-
你可以在这里找到答案:Initialization of an ArrayList in one line
标签: java arrays object arraylist parentheses