Arrays - component type vs Element type

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rob Griffiths

    #1

    Arrays - component type vs Element type

    Can anyone explain to me the difference between an element type and a
    component type?

    In the java literature, arrays are said to have component types, whereas
    collections from the Collections Framework are said to have an element
    type.


    http://java.sun.com/docs/books/jls/s...rrays.doc.html

    states:
    [color=blue]
    > The *component type* of an array may itself be an array type. The components of
    > such an array may contain references to subarrays. If, starting from any
    > array type, one considers its component type, and then (if that is also an
    > array type) the component type of that type, and so on, eventually one must
    > reach a component type that is not an array type; this is called the *element
    > type* of the original array, and the components at this level of the data
    > structure are called the elements of the original array.[/color]


    Reading that - I thought I'd 'got' the difference, but then in 10.1 (a
    para later) the terms seem to used synonomously!
    [color=blue]
    > An array type is written as the name of an *element type* followed by some
    > number of empty pairs of square brackets []. The number of bracket pairs
    > indicates the depth of array nesting. An array's length is not part of its
    > type.
    > The element type of an array may be any type, whether primitive or reference.
    > In particular:
    >
    > € Arrays with an interface type as the *component type* are allowed. The
    > elements of such an array may have as their value a null reference or
    > instances of any type that implements the interface.
    > € Arrays with an abstract class type as the *component type* are allowed.
    > The elements of such an array may have as their value a null reference or
    > instances of any subclass of the abstract class that is not itself
    > abstract.[/color]

    When describing collections from the Collections framework there is
    never any of this ambiguity/distinction - it is always "element type",
    even when talking about a collection whose elements are themselves
    collections.

    Am I missing a "big idea"?

    Any thoughts greatly appreciated.

    Rob

    r.w.griffiths@o pen.ac.uk
  • Oliver Wong

    #2
    Re: Arrays - component type vs Element type

    "Rob Griffiths" <r.w.griffiths@ open.ac.uk> wrote in message
    news:r.w.griffi ths-960561.22552522 012006@europe.i sp.giganews.com ...[color=blue]
    > Can anyone explain to me the difference between an element type and a
    > component type?
    >
    > In the java literature, arrays are said to have component types, whereas
    > collections from the Collections Framework are said to have an element
    > type.
    >
    >
    > http://java.sun.com/docs/books/jls/s...rrays.doc.html
    >
    > states:
    >[color=green]
    >> The *component type* of an array may itself be an array type. The
    >> components of
    >> such an array may contain references to subarrays. If, starting from any
    >> array type, one considers its component type, and then (if that is also
    >> an
    >> array type) the component type of that type, and so on, eventually one
    >> must
    >> reach a component type that is not an array type; this is called the
    >> *element
    >> type* of the original array, and the components at this level of the data
    >> structure are called the elements of the original array.[/color]
    >
    >
    > Reading that - I thought I'd 'got' the difference, but then in 10.1 (a
    > para later) the terms seem to used synonomously!
    >[color=green]
    >> An array type is written as the name of an *element type* followed by
    >> some
    >> number of empty pairs of square brackets []. The number of bracket pairs
    >> indicates the depth of array nesting. An array's length is not part of
    >> its
    >> type.
    >> The element type of an array may be any type, whether primitive or
    >> reference.
    >> In particular:
    >>
    >> € Arrays with an interface type as the *component type* are allowed.
    >> The
    >> elements of such an array may have as their value a null reference or
    >> instances of any type that implements the interface.
    >> € Arrays with an abstract class type as the *component type* are
    >> allowed.
    >> The elements of such an array may have as their value a null reference
    >> or
    >> instances of any subclass of the abstract class that is not itself
    >> abstract.[/color]
    >
    > When describing collections from the Collections framework there is
    > never any of this ambiguity/distinction - it is always "element type",
    > even when talking about a collection whose elements are themselves
    > collections.
    >
    > Am I missing a "big idea"?
    >
    > Any thoughts greatly appreciated.[/color]

    Warning: I haven't read the latest JLS (I believe the one I read was for
    Java 1.4), so I don't know if what changes there have been, but...

    Arrays are treated specially in a lot of places in Java, and so some OO
    purist recommend avoiding arrays altogether (and just using the ArrayList
    class if you need array-like functionality). The syntax around arrays treat
    them half like objects, half like primitive values. As far as I can recall,
    only arrays have a "component type". So let me first start by definit
    element type, and then I'll come back to "component type".

    Collections are basically "a bunch of elements". The collection itself
    has a type (e.g. "Vector", "ArrayList" , "HashMap", etc.), and the elements
    it contains also has a type. In 1.4 and prior, that type was always Object,
    but starting from 1.5, using generics, you could narrow down the type of
    elements. For example, ArrayList<Integ er> has "Integer" as its element type.
    ArrayList<TreeL ist<Comparable> > has TreeList<Compar able> as its element
    type.

    The parallel concept in array is "component type". The component type of
    String[] is String. The component type of JTree[][][] is JTree[][]. The
    *element* type of JTree[][][] is JTree. Confusing, yes, I know. They've
    essentially swapped the meaning for "element type" between arrays and
    collections.

    Regarding this passage:[color=blue][color=green]
    >> € Arrays with an interface type as the *component type* are allowed.
    >> The
    >> elements of such an array may have as their value a null reference or
    >> instances of any type that implements the interface.[/color][/color]

    Note that String is both the element type AND the component type of
    String[]. So when they say "Arrays with an interface type as the component
    type", that it equivalent (in all situations I could think of) of saying
    "Arrays with an interface type as the element type".

    However, there is a difference between "Arrays with an array as the
    component type" and "Arrays with an array as the element type", with the
    latter not making sense (the element type should never be an array).

    I believe this strange naming system may be a "stuck" for backwards
    compatibility reason. When you're using the reflections API, the class Array
    has a method called getComponentTyp e(), and they could not rename, or change
    its behaviour, without breaking existing code.

    - Oliver


    Comment

    Working...