Results 1 to 6 of 6
- 02-18-2011, 05:00 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 2
- Rep Power
- 0
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>();
cows1.add(new Cow());
cows1.add(new Cow());
cows1.add(new Cow());
//System.out.println("ArrayList 1");
//System.out.println(ArrayList<Cow>.cows1);
ArrayList<Cow> cows2 = new ArrayList<Cow>();
cows2.add(new Cow());
cows2.add(new Cow());
cows2.add(new 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, 05:05 AM #2
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, 05:15 AM #3
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?
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 02-18-2011, 06:25 AM #4
Member
- Join Date
- Feb 2011
- Posts
- 2
- Rep Power
- 0
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 itLast edited by swalf; 02-18-2011 at 06:29 AM. Reason: short
- 02-18-2011, 03:26 PM #5
Why don't you just use a loop?
- 02-18-2011, 06:14 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,402
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Test for all empty Strings in LinkedHashMap<String,ArrayList<String>
By albertkao in forum New To JavaReplies: 1Last Post: 11-04-2010, 06:53 PM -
Trying to create an undo function using an arraylist.
By Spirit356 in forum New To JavaReplies: 6Last Post: 04-27-2010, 07:01 PM -
Possible? Callback function passed as arguments to another function
By TreyAU21 in forum Advanced JavaReplies: 3Last Post: 12-04-2009, 03:08 PM -
Search function for ArrayList?
By javanoobie in forum New To JavaReplies: 11Last Post: 04-17-2009, 08:38 PM -
Test Advisory Panel-Telecommute- Test your Java skills + share insights on Java tests
By michelle in forum Jobs OfferedReplies: 0Last Post: 04-05-2008, 12:38 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks