1

The following code is given for my assignment:

class AList<T> implements ListInterface<T> {

private T[] list; // array of list entries
private int numberOfEntries; // current number of entries in list
private static final int DEFAULT_INITIAL_CAPACITY = 25;

public AList() {
    this(DEFAULT_INITIAL_CAPACITY);  // call next constructor
} // end default constructor

public AList(int initialCapacity) {
    numberOfEntries = 0;
    // the cast is safe because the new array contains null entries
    @SuppressWarnings("unchecked")
    T[] tempList = (T[]) new Object[initialCapacity];
    list = tempList;
} // end constructor

My assignment is to create a method that returns true when the contents of two AList objects are the same. That is, they have the same number of items and each item in one object is equal to the item in its corresponding location in the other object. I am required to use the following meathod header:

public boolean equals (Object other)
{
//my code goes here
}

I tried this, but it's not working.

public boolean equals(Object other)
{
if (Arrays.equals(this.list, other))
return true;
else
return false;

}//end equals

other is of type Object. how do I make it an array?

5
  • 1
    other "should" be an instance AList. You need to check for that first, then cast it and try an compare the arrays... Commented Sep 19, 2013 at 3:43
  • I'm guessing that somewhere in there you'd use a loop. But maybe before that check that the two arrays are the same size. Commented Sep 19, 2013 at 3:45
  • Why are you converting to an array at all, instead of using a sane List#equals() implementation? Commented Sep 19, 2013 at 3:48
  • @MadProgrammer other would be an instance of AList. in the main method it would look something like this: AList<Integer> testListA = new AList<Integer> (); AList<Integer> testListB = new AList<Integer> (); testListA.equals(testListB) Commented Sep 19, 2013 at 5:12
  • In that case, I think you have a problem, based on the code you've supplied, because list has private access, meaning you can't reference the field. Does AList or ListInterface have any other methods for accessing the data?? Commented Sep 19, 2013 at 5:36

1 Answer 1

1

Modify your code to convert list into Object Array before compairing

public boolean equals(Object other)
{
Object listObj[]=list.toArray();
if (Arrays.equals(listObj, other))
return true;
else
return false;

}//end equals
Sign up to request clarification or add additional context in comments.

2 Comments

return Arrays.equals(listObj, other); seems shorter. Or, if you want to reduce the method body to 1 line: return Arrays.equals(list.toArray(), other);
@Algorithmist that will not work. Arrays.equals() requires two arrays for input. In your solution, "other" is still of type Object, therefore it will not compile.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.