• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayList data manipulation

 
Tempora Telora
Ranch Hand
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys,
I am having problem with data manipulation when using ArrayLists. I know that when an item is stored in an arraylist it is stored as an object. But how do i change that object to an int or something? heres an example of my code


it def. does not like the temp.get(k) == 1 and hte double.parseDouble. Can anyone tell me how to compare the index k with 1 and add the items in list?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Randy Tatham:
... I know that when an item is stored in an arraylist it is stored as an object. But how do i change that object to an int or something? ...

I think the real question is: What kind of object are you putting in the ArrayList? That will determine how you get an int value from it later.

If all you're doing is putting "int values" into an ArrayList, then you will probably want to wrap them as Integer objects.

Note that whenever you get an Object from a List, you need to explicitly cast it back down to its proper type. For example...

Integer myIntObject = (Integer)myList.get(x);
[ September 16, 2005: Message edited by: marc weber ]
 
Tempora Telora
Ranch Hand
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if i store them in as a string will that matter?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Randy Tatham:
if i store them in as a string will that matter?

It would change the way you get a numeric int from it later, if you want to do numeric operations.

If you have String objects, then you would probably use Integer.parseInt(myString) to get an int value (after you've cast the Object back down to type String).
[ September 16, 2005: Message edited by: marc weber ]
 
Tempora Telora
Ranch Hand
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I keep getting this

C:\Program Files\Xinox Software\JCreator LE\MyProjects\Recursion\Methods.java:69: inconvertible types
found : java.lang.Object
required: double
total = (double)list.get(k);
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Randy Tatham:
I keep getting this

C:\Program Files\Xinox Software\JCreator LE\MyProjects\Recursion\Methods.java:69: inconvertible types
found : java.lang.Object
required: double
total = (double)list.get(k);

list.get(k) is of type Object. If you originally put it into the List as a String, then you need to explicitly cast it back to String when you take it out. Then you can parse that String as a primitive double.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic