Arraylist problem
Hello folks,
I have two array lists both of which have objects of type TapeData
TapeData goes as
class TapeData{
String tapeNumber;// tape numbers
String disc;// discription
String addedDisc;// added discription
}
(Tape data is a subclass of ReadWriteFile class where i am originally populating the two ArrayLists dynamically)
now the issue is when i need to compare the two Array Lists with only thetapeNumber element if any of the tape numbers from the TapeData objects in List A do not exist in any of the the TapeData objects of list B, those objects have to be added to a third list, lets say badArray.
below is what i am doing, but it is adding all elements of list A to badArray, can you please help me out with this ..
here is the code
ArrayList reconcile(ArrayList array1, ArrayList array2){
firstArray = array1;
secondArray = array2;
try{
Iterator firstIter = firstArray.iterator();
Iterator secondIter = secondArray.iterator();
ReadWriteFile.TapeData key;
ReadWriteFile.TapeData key2;
while (firstIter.hasNext()){
key = (ReadWriteFile.TapeData)firstIter.next();
while (secondIter.hasNext()){
key2 = (ReadWriteFile.TapeData)secondIter.next();
if (key.tapeNumber.equals(key2.tapeNumber)){
System.out.println("Good: " + key.tapeNumber);
}
} badArray.add(key);// end of second while
System.out.println("Bad tape: " + key.tapeNumber);
}
System.out.println("Done");
}catch (Exception e){
System.out.println(e);
}
return badArray;
}// end of reconcile method
null
[2543 byte] By [
bilaliza] at [2007-11-27 4:40:36]

# 1
It's doing exactly what you've asked it to (as code almost always does!) - iterates over firstArray, and add each element to badArray, indiscriminately . Shouldn't the add to badArray be inside the inner loop, and under some condition?
On a side note, why assign the arguments to something else inside the loop? firstArray, secondArray and badArray appear to be instance variables, but you're returning badArray from here - that seems odd, is it what you meant?
# 2
thats true, thats what i originally thought too that i should add to badArray with an else, but if i do so, it will add as soon as the hasNext does not contain it thus not going to the element after it. .. so thats where i am stuck ..
regarding the assignment i think i have placed the return badArray after every thing is done, isnt that how it should be?
# 3
> thats true, thats what i originally thought too that
> i should add to badArray with an else, but if i do
> so, it will add as soon as the hasNext does not
> contain it thus not going to the element after it. ..
> so thats where i am stuck ..
Not sure I follow you here. What you're doing is pretty commonplace, it works
> regarding the assignment i think i have placed the
> return badArray after every thing is done, isnt that
> how it should be?
I mean should badArray even be an instance variable?
# 4
what i mean to say is that if i add elements to badArray within the loop it will add every first element in the second array if it does not match the element in first array regardless if the element exists some where down the array in second array. which is of course not our objective here.
i am a beginner to java so i dont really understand what could be the problem declaring badArray as an instance veriable.... can you point out a better option please?
and thanks for your help :P
# 5
You need to create a boolean to remember whether you have found the key yet.
Then when the second loop finishes, you can use the boolean to determine whether the key should be added to badArray.
The boolean can also be used to terminate the second while loop, once you find a good key.
ArrayList reconcile(ArrayList array1, ArrayList array2){
firstArray = array1;
secondArray = array2;
try {
Iterator firstIter = firstArray.iterator();
Iterator secondIter = secondArray.iterator();
ReadWriteFile.TapeData key;
ReadWriteFile.TapeData key2;
while (firstIter.hasNext()){
key = (ReadWriteFile.TapeData)firstIter.next();
boolean badKey = true;
while (badKey && secondIter.hasNext()){
key2 = (ReadWriteFile.TapeData)secondIter.next();
if (key.tapeNumber.equals(key2.tapeNumber)) {
System.out.println("Good: " + key.tapeNumber);
badKey = false;
}
}
if (badKey) badArray.add(key);// end of second while
System.out.println("Bad tape: " + key.tapeNumber);
}
System.out.println("Done");
}catch (Exception e) {
System.out.println(e);
}
return badArray;
}// end of reconcile method
# 6
Thanks a lot gte42e, it did help me understand the problem :-)
# 7
Bilaliz,
there is even a shorter way to do what you want:
/*
* return all entries of array1 that are not in array2
*/
ArrayList reconcile(ArrayList array1, ArrayList array2){
badArray = new ArrayList(array1);
badArray.removeAll(array2);
return badArray;
}// end of reconcile method
Class java.util.Collections contains many methods for set operations: addAll, retainAll, removeAll, ...; these are quiete useful.
# 8
Hi, zincteapot
thanks for the suggestion, it would be a great way to do it if it required to compare all of the elements completely, how ever the requirement was to compare only the first element in a given array location with the first element in the given location of the other array.
i.e at a given location key.tapenumber with key2.tapenumber needed to be compared while key.discription did not need to be compared.
thats why i couldnt use the method you suggested, i wish i was a bit more clear on the requirements. but thanks for your help any ways :)
# 9
bilaliz,
you are right, I missed that point. It should work if you modify class TapeData:
class TapeData {
String tapeNumber; // tape numbers
String disc; // discription
String addedDisc; // added discription
/* two TapeData objects are considered to be equal if the tapeNumber is equal */
public boolean equals(Object o) {
if (!(o instanceof TapeData)) {
return false;
}
TapeData other = (TapeData) o;
return this.tapeNumber.equals(other.tapeNumber);
}
}
# 10
One more thing. Make sure u r storing the objects in HashSet instead of ArrayList since list allows duplicate objects.
# 11
The OP has said nothing about duplicates being impermissible so this suggestion is otiose.
ejpa at 2007-7-12 9:51:47 >
