ANDROID,JAVA:使用游标和循环获取重复结果
ANDROID,JAVA:Getting duplicate results with cursor and loop
我是 java 的菜鸟,但我对 C++ 有一定的了解,因此考虑到这一点我制作了循环。
我正在尝试从两个包含来自数据库的 table 名称的数组中获取数据。如果任何 table 中的任何一个元素等于另一个元素,则两个 table 的两个名称都将保存在另一个数组中。这种情况一直发生到阵列结束。现在由于一些问题,每个游标多次包含相同的 table,但我想我已经解决了。
现在通过使用下面的逻辑,我应该得到一对或两对不同的 tables 但我一次又一次地得到相同的对。我多次改变循环顺序和逻辑,但这种形式至少给出了结果。
我认为问题是逻辑上的而不是语法上的。我哪里错了?
我的代码是:
int maa;
for (int i = 0; i < ArraySource.size(); i++) {
// cursor for source
Cursor sourceCursor = myDB.rawQuery("SELECT * FROM '" + ArraySource.get(i) + "';", null);
int sourceIndex = sourceCursor.getColumnIndex("Stop");
for (int ii = 0; ii < ArrayDest.size(); ii++) {
// counter for finding match
maa=0;
Cursor destCursor = myDB.rawQuery("SELECT * FROM '" + ArrayDest.get(ii) + "';", null);
int destIndex = destCursor.getColumnIndex("Stop");
sourceCursor.moveToFirst();
String nameSource = sourceCursor.getString(sourceIndex);
destCursor.moveToFirst();
String nameDest = destCursor.getString(destIndex);
// data is repeated many times in the cursor, so this is hack to take the first result and check with every result
// so if match is found loop is broken
// But to do that first result should be taken out of loop, hence the following
// First result of first cursor checking with first result of second cursor
if (nameSource.equals(nameDest)){
IndirectArray1.add(ArraySource.get(i));
IndirectArray2.add(ArrayDest.get(ii));
maa=1;
}else {
//checking with all results of second cursor
for (destCursor.moveToNext(); !destCursor.isAfterLast(); destCursor.moveToNext()) {
if(nameDest.equals(destCursor.getString(destIndex))){
break;
}
if (nameSource.equals(destCursor.getString(destIndex))) {
IndirectArray1.add(ArraySource.get(i));
IndirectArray2.add(ArrayDest.get(ii));
maa=1;
break;
}
}
}
// condition for running nexr for loop above logic is used in following loop
if(maa ==0){
MsMoot:
for (sourceCursor.moveToNext(); !sourceCursor.isAfterLast(); sourceCursor.moveToNext()) {
destCursor.moveToFirst();
if (nameSource.equals(sourceCursor.getString(sourceIndex))) {
break;
}
if (sourceCursor.getString(sourceIndex).equals(destName)) {
IndirectArray1.add(ArraySource.get(i));
IndirectArray2.add(ArrayDest.get(ii));
break;
}else {
for (destCursor.moveToNext(); !destCursor.isAfterLast(); destCursor.moveToNext()) {
if (destCursor.getString(destIndex).equals(nameDest)) {
break;
}
if (sourceCursor.getString(sourceIndex).equals(destCursor.getString(destIndex))) {
IndirectArray1.add(ArraySource.get(i));
IndirectArray2.add(ArrayDest.get(ii));
break MsMoot;
}
}
}
}
// closing cursors
}destCursor.close();
}sourceCursor.close();
}
看起来像无限循环。您正在获得重复的行,因为您试图多次循环所有内容。在结果中,您多次获得每个 table 的第一行,然后某些值有时被标记为相同。游标查询特定的 table,并存储数据。现在你应该读取所有数据,并以更人性化的方式存储它。然后检查存储的数据是否相等。这是示例代码。
@编辑
现在,对于存储在 ArraySource 列表中的每个 table,您正在为 ArrayDest 列表中的每个 table 调用查询,然后我放置 foreach 循环,检查 firstList 中的每个字符串是否等于 secondList 中的每个字符串.这应该可以正常工作。
for(int i = 0; i < ArraySource.size(); i++) {
List<String> firstList = new ArrayList<>();
List<String> secondList = new ArrayList<>();
Cursor sourceCursor = myDB.rawQuery("SELECT * FROM '" + ArraySource.get(i) + "';", null);
if(sourceCursor != null) {
if(sourceCursor.moveToFirst()) {
do {
String a = sourceCursor.getString(0);
firstList.add(a);
} while(sourceCursor.moveToNext());
}
}
sourceCursor.close();
for (int j = 0; j < ArrayDest.length; j++) {
Cursor destCursor = myDB.rawQuery("SELECT * FROM '" + ArrayDest.get(j) + "';", null);
if (destCursor != null) {
if (destCursor.moveToFirst()) {
do {
String b = destCursor.getString(0);
secondList.add(b);
} while (destCursor.moveToNext());
}
}
destCursor.close();
for (String one : firstList) {
for (int k = 0; k < secondList.size(); k++) {
String two = secondList.get(k);
if (one.equals(two)) {
//make your logic here
}
}
}
}
}
我是 java 的菜鸟,但我对 C++ 有一定的了解,因此考虑到这一点我制作了循环。 我正在尝试从两个包含来自数据库的 table 名称的数组中获取数据。如果任何 table 中的任何一个元素等于另一个元素,则两个 table 的两个名称都将保存在另一个数组中。这种情况一直发生到阵列结束。现在由于一些问题,每个游标多次包含相同的 table,但我想我已经解决了。 现在通过使用下面的逻辑,我应该得到一对或两对不同的 tables 但我一次又一次地得到相同的对。我多次改变循环顺序和逻辑,但这种形式至少给出了结果。 我认为问题是逻辑上的而不是语法上的。我哪里错了?
我的代码是:
int maa;
for (int i = 0; i < ArraySource.size(); i++) {
// cursor for source
Cursor sourceCursor = myDB.rawQuery("SELECT * FROM '" + ArraySource.get(i) + "';", null);
int sourceIndex = sourceCursor.getColumnIndex("Stop");
for (int ii = 0; ii < ArrayDest.size(); ii++) {
// counter for finding match
maa=0;
Cursor destCursor = myDB.rawQuery("SELECT * FROM '" + ArrayDest.get(ii) + "';", null);
int destIndex = destCursor.getColumnIndex("Stop");
sourceCursor.moveToFirst();
String nameSource = sourceCursor.getString(sourceIndex);
destCursor.moveToFirst();
String nameDest = destCursor.getString(destIndex);
// data is repeated many times in the cursor, so this is hack to take the first result and check with every result
// so if match is found loop is broken
// But to do that first result should be taken out of loop, hence the following
// First result of first cursor checking with first result of second cursor
if (nameSource.equals(nameDest)){
IndirectArray1.add(ArraySource.get(i));
IndirectArray2.add(ArrayDest.get(ii));
maa=1;
}else {
//checking with all results of second cursor
for (destCursor.moveToNext(); !destCursor.isAfterLast(); destCursor.moveToNext()) {
if(nameDest.equals(destCursor.getString(destIndex))){
break;
}
if (nameSource.equals(destCursor.getString(destIndex))) {
IndirectArray1.add(ArraySource.get(i));
IndirectArray2.add(ArrayDest.get(ii));
maa=1;
break;
}
}
}
// condition for running nexr for loop above logic is used in following loop
if(maa ==0){
MsMoot:
for (sourceCursor.moveToNext(); !sourceCursor.isAfterLast(); sourceCursor.moveToNext()) {
destCursor.moveToFirst();
if (nameSource.equals(sourceCursor.getString(sourceIndex))) {
break;
}
if (sourceCursor.getString(sourceIndex).equals(destName)) {
IndirectArray1.add(ArraySource.get(i));
IndirectArray2.add(ArrayDest.get(ii));
break;
}else {
for (destCursor.moveToNext(); !destCursor.isAfterLast(); destCursor.moveToNext()) {
if (destCursor.getString(destIndex).equals(nameDest)) {
break;
}
if (sourceCursor.getString(sourceIndex).equals(destCursor.getString(destIndex))) {
IndirectArray1.add(ArraySource.get(i));
IndirectArray2.add(ArrayDest.get(ii));
break MsMoot;
}
}
}
}
// closing cursors
}destCursor.close();
}sourceCursor.close();
}
看起来像无限循环。您正在获得重复的行,因为您试图多次循环所有内容。在结果中,您多次获得每个 table 的第一行,然后某些值有时被标记为相同。游标查询特定的 table,并存储数据。现在你应该读取所有数据,并以更人性化的方式存储它。然后检查存储的数据是否相等。这是示例代码。
@编辑
现在,对于存储在 ArraySource 列表中的每个 table,您正在为 ArrayDest 列表中的每个 table 调用查询,然后我放置 foreach 循环,检查 firstList 中的每个字符串是否等于 secondList 中的每个字符串.这应该可以正常工作。
for(int i = 0; i < ArraySource.size(); i++) {
List<String> firstList = new ArrayList<>();
List<String> secondList = new ArrayList<>();
Cursor sourceCursor = myDB.rawQuery("SELECT * FROM '" + ArraySource.get(i) + "';", null);
if(sourceCursor != null) {
if(sourceCursor.moveToFirst()) {
do {
String a = sourceCursor.getString(0);
firstList.add(a);
} while(sourceCursor.moveToNext());
}
}
sourceCursor.close();
for (int j = 0; j < ArrayDest.length; j++) {
Cursor destCursor = myDB.rawQuery("SELECT * FROM '" + ArrayDest.get(j) + "';", null);
if (destCursor != null) {
if (destCursor.moveToFirst()) {
do {
String b = destCursor.getString(0);
secondList.add(b);
} while (destCursor.moveToNext());
}
}
destCursor.close();
for (String one : firstList) {
for (int k = 0; k < secondList.size(); k++) {
String two = secondList.get(k);
if (one.equals(two)) {
//make your logic here
}
}
}
}
}