Results 1 to 11 of 11
- 05-21-2011, 05:37 AM #1
Member
- Join Date
- May 2011
- Posts
- 15
- Rep Power
- 0
Need help adding data from an array with a variable.
Well what I'm trying to do is, I'm storing the current day in an array, and whenever the user clicks a button it will make it go to the next day.
I got the info I need stored in my array but whenever I try to use it to add an integer on it, it says "Array required, but java.util.arraylist found".
Java Code:ArrayList playerList = new ArrayList(); playerList.add(newplayer.getDay()); [COLOR="red"]playerList[0] = newplayer.getDay() + 1;[/COLOR] //"Array required, but java.util.arraylist found" I get this error for this code. crntdaylbl.setText(playerList[0] + "/30");Last edited by Fubarable; 05-21-2011 at 12:36 PM. Reason: code tags added
- 05-21-2011, 05:53 AM #2
playerList[0] refers to an Array. You would use playerList.get() to access your ArrayList.
Take a look at the API.
ArrayList (Java Platform SE 6)Last edited by Dark; 05-21-2011 at 08:24 AM.
- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
- 05-21-2011, 08:05 AM #3
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
I do not see playerList.get(), only newplayer.getDay().
please use code tags.
You use
which is for Array and not for ArrayList.Java Code:playerList[0]
try set() method, syntax is <ArrayList>.set(<index>, <object>);
Java Code:[b]EXAMPLE:[/b] int indexToChange = 0; int newDay = newPlayer.getDay()+1; playerList.set(indexToChange,newDay);
- 05-21-2011, 08:13 AM #4
get(int index)
Returns the element at the specified position in this list.
I was referring to how he was trying to access his array list. He was trying to access slot 0 in an Array that didn't exist. If he added newPlayer.getDay() to playerList then did playerList.get(0) he would get exactly what he was trying to do there. If you read his code you would know that playerList is an ArrayList of Objects and playerList.get() is within the bounds.
I referred him to the API because he is probably learning about ArrayLists and by browsing the API he might stumble across the components he needs.- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
- 05-21-2011, 08:20 AM #5
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
pardon, I misunderstood your reply :(
When you said "you use" what came to me is that he uses playerList.get() in his Original post, then I look at OP's post and he did not use playerList.get().
- 05-21-2011, 08:22 AM #6
No matter, I was just offering a correction to his syntax. I edited my post to try and clear up any confusion.
Last edited by Dark; 05-21-2011 at 08:24 AM.
- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
- 05-21-2011, 08:32 AM #7
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
I as well. Anyway, hopefully OP will be able to solve it.
- 05-21-2011, 12:34 PM #8
Member
- Join Date
- May 2011
- Posts
- 15
- Rep Power
- 0
I still haven't been able to solve it how do I put the value of what I get from an array into a variable like:
int currentday = 0;
int totalday = 0;
currentday = playerList.get(0);
totalday = currentday + 1;
I get an error saying can't place Object into an Integer how do I fix this maybe I'm using the wrong kind of an array?
-
Again, it's not an array but an ArrayList. The difference is important which is why it has been discussed in several posts above. Have you read those posts? If you are confused by any of them, please ask for clarification before proceeding.
You are using a non-generic ArrayList which holds Objects. To make the Object an Integer, you'll have to cast it. Otherwise, if the ArrayList only holds Integers, then make it a generic one:I get an error saying can't place Object into an Integer how do I fix this maybe I'm using the wrong kind of an array?Java Code:int currentday = 0; int totalday = 0; currentday = playerList.get(0); totalday = currentday + 1;
Also (and again), please use code tags when posting code.Java Code:ArrayList<Integer> playerList = new ArrayList<Integer>();
- 05-21-2011, 02:02 PM #10
Changing your ArrayList to what Fubarable suggested at the end would take care of your problem. In Java 5.0 auto-boxing and unboxing came around to make everyone's life easier.
Your ArrayList<Integer> will store an Integer wrapper, which when you do current day = playerList.get(0) will return the int you stored in that position.
Does that make sense?- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
- 05-22-2011, 05:22 AM #11
Member
- Join Date
- May 2011
- Posts
- 15
- Rep Power
- 0
Thanks a lot guys!, I now see that I was confusing Arrays and Array Lists, I thought they were the same thing? Anyway at the moment I'm not just storing Integers in the array all except one of the data in my array are integers. For what I'm trying to do would it be better to use an array or an array list? Also if I move my String from my array into a new array I would be able to make my original array a integer generic one would that be a good idea?
Similar Threads
-
Adding all elements of an Array together, not adding up correctly
By Teclis in forum New To JavaReplies: 1Last Post: 04-05-2011, 08:58 PM -
Print variable data type
By BillyB in forum New To JavaReplies: 1Last Post: 01-14-2011, 04:37 PM -
Variable of an object in an array compared to an element of another array?
By asmodean in forum New To JavaReplies: 23Last Post: 09-07-2010, 08:12 PM -
Need Help: Adding a day in a variable using Jasper Reports
By bischmarck in forum New To JavaReplies: 1Last Post: 11-06-2009, 06:47 AM -
adding a variable in order to a list
By Jrr in forum New To JavaReplies: 2Last Post: 11-19-2007, 01:10 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks