• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Extracting and using items in ArrayLists

 
Sam Thompson
Ranch Hand
Posts: 93
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone

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.

S.T.
 
Guillaume Jourdan
Ranch Hand
Posts: 47
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You must specify the index in get :
get(index)

Example, to get the first element in your array list :
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65124
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sam Thompson wrote:I tried using the get() method, but it doesn't seem to work.

This is not a useful description of what is happening.
 
Sam Thompson
Ranch Hand
Posts: 93
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I mean is that the program I am using to write my code (I am using netbeans) keeps telling me that there are incompatible types.

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.

S.T.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65124
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll either need to use a cast, or use generics.
 
Sam Thompson
Ranch Hand
Posts: 93
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I do that exactly?

Thanks

S.T.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65124
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't know how to cast? Let's say that the list contains Doubles. Then: Double value = (Double)list.get(0);

If casting is new to you, it might be too early to get into generics.
 
Sam Thompson
Ranch Hand
Posts: 93
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh you mean typecasting? I know what that is now .

But are you sure that would work if the element you are extracting from the arraylist is a general object. Is it going to change the nature or value of the variable that I want?

S.T.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65124
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cast it to the type that you put in. If you put Strings into the list, you'd cast to String.
 
Sam Thompson
Ranch Hand
Posts: 93
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

S.T.
 
jatan bhavsar
Ranch Hand
Posts: 299
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sam,

Modify the code for the method bets as below.



Return type of the method is Array of double so you need to declare the array of double and return it from the method. Size of the double array can be taken from the player.length.

Regards
jatan
 
Vishal Shaw
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jatan bhavsar wrote:Hi Sam,




I did not go through all of it but I bet is going to be problematic.
switch to instead.
 
Winston Gutkowski
Bartender
Pie
Posts: 10509
64
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Winston
 
Winston Gutkowski
Bartender
Pie
Posts: 10509
64
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Winston
 
Sam Thompson
Ranch Hand
Posts: 93
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Everyone:

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?

S.T.
 
Winston Gutkowski
Bartender
Pie
Posts: 10509
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Start here.

Winston
 
Sam Thompson
Ranch Hand
Posts: 93
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would enums work exactly in my program?

All I want the program to do is to simply record the hands every time the event is over.

S.T.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49827
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at the Java Language Specification section.
 
Winston Gutkowski
Bartender
Pie
Posts: 10509
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Winston
 
Sam Thompson
Ranch Hand
Posts: 93
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston:

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.

S.T.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic