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.
Example, to get the first element in your array list :
Its all considering it as an object and not an int or double variable like how I want it to so I can make changes to the values and then store them back in the ArrayList again.
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 one ArrayList 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.
Sam Thompson wrote:
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.
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: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.