Friday, April 24, 2015

Adding object

Student[] myStudents = new Student[5];
myStudents[0] = new Student(<parameters>);
myStudents[1] = new Student(<parameters>);

@Stefan Hoffmann is correct. However, if you still need to use an array you will need to do something like this.

List is a much better option since you can dynamically size the amount of students.  With an array you can only assign the position.  So the array you created has 5 positions 0 - 4 (always start with 0).  You can assign those positions like the code I provided.  If your position doesn't exist you will get an array out of bounds exception.  This is why List is the better object to use, because it will continue to grow or shrink based on your calls to List.Add() and List.Remove().

No comments:

Post a Comment