Casting from an object to an int
Hello hope someone can help me ?
I have a list (linkedlist) with a number of objects
Each object is an int
I would like to convert some of these objects into ints to use elsewhere
However when I try I always get an error
Can anyone tell me how to cast /convert from an object (containing an int) to an int?
Thanks for any adive :)
[375 byte] By [
judesa] at [2007-11-27 1:03:12]

> Hello hope someone can help me ?> > I have a list (linkedlist) with a number of objectsIs it a standard LinkedList or something you've made yourself?
judesa at 2007-7-11 23:38:16 >

just a standard linkedlistI initially filled it with a series of array objects but then coul创nt extract them so thought maybe might be easier to convert from an object to an int?
judesa at 2007-7-11 23:38:16 >

I'm not quite sure what your problem is.
If you store objects of a class in the LinkedList you just store them and retrieve them and access whatever information they hold. If you're using generics you don't have to cast at all.
If you want to store ints it's a special case. These will be automatically converted to their corresponding wrapper class, namely Integer, when added to the LinkedList and then back to int again when you retrieve them back from the LinkedList. You declare the LinkedList like this,
LinkedList<Integer> ll = new LinkedList<Integer>();
Then it's just to start adding/retrieving ints.
judesa at 2007-7-11 23:38:16 >

The automatic conversion (between primitives and their corresponding wrapper classes) is called autoboxing. There's an example here, http://java.sun.com/developer/JDCTechTips/2005/tt0405.html#1
judesa at 2007-7-11 23:38:16 >

> If you want to store ints it's a special case. These
> will be automatically converted to their
> corresponding wrapper class, namely Integer, when
> added to the LinkedList and then back to int again
> when you retrieve them back from the LinkedList. You
> declare the LinkedList like this,...
This is true if you are using java 1.5 or 1.6. If you are required to use java 1.4 then you will have to box and unbox the integers yourself.
I think that a linked list will only take objects, so if you want to add the integer "4" you'd have to do:
myList.add(new Integer(4));
for a linked list w/ 1.4, you'd have to do something like this:
public static void main(String[] args)
{
LinkedList myLList = new LinkedList();
Integer myInteger = new Integer(4);// box the int as an Integer object
myLList.add(myInteger);
int myInt = ((Integer)myLList.getFirst()).intValue(); // cast and unbox to get back int
System.out.println(myInt);
}
Hi
Thanks for the help
Sorry if am a little fuzzy am just starting to get a grip on it
My problem is a little clrearer at leat to me now :)
I have create a linkedlist and created an object called cars and it is this object cars which holds an int
And my porblme is to then get the int from this object as I need to populate an array with it?
Thanks
judesa at 2007-7-11 23:38:16 >

Which version of Java are you using? If 1.5 or 1.6 (or version 5 or 6) then I'd use a linked list of the object as Ronda suggested. If the class is called Car, then something like this:LinkedList<Car> myCarLL = new LinkedList<Car>();
Hi
I am using version 5
I have created a linkedlist using code as you suggested
i.e. LinkedList<Car> myCarLL = new LinkedList<Car>();
I then have a LinkedList e.g. (car1, car2,car3)
I can create an object of an item from the list e.g.
Object test = myCarLL.get(0)
but how to I then take this and and cast(convert) it to an int
I am getting confused on how this can be done
Thanks for the advice
judesa at 2007-7-11 23:38:16 >

> I can create an object of an item from the list e.g.
>
> Object test = myCarLL.get(0)
>
> but how to I then take this and and cast(convert) it
> to an int
It depends on the structure of Car. If it is structured like so:
class Car
{
int carNumber;
String name;
public Car(int carInt, String name)
{
this.carNumber = carInt;
this.name = name;
}
public int getCarNumber()
{
return carNumber;
}
public String getName()
{
return name;
}
}
Then you'd call:
int myInt = myCarLL.get(0).getCarNumber();