Hello. I have some question. Lets look at ArrayList. Method add(Object
o) is used for inserting objects into list, any object for example
String, int, doble etc. But there is method get(int index) for
returning values. Can I return double object without converting this
object into String? Something like this:
double d = list.get(3); Im know so that is not possible but tell my
why? Why I must: String s = list.get(3).toString(); Is this safety
problem? Please correct me if im wrong.
Darek Tarczynski, Poland
> Hello. I have some question. Lets look at ArrayList. Method add(Object
> o) is used for inserting objects into list, any object for example
[quoted text clipped - 6 lines]
>
> Darek Tarczynski, Poland
If you put doubles into the list you should get doubles out (though if you
are not using Java 5.0, you will have to mess around with conversion
between double and java.lang.Double and casting from java.lang.Object).
In Java 5.0:
List<Double> myDoubles = new ArrayList<Double>();
myDoubles.add(3.142);
double pi = myDoubles.get(0);
Dan.

Signature
Daniel Dyer
http://www.dandyer.co.uk
<dtarczynski@gmail.com> wrote...
> Lets look at ArrayList. Method add(Object
> o) is used for inserting objects into list,
[quoted text clipped - 3 lines]
> converting this object into String? Something like this:
> double d = list.get(3);
Yes you can, if you use the generics:
ArrayList<Double> list = new ArrayList<Double>();
list.add(1.0);
double d = list.get(0);
However, if you don't declare the ArrayList with generics, the compiler will
complain, as it then cannot be sure that every element in the ArrayList is
compatible with a Double.
In that case, you'll need to cast what you get to something that's
assignable to the type Object, in this case a Double, the wrapper class for
double, which in turn can be automatically converted to a primitive double,
with auto-unboxing.
ArrayList list = new ArrayList();
list.add(1.0);
double d = (Double) list.get(0);
> Im know so that is not possible but tell my
> why?
> Is this safety problem? Please correct me if im wrong.
Yes, it's a sort of safety problem.
In this case it's actually two issues in one.
1. All instances of any class is assignable to the "Object" type, but not
the other way around.
This means that you can do:
Object o = new Double(1.1);
But not:
Double d = new Object();
2. From 1.5 any primitive can be automatically "boxed" into its wrapper
class, and consequently unboxed.
But you cannot unbox from any other type than the actual wrapper class.
This means that you can do:
double d = new Double(1.1);
But not:
Object o = new Double(1.1);
double d = o;
In the case a reference is of type Double, you can still cast:
Object o = new Double(1.1);
double d = (Double) o;
Now, what does this have to do with the ArrayList?
When you *don't* use generics, all references in the ArrayList are of type
Object, which cannot be unboxed to primitives. However, primitives can be
*boxed* into Double and consequently, they can be assigned to references of
type Object.
That's why the following would work:
ArrayList list = new ArrayList();
list.add(1.1); // First autoboxed to Double, then inserted as Object
...but not this:
double d = list.get(0); // Object retrieved, but cannot unbox
When you *use* generics, you tell beforehand what types can (and cannot) be
referenced to by the list:
ArrayList<Double> list = new ArrayList<Double>();
list.add(1.1); // First autoboxed to Double, then inserted as Double
...and this:
double d = list.get(0); // Double retrieved, which is unboxed
> Why I must: String s = list.get(3).toString();
That's not a "must". BTW, the common approach isn't to use "toString"
anyway, but to cast to the actual type (unless it's something else, of which
you want the result of its toString method):
String s = (String) list.get(3);
But that's of course only valid if the list contains Strings.
If you use generics, it's even simpler:
ArrayList<String> list = new ArrayList<String>();
...
String s = list.get(3);
Finally, if you use the ArrayList to try to keep track of *different* type
of instances, you'll still need to retrieve them "the old way", with
casting, etc...
/// Bjorn A
dtarczynski@gmail.com - 07 Jun 2006 14:17 GMT
Bjorn Abelli napisal(a):
> <dtarczynski@gmail.com> wrote...
>
[quoted text clipped - 112 lines]
>
> /// Bjorn A
Thank you for all professional answers.
dimitar - 07 Jun 2006 16:33 GMT
> Bjorn Abelli napisal(a):
>> <dtarczynski@gmail.com> wrote...
[quoted text clipped - 112 lines]
>
> Thank you for all professional answers.
> Bjorn Abelli napisal(a):
>> <dtarczynski@gmail.com> wrote...
[quoted text clipped - 112 lines]
>
> Thank you for all professional answers.
> Bjorn Abelli napisal(a):
>> <dtarczynski@gmail.com> wrote...
[quoted text clipped - 112 lines]
>
> Thank you for all professional answers.
and thank you for making me scroll to the bottom of your post.
no need to go to the fittness today.