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?
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 ]
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
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
Joined: Jun 20, 2005
Posts: 83
posted
0
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);
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.