Я запутался, почему мой массив Object по-прежнему такой же.Я пытался посчитать индекс из массива.Как мне подсчитать индекс?
public class TryArray {
Students[] arr;
public void add(Students std) {
for (int i = 0; i < arr.length ; i++) {
arr[i] = std;
System.out.println(std); //it willbe more than one time displayed
break;
}
}
public TryArray(int lengthOfArray) { //Constructor for the length Array
arr = new Students[lengthOfArray];
}
}
А вот и мой основной класс test1.java
public class test1 {
public static void main(String[] args) {
TryArray val = new TryArray(4);
Students a1 = new Students(1, "Bob", "Julius");
Students a2 = new Students(2, "Joe", "Chris");
Students a3 = new Students(3, "Christian", "Henderson");
Students [] arr = {a1,a2,a3};
val.add(a1);
val.add(a2);
val.add(a3);
}
}
Ожидаемый результат:
1, Bob, Julius
2, Joe, Chris
3, Christian, Henderson
Я что-то пропустил?