ArrayList と for ループを使用して、文字列一致 "Meg" を削除しようとしています。これまでのところ、以下のコードを作成しましたが、なぜ機能しないのかわかりません。while
問題は以下のループにあると思います
while((customerName.get(i)).equals("Meg"))
{
customerName.remove(i);
}
前もって感謝します。
完全なコードは次のとおりです。
import java.util.ArrayList;
public class CustomerLister2
{
public static void main(String[] args)
{
ArrayList<String> customerName = new ArrayList<String>();
customerName.add("Chris");
customerName.add("Lois");
customerName.add("Meg");
customerName.add("Peter");
customerName.add("Stewie");
customerName.add(3, "Meg");
customerName.add(4, "Brian");
int currentSize = customerName.size();
for(int i = 0; i < currentSize - 1; i++)
{
while((customerName.get(i)).equals("Meg"))
{
customerName.remove(i);
}
}
for(String newStr: customerName)
{
System.out.println(newStr);
}
}
}