Results 1 to 17 of 17
Thread: ArrayList irritation
- 12-15-2008, 12:50 AM #1
Member
- Join Date
- Oct 2008
- Posts
- 34
- Rep Power
- 0
ArrayList irritation
How do you get a class to take in another arraylist, heres what I got
now I know it's wrong I keep getting an error that says it cannot find the variable other. Can anyone help?Java Code:public void transfer(Purse other) { for(int i = 0; i < Purse.other.size()-1; i++) { purse.add(Purse.other.get(i)); } }
-
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); }
- 12-15-2008, 01:34 AM #3
Member
- Join Date
- Oct 2008
- Posts
- 34
- Rep Power
- 0
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()
-
I had wrongly assumed that Purse inherited from Collection / ArrayList. You'll have to tell us more about your Purse class.
- 12-15-2008, 01:50 AM #5
Member
- Join Date
- Oct 2008
- Posts
- 34
- Rep Power
- 0
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?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)); } } }
-
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 02:15 AM.
- 12-15-2008, 02:15 AM #7
Member
- Join Date
- Oct 2008
- Posts
- 34
- Rep Power
- 0
The .get() method should be in the ArrayList class. All my other methods work fine with it
-
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.
- 12-15-2008, 02:19 AM #9
Member
- Join Date
- Oct 2008
- Posts
- 34
- Rep Power
- 0
Can I assign other as an ArrayList to make it work?
-
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?
- 12-15-2008, 02:23 AM #11
Member
- Join Date
- Oct 2008
- Posts
- 34
- Rep Power
- 0
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.
-
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....
- 12-15-2008, 02:43 AM #13
Member
- Join Date
- Oct 2008
- Posts
- 34
- Rep Power
- 0
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?
-
Nope. you just call get() on other, and the get method on Purse will take care of all of that.does that mean I have to call the other ArrayList directly?
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.
- 12-16-2008, 01:17 AM #15
Member
- Join Date
- Oct 2008
- Posts
- 34
- Rep Power
- 0
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
it should've beenJava Code:for(int i = 0; i < other.size()-1; i++) { purse.add(other.get(i)); }
Why does java have to be so specific? *sigh*Java Code:for(int i = 0; i < other.purse.size()-1; i++) { purse.add(other.purse.get(i)); }
- 12-16-2008, 07:12 AM #16
Senior Member
- Join Date
- Nov 2007
- Posts
- 160
- Rep Power
- 6
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:
You should use:Java Code:other.purse.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:other.get(i)
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 09:59 AM.
-
Similar Threads
-
Arraylist
By gnarly hogie in forum New To JavaReplies: 2Last Post: 12-11-2008, 01:59 AM -
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:43 AM -
ArrayList
By ramitmehra123 in forum New To JavaReplies: 1Last Post: 02-07-2008, 12:47 AM -
ArrayList
By kizilbas1 in forum New To JavaReplies: 11Last Post: 12-05-2007, 07:30 PM -
New to arraylist
By kleave in forum New To JavaReplies: 2Last Post: 11-19-2007, 06:45 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks