Java 比较嵌套循环中的信息并在匹配时进行标记
数据是一个包含以下内容的数组: 观测1,观测2,观测3,观测4,观测5 stringarray2可能包含上述任何内容的数组,例如: 观察1,观察4 我试图将项目添加到countryList中,如果它们出现在两个数组中,则将其设置为true,否则设置为false 我需要帮助设置嵌套循环 我目前有下面的设置,非常接近,但这只将数组2中的最后一项添加为true(例如,observation4) 你能看出我哪里出了问题吗Java 比较嵌套循环中的信息并在匹配时进行标记,java,android,arrays,loops,nested,Java,Android,Arrays,Loops,Nested,数据是一个包含以下内容的数组: 观测1,观测2,观测3,观测4,观测5 stringarray2可能包含上述任何内容的数组,例如: 观察1,观察4 我试图将项目添加到countryList中,如果它们出现在两个数组中,则将其设置为true,否则设置为false 我需要帮助设置嵌套循环 我目前有下面的设置,非常接近,但这只将数组2中的最后一项添加为true(例如,observation4) 你能看出我哪里出了问题吗 while (data.moveToNext()){ if (d
while (data.moveToNext()){
if (data.moveToFirst()){
do{
observation = data.getString(1);
if (editing.equals("yes")) {
stringarray2
Boolean test = false;
for (String name:stringarray2)
{
if (observation.equals(name)){
test = true;
}else{
test = false;
}
}
Country country = new Country(null,observation,test);
countryList.add(country);
}else{
Country country = new Country(null,observation,false);
countryList.add(country);
}
}while(data.moveToNext());
}
}
我认为你的问题在于你的状态 用你的第一个循环
while (data.moveToNext())
已经检查是否有下一个并移动到它,您不应该再次移动到第一个,只需开始使用该值
你需要像这样的东西
try {
while (data.moveToNext()) {
observation = data.getString(1);
if (editing.equals("yes")) {
stringarray2
Boolean test = false;
for (String name:stringarray2)
{
if (observation.equals(name)){
test = true;
}else{
test = false;
}
}
Country country = new Country(null,observation,test);
countryList.add(country);
}else{
Country country = new Country(null,observation,false);
countryList.add(country);
}
}
} finally {
data.close();
}
假设你的内在逻辑运作良好
p.S我添加了一个try finally
,因为您需要在查询结果完成后关闭光标。试试这个
if (data.moveToFirst())
{
do
{
observation = data.getString(1);
if (editing.equals("yes"))
{
stringarray2 //random word here?
Boolean test = false;
for (String name:stringarray2)
{
if (observation.equals(name))
{
test = true;
break;
}
}
Country country = new Country(null,observation,test);
countryList.add(country);
}
else
{
Country country = new Country(null,observation,false);
countryList.add(country);
}
}
while (data.moveToNext())
}
感谢您对moveToNext的解释。此代码仍然只选择最后一项,但标记的答案有效。我已经按照你的建议加了试。我不知道为什么我的问题被否决了。