Results 1 to 5 of 5
  1. #1
    corong1997 is offline Member
    Join Date
    Sep 2013
    Posts
    10
    Rep Power
    0

    Default Comparing two arrayLists?

    So for my assignment, I basically have to create a method that compares two arrayLists, and if they have the same contents in the same order, sets a boolean as true. This is what I have so far for my method, but I'm really not sure if this is even remotely correct. The boolean returns fine in the tester, but it doesn't set it as true even when the contents of the arrayLists are the same. Any insight into what I should do would be much appreciated.

    The method in question is bolded

    Java Code:
    public class Purse
    {
        private ArrayList<String> purse; //declares
        //could do: private ArrayList<String> purse = new ArrayList<String>(); 
        /**
         * a no args constructor for building a Purse object with no coins
         */
        public Purse()
        {
            purse = new ArrayList<String>(); //initializes
        }
        /**
         * a parameterized constructor for building a Purse object
         * @param coinName a string representing a coin in the Purse
         */
        public Purse(String coinName)
        {
            purse = new ArrayList<String>();
            purse.add(coinName);
        }
        /**
         * This method will add a coin to the Purse
         * @param coinName a string representing a coin in the Purse
         */
        public void addCoin(String coinName)
        {
            purse.add(coinName);      
        }
        /**
         * This method will reverse the order of the coins in the Purse
         */
        public void reverse()
        {
            ArrayList<String> temporary = new ArrayList<String>();
            
            for(int i = 0; i<purse.size(); i++)
            {
                temporary.add(purse.get(purse.size() - (i+1)));
            }
            
            for(int i = 0; i < purse.size(); i++)
            {
                purse.set(i, temporary.get(i));
            }
        }
         //This algorithm shifts around the positions and takes up less memory
    //     public void reverse()
    //     {
    //         int i = 0;
    //         int j = purse.size()-1;
    //         while(i<j)
    //         {
    //             String temp = purse.get(i);
    //             purse.set(i, purse.get(j));
    //             purse.set(j, temp);
    //             i++;
    //             j--:
    //         }
    //     }
        /**
         * This method will check to see if a different purse has the same
         * coins as another
         * @return a boolean representation of whether or not the purses have
         * the same coins
         */
     [B]   public boolean sameContents(Purse other)
        {
            boolean same = false;
            if (purse.equals(other.purse))
            {
                same = true;
            }
            else
            {
                same = false;
            }
            
            return same;
        }[/B]
        /**
         * This method will transfer the coins from Purse other(in the parameters) to purse
         */
        public void transfer(Purse other)
        {
            purse.add(other.purse.remove(0));
        }
        /**
         * Builds and returns a String representation of the Purse
         * @return a String representation of a Purse object
         */
        public String toString()
        {
            String str = "Purse " + purse.toString();
            return str;
        }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Comparing two arrayLists?

    What are you using to compare the contents of the arraylists?
    Have you tested the technique with two one element arraylists to see what it does?

    Can you make a small simple program that compiles, executes and shows the problem?
    Last edited by Norm; 11-12-2013 at 02:34 AM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    corong1997 is offline Member
    Join Date
    Sep 2013
    Posts
    10
    Rep Power
    0

    Default Re: Comparing two arrayLists?

    I'm not quite sure what you mean. If you mean what is happening in my tester, here it is. The bolding isnt working, but it is the second if statement

    Java Code:
    public class PurseTester
    {
        public static void main(String[] args)
        {
            Scanner scan = new Scanner(System.in);
            
            System.out.println("Would you like to make a transfer/reverse or check the contents? (t or c)");
            String choice = scan.nextLine();
            
            if (choice.equals("t"))
            {
            System.out.println("How many coins would you like to put in the purse?");
            int length = scan.nextInt();
            scan.nextLine();
            Purse p1 = new Purse();
            for(int i = 0; i<length ; i++)
            {
                System.out.println("Which coin would you like to add?");
                String a = scan.nextLine();
                           
                p1.addCoin(a);
            }
            System.out.println(p1);
            Purse p2 = new Purse("Penny");
            System.out.println("Purse 2:\n" + p2);
            p1.transfer(p2);
            System.out.println("Purse 1 after transfer:" + p1);
            p1.reverse();
            System.out.println("\nPurse 1 Reverse: " + p1 + "Purse 2: " + p2 + "\nHave a nice day!");
        }
        
       [B] if (choice.equals("c"))
        {
            System.out.println("How many coins are in your first purse?");
            int length = scan.nextInt();
            scan.nextLine();
            Purse p1 = new Purse();
            for(int i = 0; i<length ; i++)
            {
                System.out.println("Which coin would you like to add?");
                String a = scan.nextLine();
                           
                p1.addCoin(a);
            }
            
            System.out.println("How many coins are in your second purse?");
            int length2 = scan.nextInt();
            scan.nextLine();
            Purse p2 = new Purse();
            for(int i = 0; i < length ; i++)
            {
                System.out.println("Which coin would you like to add?");
                String a = scan.nextLine();
                
                p1.addCoin(a);
            }
            
            System.out.println(p1.sameContents(p2));
            [/B]
        }
        
        }
    }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Comparing two arrayLists?

    Can you make a small simple program that compiles, executes and shows the problem? A program that does NOT require the tester make any responses. Hardcode all the needed data in the code.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    corong1997 is offline Member
    Join Date
    Sep 2013
    Posts
    10
    Rep Power
    0

    Default Re: Comparing two arrayLists?

    Okay, so I just made one, and turns out what I did should work. Time to look through my code again, I guess. Thanks for the idea!

Similar Threads

  1. Replies: 3
    Last Post: 10-27-2013, 07:57 PM
  2. help with arraylists
    By Lonewolf in forum New To Java
    Replies: 10
    Last Post: 09-17-2013, 12:00 PM
  3. Help with arraylists!
    By MrSquishyBoots in forum New To Java
    Replies: 3
    Last Post: 01-24-2013, 10:52 AM
  4. Arraylists
    By talia in forum New To Java
    Replies: 5
    Last Post: 01-30-2012, 07:44 PM
  5. comparing Graphs and Comparing Matrix
    By jetnor in forum New To Java
    Replies: 0
    Last Post: 03-27-2011, 02:40 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
  •