Hello :)

I've been trying to compare to array lists with the following code:

final VideoObj v1 = new VideoObj( "A", 2000, "B" );
	  final VideoObj v2 = new VideoObj( "B", 2000, "B" );
          
          ArrayList<Record> list = new ArrayList<Record>(); 
	  list.add(new Record(v1,1,0,0));
	  list.add(new Record(v2,3,0,0));
	  
	  ArrayList<Record> list3 = new ArrayList<Record>(); 
	  list3.add(new Record(v1,1,0,0));
	  list3.add(new Record(v2,3,0,0));

          if (list.containsAll(list3))
		  System.out.println("equal");
		 else
			 System.out.println("not equal");

The code gives "not equal" all the times, and the data is exactly the same..
Does any body know how I can compare 2 array lists?
any idea will be highly appreciated!

Can you post the code for your Record class? I suspect the answer might be that you need to override the Equals method, so if you haven't done that I would start there...

Thanx for the reply :)
here is the code of Record Class ..

/**
 * Utility class for Inventory.  Fields are mutable and package-private.
 * Does not override <code>equals</code> or <code>hashCode</code>.
 *
 * @objecttype Mutable Data Class
 */
final class Record {
  /**
   * The video.
   * @invariant <code>video != null</code>
   */
  VideoObj video;
  /**
   * The number of copies of the video that are in the inventory.
   * @invariant <code>numOwned > 0</code>
   */
  int numOwned;
  /**
   * The number of copies of the video that are currently checked out.
   * @invariant <code>numOut <= numOwned</code>
   */
  int numOut;
  /**
   * The total number of times this video has ever been checked out.
   * @invariant <code>numRentals >= numOut</code>
   */
  int numRentals;

  /**
   * Initialize all object attributes.
   */
  Record(VideoObj video, int numOwned, int numOut, int numRentals) {
    this.video = video;
    this.numOwned = numOwned;
    this.numOut = numOut;
    this.numRentals = numRentals;
  }
  /**
   * Return a shallow copy of this record.
   */
  public Record copy() {
    return new Record(video,numOwned,numOut,numRentals);
  }
  /**
   * Return a string representation of the object in the following format:
   * <code>"video [numOwned,numOut,numRentals]"</code>.
   */
  public String toString() {
    StringBuilder buffer = new StringBuilder();
    buffer.append(video);
    buffer.append(" [");
    buffer.append(numOwned);
    buffer.append(",");
    buffer.append(numOut);
    buffer.append(",");
    buffer.append(numRentals);
    buffer.append("]");
    return buffer.toString();
  }
}

Thanks :icon_cheesygrin:

This article has been dead for over six months. Start a new discussion instead.