Why is my Array item not changed in foreach?

Asked

Viewed 175 times

6

In the call of the method:

 AlunoPrivado aluno = new AlunoPrivado();
 aluno.addCursos("Portugues", "matemática", "história", "física");
 boolean result = aluno.changeCurso("Portugues", "ciências");

Implemented method:

public boolean changeCurso(String cursoModificar, String cursoNovo) {
    for(String curso : cursos) {
        if (curso.equals(cursoModificar) ) {
            curso = cursoNovo;
            return true;
        }
    }
    return false;
}

The method changeCurso() returns true but, in the Array, the course is not changed.

  • 1

    You changed the == for Equals()? Did it work? As I use more C#, I usually forget that detail. Confirm this and I reply.

  • I switched yes, but no funnel yet...

3 answers

10


Cannot restore the contents of an array or collection position via foreach. You have to use the for classic for this.

The Array, in a simplistic way, it is stored in memory as follows:

  • Each of the items(values) assigned to the Array are stored in memory positions.

    Endereço    Valor
     #00010  "Portugues"
     #00011  "matemática"
     #00013  "história", 
     #00014  "física"
    
  • At each position of Array a memory position is assigned that holds, not the value itself, but the address where the value was stored.

    Endereço    Valor  indice
     #00001     #00010   [0]
     #00002     #00011   [1]
     #00003     #00012   [2]
     #00004     #00013   [3]
    

What the variable of the foreach curso received is the reference(address) where the value corresponding to the relevant index is stored and not the address of the Array

When the condition curso.equals(cursoModificar) is true, the variable curso has as value(reference) the address #00010.

The variable cursoNovo also references a memory position, the one assigned to it when the method was called, e.g..: #00030.

When the line is executed curso = cursoNovo;, curso refers to the address #00030, the one where the string "sciences" is, however nothing was changed in the Array. The position [0], which is stored at the address #00001, continue to reference the address #00010, that continues to guard the String "Portugues".

Confused? Maybe not if you can think of addresses instead of values.

Note: Address values are indicative only.

5

You are modifying the value of the local variable 'course', just that, so it is not working.

You would have to modify the array value.

Kind of

for ( int i = 0; i < cursos.length; i++ )
{
    if ( cursos[i] == cursoModificar )
    {
        cursos[i] = cursoNovo;
        return true;
    }
}
return false;

I did this example keeping in mind that 'courses' is an array, if it is a list just modify the methods.

  • Hummm. But with a for each it would look like this method?

  • Read your @ramaral reply

1

If courses are an Arraylist try so:

public boolean changeCurso(String cursoModificar, String cursoNovo) {
    for(String curso : cursos) {
        if (curso.equals(cursoModificar)) {
            int index = aluno.cursos.indexOf(curso);
            aluno.cursos.remove(index);
            aluno.cursos.add(index,cursoNovo);
            return true;
    }
}
    return false; }
  • It’s not an Arraylist.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.