Results 1 to 2 of 2
- 09-28-2012, 04:34 AM #1
Member
- Join Date
- Sep 2012
- Posts
- 8
- Rep Power
- 0
Using a method from another class on an object of another class.
Here is my problem.... When trying to create the method public void transfer, I am using a seperate instance of purse as my parameter. Purse contains a new ArrayList<String> for each instance of purse. However it does not recognize the .get() or .size() methods for the coins array in my "other" purse.Java Code:import java.util.ArrayList; public class Purse { private int quarters; private int dimes; private int nickels; private int pennies; public static final double QUARTER_VALUE = 0.25; public static final double DIME_VALUE = 0.10; public static final double NICKEL_VALUE = 0.05; public static final double PENNY_VALUE = 0.01; private ArrayList<String> coins; /** Constructs a default purse with no change in it. */ public Purse() { coins = new ArrayList<String>(); } public void addCoin(String coinName) { coins.add(coinName); } public void writeToString() { System.out.println(" Purse contains " + coins); } public ArrayList reverse() { ArrayList<String> reversed = new ArrayList<String>(); for(int i = coins.size() - 1; i >= 0; i--) { String newCoin = coins.get(i); reversed.add(newCoin); } return reversed; } public ArrayList toArrayList() { return coins; } public void transfer(Purse other) { ArrayList another = other.toArrayList(); for(int i = 0; i < another.size(); i++) { String z = another.get(i); coins.add(z); } } }
Is there an easier way to transfer the contents of one arraylist to another? Also how in the world do I use arraylist methods on this object!
The object contains a new arraylist.... it just won't use the arraylist methods on it... i can only use them on coins.
Any help?
- 09-28-2012, 05:38 PM #2
Similar Threads
-
Why can't my Inner Class object use Outerclass method ?
By fatabass in forum New To JavaReplies: 1Last Post: 02-04-2012, 11:11 AM -
Unable to call setRedirectStrategy() method through object DefaultHttpClient Class
By aashish.kkalra@gmail.com in forum Advanced JavaReplies: 1Last Post: 11-29-2011, 01:30 PM -
Unable to access getter method in an object class I made
By EnSlavingBlair in forum New To JavaReplies: 6Last Post: 10-01-2011, 10:52 AM -
Drawing an object in my canvas class, the object is created in a separate class
By Hornfreak in forum AWT / SwingReplies: 3Last Post: 05-02-2011, 04:37 AM -
Object class's equals() method behavior????
By skyineyes in forum New To JavaReplies: 4Last Post: 07-19-2008, 11:58 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks