Reputation: 151
I have an array of Strings, and when I initialized the array, i made all the entries "".
later, I need to find the first position in the array where the entry is " and change it to my variable tag. The problem is, whenever I run my code, it changes all the entries to tag.
String[] list = new String[100];
for(int i = 0; i < list.length; i++){
list[i] = "";
}
//Other method
for(int i = 0; i < list.length; i++){
if(list[i].equals("")){
list[i] = tag;
}
}
This is part of a homework assignment, so I can't use any predefined java API structures or import most things.
Upvotes: 0
Views: 42
Reputation: 273
You have to break
from the loop after the first change to avoid changing all the entries to tag
:
//Other method
for(int i = 0; i < list.length; i++){
if(list[i].equals("")){
list[i] = tag;
break;
}
}
Upvotes: 3