以下是我的代码。当我编译它,程序工作。但它给了我错误的输出。当我使用循环打印出arrayList的元素时,它会输出错误的输出。例如," Greenville,SC"在第一个if语句中应该被排除在外。 arraylist具有重复的值。
for (int i = 0; i < NoUber_cities.size(); i++)
{
if ((NoUber_cities.get(i)).equalsIgnoreCase("Greenville, SC"))
{
NoUber_cities.remove(i);
}
else if ((NoUber_cities.get(i)).equalsIgnoreCase("Pensacola, FL"))
{
NoUber_cities.remove(i);
}
else if ((NoUber_cities.get(i)).equalsIgnoreCase("Peoria, IL"))
{
NoUber_cities.remove(i);
}
else if ((NoUber_cities.get(i)).equalsIgnoreCase("Asheville, NC"))
{
NoUber_cities.remove(i);
}
else if ((NoUber_cities.get(i)).equalsIgnoreCase("Hattiesburg, MS"))
{
NoUber_cities.remove(i);
}
else if ((NoUber_cities.get(i)).equalsIgnoreCase("Portland, ME"))
{
NoUber_cities.remove(i);
}
else if ((NoUber_cities.get(i)).equalsIgnoreCase("Portland, ME"))
{
NoUber_cities.remove(i);
}
else if ((NoUber_cities.get(i)).equalsIgnoreCase("Huntsville, AL"))
{
NoUber_cities.remove(i);
}
else if ((NoUber_cities.get(i)).equalsIgnoreCase("Reading, PA"))
{
NoUber_cities.remove(i);
}
else if ((NoUber_cities.get(i)).equalsIgnoreCase("Birmingham, AL"))
{
NoUber_cities.remove(i);
}
else if ((NoUber_cities.get(i)).equalsIgnoreCase("Bloomington, IN"))
{
NoUber_cities.remove(i);
}
else if ((NoUber_cities.get(i)).equalsIgnoreCase("Bowling Green, KY"))
{
NoUber_cities.remove(i);
}
else if ((NoUber_cities.get(i)).equalsIgnoreCase("Lafayette, LA"))
{
NoUber_cities.remove(i);
}
else if ((NoUber_cities.get(i)).equalsIgnoreCase("Central Atlantic Coast, FL"))
{
NoUber_cities.remove(i);
}
else if ((NoUber_cities.get(i)).equalsIgnoreCase("Lancaster, PA"))
{
NoUber_cities.remove(i);
}
else if ((NoUber_cities.get(i)).equalsIgnoreCase("Charleston, SC"))
{
NoUber_cities.remove(i);
}
else if ((NoUber_cities.get(i)).equalsIgnoreCase("Charleston, WV"))
{
NoUber_cities.remove(i);
}
else if ((NoUber_cities.get(i)).equalsIgnoreCase("San Juan, PR"))
{
NoUber_cities.remove(i);
}
else if ((NoUber_cities.get(i)).equalsIgnoreCase("London, Ont"))
{
NoUber_cities.remove(i);
}
else if ((NoUber_cities.get(i)).equalsIgnoreCase("Springfield, IL"))
{
NoUber_cities.remove(i);
}
else if ((NoUber_cities.get(i)).equalsIgnoreCase("Columbia, MO"))
{
NoUber_cities.remove(i);
}
else if ((NoUber_cities.get(i)).equalsIgnoreCase("Columbia, SC"))
{
NoUber_cities.remove(i);
}
else if ((NoUber_cities.get(i)).equalsIgnoreCase("Montgomery, AL"))
{
NoUber_cities.remove(i);
}
else if ((NoUber_cities.get(i)).equalsIgnoreCase("Morgantown, WV"))
{
NoUber_cities.remove(i);
}
else if ((NoUber_cities.get(i)).equalsIgnoreCase("Fayetteville, AR"))
{
NoUber_cities.remove(i);
}
else if ((NoUber_cities.get(i)).equalsIgnoreCase("Wilmington, NC"))
{
NoUber_cities.remove(i);
}
else if ((NoUber_cities.get(i)).equalsIgnoreCase("Outer Banks, NC"))
{
NoUber_cities.remove(i);
}
}
答案 0 :(得分:0)
您的代码存在的问题是,一旦您从列表中删除项目,所有剩余项目都会更改索引,但您的代码不会考虑这一点。
例如,假设您的列表包含:
0 - AAA
1 - BBB
2 - CCC
3 - DDD
如果i
= 1并且您决定删除BBB,那么列表现在看起来像这样:
0 - AAA
1 - CCC
2 - DDD
但是你的循环然后递增i
并继续检查 - 现在i
= 2并且你错过了转移到索引1的CCC项目。
有很多方法可以解决这个问题。一种方法是在删除项目时递减索引:
for (int i = 0; i < NoUber_cities.size(); i++)
{
if ((NoUber_cities.get(i)).equalsIgnoreCase("Greenville, SC")
|| (NoUber_cities.get(i)).equalsIgnoreCase("Pensacola, FL")
|| (NoUber_cities.get(i)).equalsIgnoreCase("Peoria, IL")
|| (NoUber_cities.get(i)).equalsIgnoreCase("Asheville, NC")
|| (NoUber_cities.get(i)).equalsIgnoreCase("Hattiesburg, MS")
|| (NoUber_cities.get(i)).equalsIgnoreCase("Portland, ME")
|| (NoUber_cities.get(i)).equalsIgnoreCase("Portland, ME")
|| (NoUber_cities.get(i)).equalsIgnoreCase("Huntsville, AL")
|| (NoUber_cities.get(i)).equalsIgnoreCase("Reading, PA")
|| (NoUber_cities.get(i)).equalsIgnoreCase("Birmingham, AL")
|| (NoUber_cities.get(i)).equalsIgnoreCase("Bloomington, IN")
|| (NoUber_cities.get(i)).equalsIgnoreCase("Bowling Green, KY")
|| (NoUber_cities.get(i)).equalsIgnoreCase("Lafayette, LA")
|| (NoUber_cities.get(i)).equalsIgnoreCase("Central Atlantic Coast, FL")
|| (NoUber_cities.get(i)).equalsIgnoreCase("Lancaster, PA")
|| (NoUber_cities.get(i)).equalsIgnoreCase("Charleston, SC")
|| (NoUber_cities.get(i)).equalsIgnoreCase("Charleston, WV")
|| (NoUber_cities.get(i)).equalsIgnoreCase("San Juan, PR")
|| (NoUber_cities.get(i)).equalsIgnoreCase("London, Ont")
|| (NoUber_cities.get(i)).equalsIgnoreCase("Springfield, IL")
|| (NoUber_cities.get(i)).equalsIgnoreCase("Columbia, MO")
|| (NoUber_cities.get(i)).equalsIgnoreCase("Columbia, SC")
|| (NoUber_cities.get(i)).equalsIgnoreCase("Montgomery, AL")
|| (NoUber_cities.get(i)).equalsIgnoreCase("Morgantown, WV")
|| (NoUber_cities.get(i)).equalsIgnoreCase("Fayetteville, AR")
|| (NoUber_cities.get(i)).equalsIgnoreCase("Wilmington, NC")
|| (NoUber_cities.get(i)).equalsIgnoreCase("Outer Banks, NC"))
{
NoUber_cities.remove(i);
i--;
}
}
另一种方法是在Java 8中使用流:
List<String> citiesToRemove = Arrays.asList(
"greenville, sc",
"pensacola, fl",
"peoria, il",
"asheville, nc",
"hattiesburg, ms",
"portland, me",
"huntsville, al",
"reading, pa",
"birmingham, al",
"bloomington, in",
"bowling green, ky",
"lafayette, la",
"central atlantic coast, fl",
"lancaster, pa",
"charleston, sc",
"charleston, wv",
"san juan, pr",
"london, ont",
"springfield, il",
"columbia, mo",
"columbia, sc",
"montgomery, al",
"morgantown, wv",
"fayetteville, ar",
"wilmington, nc",
"outer banks,nc");
List<String> filteredListOfCities = NoUber_cities.stream()
.filter(item -> !citiesToRemove.contains(item.toLowerCase()))
.collect(Collectors.toList());
答案 1 :(得分:0)
向后工作:
for (int i = NoUber_cities.size() - 1; i >= 0; i--)
这样前面项目的索引不会改变,你可以删除索引i
上的项目而没有任何问题。最好从后面删除索引集合中的项目。
假设你有一个清单
0: AAA
1: BBB
2: CCC
3: DDD
4: EEE
5: FFF
6: GGG
如果您在索引3处删除DDD
,则列表将如下所示:
0: AAA
1: BBB
2: CCC
3: EEE
4: FFF
5: GGG
如您所见,更高索引的项目将向下移动并获得新索引。 lower 索引处的项目将保持不变。
现在,如果你迭代前进,当前索引是3,那么下一个索引是4,删除后指向FFF
。这意味着您从未检查过EEE
。
OTOH,如果你向后迭代,,下一个索引是2,那仍然包含CCC
。 (请注意,您已经检查了较高的索引,因此如果这些索引获得新索引并不重要)。数组的大小已更改,但索引低于删除点的项的索引不会更改。