-2

Why we can't just insert the mat, cou, stat, cg into the ArrayList's parentheses ? Why do we need to insert the object which stu into the parentheses?

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);

}
Dada
  • 6,313
  • 7
  • 24
  • 43
naddy
  • 29
  • 5
  • Because is not an `ArrayList` of `` is an `ArrayList` of ``, you can only add istances of the class Student class, it is not raw Strings. – Woohaik Nov 03 '21 at 01:14
  • Welcome to SO. This isn't valid java that you've written. Can you fix all your syntax errors in the code? Otherwise it's difficult to understand what you're trying to achieve. – sprinter Nov 03 '21 at 01:16
  • You can find answer here: [Initialization of an ArrayList in one line](https://stackoverflow.com/questions/1005073/initialization-of-an-arraylist-in-one-line) – Aliaksei A. Shysh Nov 03 '21 at 01:17

1 Answers1

1

We are adding the object Student named stu into an array list containing students. The values mat, cou,stat and cg are values stored inside of the specific student stu. If we were to put ArrList(mat) this would be attempting to add a value where an object should be held.

Dada
  • 6,313
  • 7
  • 24
  • 43
Cody
  • 21
  • 2
  • ohh I see, so it stores a bunch of data right into the arrayList. Not one data into the arrayList ? – naddy Nov 03 '21 at 01:34