# Help in test function for ArrayList

• 02-18-2011, 06:00 AM
swalf
Help in test function for ArrayList
Hi,

I had a problem figuring out how to write a test function for my Two ArrayList to see if they are the Same or not.

I did the Two ArrayList and each list have 3 obj. now I'm confused how to test them to see if both of the list are the same.

Here are my code for the ArrayList:
// ArrayList
ArrayList <Cow> cows1 = new ArrayList<Cow>();
//System.out.println("ArrayList 1");
//System.out.println(ArrayList<Cow>.cows1);

ArrayList<Cow> cows2 = new ArrayList<Cow>();
//System.out.println("ArrayList 2");
//System.out.println(ArrayList2<Cow>.Cows);

this is the function I have problem with:
//ArrayList Test
public boolean equals(ArrayList<Cow> cows) {
if (this == cows)
return true;
if (Cows == null)
return false;
if (ArrayList1() != Cows.ArrayList2())
return false;
Cow other = (Cow);
if (ArrayList1 == null) {
if (other.ArrayList2 != null)
return false;
} else if (!ArrayList1.equals(other.ArrayList2))
return false;
return true;
}

if some one can help
• 02-18-2011, 06:05 AM
Junky
What is your definition of equals?

{1,2,3}
{3,2,1}

Are those two lists equal?

On another note provide more information. Do you get errors? Then copy and paste them so we can see them. We don't read minds.
• 02-18-2011, 06:15 AM
goldest
Quote:

Originally Posted by swalf
how to test them to see if both of the list are the same.

This part is a bit confusing. What exactly do you want to check?

If you want to check object equality, go for equals().

If it's the size of list, then use the size() method and do comparison.

If you want to see if one collection elements are present in other one or not, go for containsAll() method.

In fact, there are many more things. But the main thing is you need to be more specific about what you exactly want to do?

Goldest
• 02-18-2011, 07:25 AM
swalf
I need to make test function for my two ArrayLists. to see if the two List are the same.
if ArrayList1 = ArrayList two

ArrayList1 {cow1,cow2,cow3} = ArrayList2 {cow1,cow2,cow3}
to see if the two ArrayLists are equals:

//ArrayList Test
public boolean equals(ArrayList<Cow> cows) {
if (this == cows)
return true;
if (Cows == null)
return false;
if (ArrayList1() != Cows.ArrayList2())
return false;
Cow other = (Cow);
if (ArrayList1 == null) {
if (other.ArrayList2 != null)
return false;
} else if (!ArrayList1.equals(other.ArrayList2))
return false;
return true;
}

So my test function is not working if some one can fix it or there is another way to do it
• 02-18-2011, 04:26 PM
Why don't you just use a loop?
• 02-18-2011, 07:14 PM
JosAH
Quote:

Originally Posted by swalf
I need to make test function for my two ArrayLists. to see if the two List are the same.
if ArrayList1 = ArrayList two

ArrayList1 {cow1,cow2,cow3} = ArrayList2 {cow1,cow2,cow3}
to see if the two ArrayLists are equals:

//ArrayList Test
public boolean equals(ArrayList<Cow> cows) {
if (this == cows)
return true;
if (Cows == null)
return false;
if (ArrayList1() != Cows.ArrayList2())
return false;
Cow other = (Cow);
if (ArrayList1 == null) {
if (other.ArrayList2 != null)
return false;
} else if (!ArrayList1.equals(other.ArrayList2))
return false;
return true;
}

So my test function is not working if some one can fix it or there is another way to do it

I don't understand what's going on in there: you're using ArrayList1 and ArrayList2 as method and as member variables; you're using Cows and cows interchangeably; you're even using cast like expressions that don't even compile; please clear up your code and ask a question about it.

kind regards,

Jos