I'm struggling to add a method in the class Vetor , that would be responsible for adding an object Aluno into the array, what would be the best implementation? I tried coming up with a few solutions but none worked so far and I don't know what to do
Right below this message is the class Vetor
import java.util.Arrays;
public class Vetor {
int i = 0;
private Aluno[] alunos = new Aluno[100];
public void adiciona(Aluno aluno) {
aluno = alunos[i];
i ;
}
public String toString() {
return Arrays.toString(alunos);
}
}
and this is the class Aluno
public class Aluno {
double id;
String nome;
public Aluno() {}
public Aluno(double id, String nome) {
this.id = id;
this.nome = nome;
}
public double getId() {
return id;
}
public void setId(double id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
}
and finally, my Main class
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Aluno a1 = new Aluno();
Aluno a2 = new Aluno();
a1.setNome("Rafael");
a2.setNome("Paulo");
Vetor lista = new Vetor();
lista.adiciona(a1);
lista.adiciona(a2);
System.out.println(lista);
}
}
CodePudding user response:
It should be alunos[i] = aluno
.
public void adiciona(Aluno aluno) {
alunos[i] = aluno;
i ;
}
And also, you need to handle the case when i is greater than or equal to 100 in that method.
CodePudding user response:
It should be alunos[i] = aluno
for assign alunos with index = i is aluno parameter on adiciona method.
Should use private Aluno[] alunos = new Aluno[];
for dynamic array. When index (i) >= 100, you cann't add aluno into alunos array because it is full.
CodePudding user response:
You have a bug in your adiciona()
method.
Here are the relevant lines of code:
int i = 0;
private Aluno[] alunos = new Aluno[100];
public void adiciona(Aluno aluno) {
aluno = alunos[i];
i ;
}
Your intention is that:
- the caller of the
adiciona()
method passes an object (aluno
, which is an instance of classAluno
) to the method - the method inserts that
aluno
object into the array, which you defined withAluno[] alunos
What's actually happening is:
- the method is looking up whatever is currently stored at
alunos[i]
, which will be nothing since you're starting out with an empty array; so specifically, it's evaluatingalunos[i]
and getting the valuenull
- it then assigns that "
null
' value toaluno
. This is a bit tricky.. the caller (whoever calledadiciona()
) created an object and also has a reference to that object. So there is an actual object instance, and a reference to the object. They're related, but not the same thing. When callingaidiciona()
, the caller passed the reference, and inside the method implementation foradiciona()
, it's usingaluno
to hold the reference. So when you doaluno = alunos[i]
, you're taking the local reference ofaluno
and giving it a new value, which isnull
. The actual object – whatever the caller sent when initially callingadiciona()
– that object is still around, no changes.
So, the fix is to do alunos[i] = aluno
, which will use the right-hand thing as the new value to store in the left-hand side of the expression. That is, it will save aluno
into your array at index i
.
Here's a modification of your original code should help illustrate what's going on, the only change is to add a bunch of println()
statements:
public void adiciona(Aluno aluno) {
Aluno newReferenceToOriginalObject = aluno;
System.out.println("before");
System.out.println(" aluno : " aluno);
System.out.println("newReferenceToOriginalObject : " newReferenceToOriginalObject);
System.out.println(" alunos[i] : " alunos[i]);
System.out.println();
aluno = alunos[i];
System.out.println("after");
System.out.println(" aluno : " aluno);
System.out.println("newReferenceToOriginalObject : " newReferenceToOriginalObject);
System.out.println(" alunos[i] : " alunos[i]);
i ;
}
From the output below, you can see that:
aluno
initially points to an objectAluno@24d46ca6
, but then it has valuenull
- the example variable
newReferenceToOriginalObject
is assigned to point at the original object, and still points there even afteraluno
is assignednull
- nothing changed with
alunos[i]
– it starts as null, and remains null
before
aluno : Aluno@24d46ca6
newReferenceToOriginalObject : Aluno@24d46ca6
alunos[i] : null
after
aluno : null
newReferenceToOriginalObject : Aluno@24d46ca6
alunos[i] : null