Results 1 to 6 of 6
  1. #1
    swalf is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Question 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

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    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.

  3. #3
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by swalf View Post
    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
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  4. #4
    swalf is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default

    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
    Last edited by swalf; 02-18-2011 at 06:29 AM. Reason: short

  5. #5
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Why don't you just use a loop?

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by swalf View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 1
    Last Post: 11-04-2010, 06:53 PM
  2. Trying to create an undo function using an arraylist.
    By Spirit356 in forum New To Java
    Replies: 6
    Last Post: 04-27-2010, 07:01 PM
  3. Replies: 3
    Last Post: 12-04-2009, 03:08 PM
  4. Search function for ArrayList?
    By javanoobie in forum New To Java
    Replies: 11
    Last Post: 04-17-2009, 08:38 PM
  5. Replies: 0
    Last Post: 04-05-2008, 12:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •