Re: Amazing: Vector can have elements of different types!!!



Matt Humphrey wrote:

I have a couple questions:
(1)Is that common that people utilize this feature (putting different
types of data in one place)?


Yes, this is done all the time, is very useful and can be a terrible source of confusion (if not outright bad design) if not done properly.


Thank you very much. Patricia previously said that she usually doesn't put different types into one collection. Could you give me some examples that you would do so?

(2)Is there a way to find out what type of each element belongs to? (say
I forgot 1st element is Person, 2nd element is String, etc)

Yes, use instanceOf or getClass() on the object returned, realizing that doing so may signal something wrong with your design. You can see the confusion when you say "I forgot 1st element is a ..."

(3)Is this actually a drawback that Generics from Java 5.0 wants to cover up?

It's not so much that the ability to store objects of different types is a fault as it is a necessary fundamental building block that can be misused. Really, it's only storing one kind of object--subclasses of Object, but that declaration covers a lot of sins. Good designs most commonly simply refine the declaration so that the collection stores subclasses of some other class, such as Person, IMyOtherComponent, etc, so that there's no "forgetting" what the collection contained and casting the retrieval to the right kind of variable as a simple kind of check. Generics provide a syntax for that declaration, compiler support for checking it and some nice features like autoboxing to reduce unnecessary casting code.

You could say that part of the problem is that collections of any type (even with generics) do not have runtime constraints on the types of objects they can hold. But I believe that runtime type checking usually comes too late to be useful--it always signals a program error. The value of generics is that they validate code at compile time in order to prevent these messes at runtime.

Matt Humphrey matth@xxxxxxxxxxxxxx http://www.iviz.com/


.



Relevant Pages

  • Re: The universe looks designed to me
    ... Careful - there are cases where a feature can become common ... in a population without being a reproductive advantage. ... Not in your and my current environment - but there are several ...
    (talk.origins)
  • Re: Nice to have feature in upcoming Fortran compilers...
    ... I'm not sure what you mean by global COMMON. ... realize that there are old extensions using COMMON for this purpose. ... Perhaps you are arguing for requiring compiler diagnostics for all ... features to be adopted into the language as a kind of optional feature, ...
    (comp.lang.fortran)
  • Re: OT - Has Anyone Been Unable To Delete A Directory In XP?
    ... its a common "feature" in eXcrementPile) ... In all fairness, I didn`t have any luck on the one machine I tried it ...
    (uk.media.dvd)
  • Re: Regular Expressions Question.
    ... > Recognizing e-mail addresses via regular expressions is actually ... but also a common enough feature that ...
    (comp.lang.java.programmer)