I am currently working on a project that requires using multiple ArrayLists in order to store and record data of an ongoing event.
However, I am having a great deal of difficulty actually extracting the variables and such out of the ArrayLists.
Say for example I had an ArrayList like the following:
I know how to store these, by using the ArrayList add() method. But I want to extract them, change them depending on event conditions, then restore the changed variables.
I tried using the get() method, but it doesn't seem to work.
For example what if I wanted to increment the variable and then put it back in the ArrayList?
Is there a way of doing this, or I am doing this wrong?
Thanks for all your help in advance everyone!
Hope to hear from you soon.
You cast it to the type that you put in. If you put Strings into the list, you'd cast to String.
Joined: Jul 05, 2011
Okay, I tried that, but instead NetBeans says that there is a problem here. If it makes a little easier, I am posting my code on here.
The problem is primarily with the method bets() at the end. It isn't finished yet and I haven't put in the return statement yet. But I am going to be doing the same thing with other methods in the future for this project. The methods will extract data stored in each of the ArrayLists about different players, change the values of the variables needed, and then store them back in the ArrayLists.
The Problem is on line 95 in the method bets(). Netbeans says that "variable declaration not allowed here". They are inconvertible types, it required type double but found Object- according to the error box anyway.
Any way of how I can get it to allow me to change the values and restore them after the changes are complete?
I am especially using ArrayLists because it will get to a point where players in the program will drop out of the event. Would be more efficient I thought anyway.
Sam Thompson wrote:The methods will extract data stored in each of the ArrayLists about different players, change the values of the variables needed, and then store them back in the ArrayLists.
It seems to me that you're concentrating far too much on coding and implementation, and not enough on design. I reckon that most of those pieces of information could be stored in a Player class, viz:and then all you'd need to do is keep oneArrayList of players. I'd also suggest that you look at your other pieces of information and see if they group together naturally (eg, a Hand).
BTW, you really should get used to adding types to your collections, ie:
List<Player> players = new ArrayList<Player>(); it'll save you a lot of casting.
Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
I also suggest you have a look at Java enums. They were specifically added to allow you to store information exactly like the above, and to do so in a type-safe manner.
Joined: Jul 05, 2011
Thank you so much for helping me out here. The problem was finally resolved. For future readers reading this thread, what I was trying to do was to extract data or values from an ArrayList, change it based on event condtions and then restore it back in the ArrayList.
However, I have a few more questions, though, outside the project. How exactly do you work with Java enums and how would you implement them? Especially in a progam like mine?
Sam Thompson wrote:How would enums work exactly in my program?
Sam, you really need to read the tutorials, because you will never work out how enums work "exactly in [your] program" unless you understand how they work in general.
I know you're probably just dying to bash out some more code, but it's a poor way to learn; and it's not how the pros do it.
Programming is about thinking, NOT coding.
Joined: Jul 05, 2011
I know. I understand what you are saying. I enjoy writing programs very much and I always have.
I will keep in mind what you said. Thank you very much for your help.
And thanks to everyone else who responded and contributed their suggestions and ideas.