473,385 Members | 1,342 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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:
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.

Reading that - I thought I'd 'got' the difference, but then in 10.1 (a
para later) the terms seem to used synonomously!
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.


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.***********@open.ac.uk
Jan 22 '06 #1
1 8671
"Rob Griffiths" <r.***********@open.ac.uk> wrote in message
news:r.*********************************@europe.is p.giganews.com...
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:
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.

Reading that - I thought I'd 'got' the difference, but then in 10.1 (a
para later) the terms seem to used synonomously!
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.


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.


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<Integer> has "Integer" as its element type.
ArrayList<TreeList<Comparable>> has TreeList<Comparable> 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:
€ 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.


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 getComponentType(), and they could not rename, or change
its behaviour, without breaking existing code.

- Oliver
Jan 23 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: ReignMan | last post by:
I'm running JDK 1.4.0_01, Xalan2.5.1 and Tomcat 4.1.27. XSL transformations work, yet when running in debug mode (Eclipse IDE), I get the following: 990S2html.xsl:3:80: Element type...
0
by: Leonardo Francalanci | last post by:
I wrote a function to sum arrays. It works, but I had to cast the data pointer to int64 (because my arrays are 'int8'): int64* ptr1 = ARR_DATA_PTR(v1); What if I want to write a more general...
2
by: Florian G. Pflug | last post by:
Hi Since sometime yesterday, my postgresql (7.4.5) reports "ERROR: cannot compare arrays of different element types", when I analyze a specific table in my database. Here is the tables...
6
by: libsfan01 | last post by:
hi all! is it possible to change an element type with javacript for example onclick to change a text box to a hidden element or to a textarea? kind regards marc
7
by: Michael Bray | last post by:
Let's say I am using reflection to analyze my classes, and I have an array of another class. How do I get the base class that forms the array from a type? public class A { } A MyArray =...
3
by: walex | last post by:
Hello guys, I'm trying to install devsec an application for ffpeg for camera,but on,is a c programme,after compilation ,i now type make, then this two error are generated.common.h:67: error: array...
1
by: shrav4ever | last post by:
Hi All, -------------------------------------------------------------------------------- This is a part of the code : -------------------------------------------------------------- extern...
1
by: am72de | last post by:
Hi all, I have to write an editor for Visual Studio. It's one editor for different kind of Arrays: Button, TextBox and so on. The intern array is Control, so I can use it for all of the...
10
by: arcadio | last post by:
Hi everyone, I'm currently struggling to compile a large piece of legacy code. GCC 3.3 compiles it without complaining, but GCC 4.2.3 (the default in Debian) refuses it and signals "several...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.