1

My array is setup to contain a string in every index. This string is a representation of an airline seating chart.

for(int i = 0; i < 7; i++)
{
    airline[i] = (i+1) + " AB CD";
}

After the user has input a char value for the row and column they want to reserve, I should be replacing the seat with an 'X'. For example, they input: row: 1 column: A, at 1A, I should replace the A with an X so it cannot be checkout again by another user. I am using:

    airline[i].replaceAll('A', 'X');

After a print statement of the array, I notice nothing is changing.

Mariela
  • 19
  • 2

1 Answers1

4

String is immutable. You need to reassign it to see the change, otherwise the change happens and you never see it.

 airline[i]= airline[i].replaceAll('A', 'X');

And you can check that in replaceAll() method. It returns a new String

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307