Results 1 to 17 of 17
  1. #1
    gnarly hogie is offline Member
    Join Date
    Oct 2008
    Posts
    34
    Rep Power
    0

    Default ArrayList irritation

    How do you get a class to take in another arraylist, heres what I got
    Java Code:
    	
    public void transfer(Purse other)
    {
    	for(int i = 0; i < Purse.other.size()-1; i++)
    	{
    			purse.add(Purse.other.get(i));
    	}	
    }
    now I know it's wrong I keep getting an error that says it cannot find the variable other. Can anyone help?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    have you tried using just other and not Purse.other?

    Also, have you considered simplifying things by using addAll?
    Java Code:
    public void transfer(Purse other)
    {
      purse.addAll(other);
    }

  3. #3
    gnarly hogie is offline Member
    Join Date
    Oct 2008
    Posts
    34
    Rep Power
    0

    Default

    To your first question, yeah I get the error that it can't find the methods size() and get(). To your second one I just tried it but it still can't find either of the methods, size() or addAll()

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I had wrongly assumed that Purse inherited from Collection / ArrayList. You'll have to tell us more about your Purse class.

  5. #5
    gnarly hogie is offline Member
    Join Date
    Oct 2008
    Posts
    34
    Rep Power
    0

    Default

    Java Code:
    import java.util.*;
    
    public class Purse
    {
    	private ArrayList<String> purse;//sets an arrayList to be used by all methods
    	
    	public Purse()
    	{
    		purse = new ArrayList<String>();//Intializes an ArrayList
    	
    	}
    	
    	public void addCoin(String coinName)
    	{
    		purse.add(coinName);//Adds a coin to the purse
    	}
    	
    	public String toString()
    	{	
    		String end = "]";//String variable holds the end
    		String space = ",";//String variable holds the space
    		String start = "Purse[";//String variable that holds the begining
    		
    		System.out.print(start);//Prints out the begining
    		
    		for(int i = 0;i < purse.size(); i++)
    		{
    			System.out.print(purse.get(i));//Prints out all the strings
    			if(i!=purse.size()-1)//If statement that stops the printing space if before the array ends
    				System.out.print(space);//prints out the space
    		}
    		
    		System.out.print(end);//prints out the end
    		System.out.println();//prints out a line
    		return null;//Lets it compile
    	}	
    	
    	public void reverse()
    	{
    		int a = purse.size()-1;
    		
    		for(int i = 0; i < purse.size()-1; i++)
    		{
    			String temp = purse.get(a);
    			purse.set(a, purse.get(i));
    			purse.set(i, temp);
    			a--;
    		}
    	} 
    	
    	public void transfer(Purse other)
    	{
    		for(int i = 0; i < other.size()-1; i++)
    		{
    			purse.add(other.get(i));
    		}	
    	}
    }
    My purse class is supposed to take inputs in a string format such as quarter, dime etc. Everything works except for the transfer method with is supposed to take another ArrayList from the user and transfer all of the coins within that purse to another purse. Did explain it well?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    So you're trying to call the get method however your Purse class doesn't have this method yet. So, you will need to write this. Good luck.

    Let us know if you have any questions on how to write this get(int i) method, OK?
    Last edited by Fubarable; 12-15-2008 at 03:15 AM.

  7. #7
    gnarly hogie is offline Member
    Join Date
    Oct 2008
    Posts
    34
    Rep Power
    0

    Default

    The .get() method should be in the ArrayList class. All my other methods work fine with it

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    It is in the ArrayList class, but you're calling the method on a Purse object not an ArrayList object. There's a BIG difference here. Purse holds an ArrayList but isn't itself an ArrayList and so get has no meaning. Again, you will have to write this method for the Purse class. And your method will use ArrayList's get to work.

    For instance, ArrayList has an add(...) method, and Purse has an addCoin(...) method that then uses the ArrayLists' add method to function. Think of your get method as being analogous to this.

  9. #9
    gnarly hogie is offline Member
    Join Date
    Oct 2008
    Posts
    34
    Rep Power
    0

    Default

    Can I assign other as an ArrayList to make it work?

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    No, you can't and you shouldn't. Why won't you just create a get method as I recommend? What's holding you back?

  11. #11
    gnarly hogie is offline Member
    Join Date
    Oct 2008
    Posts
    34
    Rep Power
    0

    Default

    Laziness and knowledge, I have no idea where to start for such a method but oh well I'll try to make it. Thank you.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    look at the addCoin method that you already have. Again, the get method will be similar except that the parameter will be an integer and you will return something -- likely a String. The arraylist object will be called from within the method....

  13. #13
    gnarly hogie is offline Member
    Join Date
    Oct 2008
    Posts
    34
    Rep Power
    0

    Default

    Yeah, the int and String thing I understand but calling the ArrayList is confusing. My addCoin calls the purse ArrayList directly, does that mean I have to call the other ArrayList directly?

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    does that mean I have to call the other ArrayList directly?
    Nope. you just call get() on other, and the get method on Purse will take care of all of that.

    Just try to create a real real simple get(int i) method and post it here. If it stinks we'll attack it mercilessly, but hey that's what friends are for, right?

    But seriously, you have nothing to lose to try this here and now.

  15. #15
    gnarly hogie is offline Member
    Join Date
    Oct 2008
    Posts
    34
    Rep Power
    0

    Default

    Hey fubarable, sorry I wasn't able to answer back soon I have been busy with work but it turns out that I had the answer but I wrote it wrong, instead of
    Java Code:
    for(int i = 0; i < other.size()-1; i++)
    		{
    			purse.add(other.get(i));
    		}
    it should've been
    Java Code:
    for(int i = 0; i < other.purse.size()-1; i++)
    		{
    			purse.add(other.purse.get(i));
    		}
    Why does java have to be so specific? *sigh*

  16. #16
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    8

    Default

    What you're doing now is referencing your ArrayList directly from your Purse Object... What Fubarable has been trying to get you to do is to make a get method that does this for you...

    So instead of:
    Java Code:
    other.purse.get(i)
    You should use:
    Java Code:
    other.get(i)
    For this to work, you'll need a get method in your purse class. Becaus we pass an int, and receive a String, we expect the method to look like this:

    Java Code:
    String get(int i) {
        //code to get the requested String from the ArrayList
        //return the results from above
    }
    Last edited by carderne; 12-16-2008 at 10:59 AM.

  17. #17
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by carderne View Post
    What you're doing now is referencing your ArrayList directly from your Purse Object... What Fubarable has been trying to get you to do is to make a get method that does this for you...
    Exactly what he says -- Thanks!

Similar Threads

  1. Arraylist
    By gnarly hogie in forum New To Java
    Replies: 2
    Last Post: 12-11-2008, 02:59 AM
  2. Replies: 2
    Last Post: 04-21-2008, 12:43 PM
  3. ArrayList
    By ramitmehra123 in forum New To Java
    Replies: 1
    Last Post: 02-07-2008, 01:47 AM
  4. ArrayList
    By kizilbas1 in forum New To Java
    Replies: 11
    Last Post: 12-05-2007, 08:30 PM
  5. New to arraylist
    By kleave in forum New To Java
    Replies: 2
    Last Post: 11-19-2007, 07:45 PM

Posting Permissions

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