Results 1 to 3 of 3

Thread: Arraylist

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

    Default Arraylist

    I am having trouble with ArrayList's right now, what I have to do is write a purse class where I make an ArrayList that I can input coins in a string format (quarter, dime, nickel etc.) and then right an addCoin method and toString. The addCoin method is easy but the toString one is where I am having problems such as I don't know how to return the ArrayList. Sorry if it's a dumb question but my teacher or book didn't go into detail with this.
    Java Code:
    import java.util.*;
    
    public class Purse
    {
    	ArrayList<String> purse;
    	
    	public Purse()
    	{
    		purse = new ArrayList<String>();
    	
    	}
    	
    	public void addCoin(String coinName)
    	{
    		purse.add(coinName);
    	}
    	
    	public String toString()
    	{	
    		for(int i = 0;i < purse.size(); i++)
    		{
    			String a = purse.get(i);
    			return a;
    		}		
    	}	
    }

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

    Default

    Nvm figured it out.

  3. #3
    Sven is offline Member
    Join Date
    Dec 2008
    Location
    Enschede, Overijssel, the Netherlands
    Posts
    19
    Rep Power
    0

    Default

    Use purse.toString(). This works on Collections as it prints out the 'toString()' of all the individual elements.

Similar Threads

  1. Replies: 2
    Last Post: 04-21-2008, 11:43 AM
  2. ArrayList
    By ramitmehra123 in forum New To Java
    Replies: 1
    Last Post: 02-07-2008, 12:47 AM
  3. ArrayList
    By kizilbas1 in forum New To Java
    Replies: 1
    Last Post: 01-12-2008, 08:48 PM
  4. ArrayList
    By kizilbas1 in forum New To Java
    Replies: 11
    Last Post: 12-05-2007, 07:30 PM
  5. New to arraylist
    By kleave in forum New To Java
    Replies: 2
    Last Post: 11-19-2007, 06: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
  •