aspose file tools*
The moose likes Java in General and the fly likes Extracting and using items in ArrayLists Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Extracting and using items in ArrayLists" Watch "Extracting and using items in ArrayLists" New topic
Author

Extracting and using items in ArrayLists

Sam Thompson
Ranch Hand

Joined: Jul 05, 2011
Posts: 93
    
    1
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

Joined: Jul 24, 2012
Posts: 47

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

Joined: Jan 10, 2002
Posts: 61413
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Sam Thompson
Ranch Hand

Joined: Jul 05, 2011
Posts: 93
    
    1
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

Joined: Jan 10, 2002
Posts: 61413
    
  67

You'll either need to use a cast, or use generics.
Sam Thompson
Ranch Hand

Joined: Jul 05, 2011
Posts: 93
    
    1
How do I do that exactly?

Thanks

S.T.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

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

Joined: Jul 05, 2011
Posts: 93
    
    1
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

Joined: Jan 10, 2002
Posts: 61413
    
  67

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

Joined: Jul 05, 2011
Posts: 93
    
    1
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

Joined: Jul 23, 2008
Posts: 296

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

Joined: Aug 09, 2012
Posts: 179
jatan bhavsar wrote:Hi Sam,




I did not go through all of it but I bet is going to be problematic.
switch to instead.


Programming is about thinking, NOT coding
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

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

Joined: Jul 05, 2011
Posts: 93
    
    1
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

Joined: Mar 17, 2011
Posts: 8008
    
  22

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

Joined: Jul 05, 2011
Posts: 93
    
    1
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

Joined: Oct 13, 2005
Posts: 39393
    
  28
Have a look at the Java Language Specification section.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

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

Joined: Jul 05, 2011
Posts: 93
    
    1
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Extracting and using items in ArrayLists