Scott Tilney
Scott Tilney

Reputation: 13

Convert type object to type array. Java

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?

Upvotes: 1

Views: 465

Answers (1)

Algorithmist
Algorithmist

Reputation: 6695

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

Upvotes: 1

Related Questions