Home > Back-end >  I'm having trouble with creating a method to add an object to an array
I'm having trouble with creating a method to add an object to an array

Time:06-25

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 class Aluno) to the method
  • the method inserts that aluno object into the array, which you defined with Aluno[] 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 evaluating alunos[i] and getting the value null
  • it then assigns that "null' value to aluno. This is a bit tricky.. the caller (whoever called adiciona()) 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 calling aidiciona(), the caller passed the reference, and inside the method implementation for adiciona(), it's using aluno to hold the reference. So when you do aluno = alunos[i], you're taking the local reference of aluno and giving it a new value, which is null. The actual object – whatever the caller sent when initially calling adiciona() – 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 object Aluno@24d46ca6, but then it has value null
  • the example variable newReferenceToOriginalObject is assigned to point at the original object, and still points there even after aluno is assigned null
  • 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
  • Related