0
how can i do a linear search in multi-dim array
i have a 2d array and i need to check that whether each value of first array is equal to any value in the 2nd array and if value differs then add that in an arraylist.but how can i do this i mean i put 2 loops to access them and check condition but in that loop block i have also put add() statement for arraylist and so the value is stored many times in arraylist.how can i avert it.
9 Antworten
+ 7
add a
break;
after the add ()
+ 7
Your problem would be way easier to understand if you provide a simple code
I would do it like that:
for (int i=0; i < first Array.length; i++){
for (int j=0; j < second Array.length; j++){
if...
add ();
}
}
this should work fine because it only goes through the first array once
if that's not working feel free to ask
+ 6
I'm not sure how the TimeTable class is working, but you can use a boolean value to keep track of what's happening within the inner loop. Here is a sample code of the similar concept:
https://code.sololearn.com/cMcqAiIwr94w/?ref=app
+ 5
could you please post the full java code since this is missing quite a lot, that would really help^^
+ 3
yeah @kamil my code is similar.
my problem here is that if conditions evaluates to true it do the add(); second Array. length times. how can i avert this as i want it to add only once.
+ 1
// to iterate through each subject from current day
for(int current = 0;current<TimeTable.table[corresponding_value].length;current++) {
// to iterate through and check each subject of previous day
for(int previous = 0;previous<TimeTable.table[corresponding_value].length;previous++) {
if(!TimeTable.table[corresponding_value][current].equals(TimeTable.table[previous_day_value][previous])) {
change.add(TimeTable.table[corresponding_value][current]);
break;
}else if(TimeTable.table[corresponding_value][current].equals(TimeTable.table[previous_day_value][previous])) {
continue;
}
}
}
return change;
}
this is my code .i want to check current with each each of previous index and if it is not equal then it will add that only once;
@kamil thnx for your help man but that actually didn't work with respect to my needs.
0
ok i'll do it as soon as i get access to my pc, hnx for this gesture.
0
Kamil, what does that code, the first one you showed, do?