【问题标题】:Why we have to insert data into ArrayList like this? [closed]为什么我们必须像这样将数据插入到 ArrayList 中? [关闭]
【发布时间】:2021-12-17 11:07:27
【问题描述】:

为什么我们不能只将matcoustatcg 插入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);

}

【问题讨论】:

  • 因为不是&lt;String, String, String, String&gt;ArrayList&lt;Student&gt;ArrayList,所以只能添加Student类的实例,它不是原始字符串。
  • 欢迎来到 SO。这不是您编写的有效 java。你能修复代码中的所有语法错误吗?否则很难理解你想要达到的目标。
  • 你可以在这里找到答案:Initialization of an ArrayList in one line

标签: java arrays object arraylist parentheses


【解决方案1】:

我们正在将名为 stu 的对象 Student 添加到包含学生的数组列表中。值matcoustatcg 是存储在特定学生stu 内部的值。 如果我们输入ArrList(mat),这将尝试在应该保存对象的位置添加一个值。

【讨论】:

  • 哦,我明白了,所以它将一堆数据直接存储到 arrayList 中。没有一个数据进入 arrayList ?
猜你喜欢
  • 2021-12-12
  • 2017-05-26
  • 1970-01-01
  • 2013-10-22
  • 1970-01-01
  • 2018-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多