Extending Arrays

Discussion in 'Java' started by Chase Preuninger, Apr 17, 2008.

  1. I wonder what would would happen if u would create a class extending
    an array.

    class CustomArray extends Object[ ]
    {
    }
    Chase Preuninger, Apr 17, 2008
    #1
    1. Advertising

  2. Chase Preuninger wrote:
    > I wonder what would would happen if u would create a class extending
    > an array.
    >
    > class CustomArray extends Object[ ]
    > {
    > }


    It would be never more useful than implementing List.

    class CustomArray implements List {
    }

    --
    Andrea Francia
    http://www.andreafrancia.it/
    Andrea Francia, Apr 17, 2008
    #2
    1. Advertising

  3. i know, just wondering if it would work
    Chase Preuninger, Apr 17, 2008
    #3
  4. Chase Preuninger wrote:
    > i know, just wondering if it would work

    Probably you could override these two methods

    public Object operator[](int index);
    public void operator[](int index, Object value);

    But you could not change the 'length' because it is final.

    --
    Andrea Francia
    http://www.andreafrancia.it/
    Andrea Francia, Apr 17, 2008
    #4
  5. Chase Preuninger

    Jan Thomä Guest

    On Thu, 17 Apr 2008 22:06:49 GMT Andrea Francia wrote:

    > Chase Preuninger wrote:
    >> i know, just wondering if it would work

    > Probably you could override these two methods
    >
    > public Object operator[](int index);
    > public void operator[](int index, Object value);
    >
    > But you could not change the 'length' because it is final.


    Errm, we are talking Java here, right ?
    Jan Thomä, Apr 17, 2008
    #5
  6. Jan Thomä wrote:
    > On Thu, 17 Apr 2008 22:06:49 GMT Andrea Francia wrote:
    >
    >> Chase Preuninger wrote:
    >>> i know, just wondering if it would work

    >> Probably you could override these two methods
    >>
    >> public Object operator[](int index);
    >> public void operator[](int index, Object value);
    >>
    >> But you could not change the 'length' because it is final.

    >
    > Errm, we are talking Java here, right ?


    Hypothetic Java

    --
    Andrea Francia
    http://www.andreafrancia.it/
    Andrea Francia, Apr 17, 2008
    #6
  7. Andrea Francia <> writes:

    > Jan Thomä wrote:
    >> Errm, we are talking Java here, right ?

    >
    > Hypothetic Java


    I hypothetic Java, you can do anything you want :)

    In real Java, you are very far from being able to extend array
    types or overload operators.

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Apr 18, 2008
    #7
  8. Chase Preuninger

    Roedy Green Guest

    On Thu, 17 Apr 2008 13:17:41 -0700 (PDT), Chase Preuninger
    <> wrote, quoted or indirectly quoted someone
    who said :

    >I wonder what would would happen if u would create a class extending
    >an array.


    You have two problems:

    the name of the class is no accessible in ordinary java. You would
    have to write byte code.

    The class is almost certainly final.

    Consider writing an HAS-A rather than IS-A implementation, the way
    ArrayList does.
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Apr 18, 2008
    #8
  9. I am not took good at reflection, but would it be possible the change
    the size of an array with reflection.
    Chase Preuninger, Apr 18, 2008
    #9
  10. Chase Preuninger

    Jan Thomä Guest

    On Thu, 17 Apr 2008 19:01:49 -0700 (PDT) Chase Preuninger wrote:

    > I am not took good at reflection, but would it be possible the change
    > the size of an array with reflection.


    No. If you want to have resizable collections you should go for an
    ArrayList or something similar. That's what they are made for.

    Jan
    Jan Thomä, Apr 18, 2008
    #10
  11. Chase Preuninger

    Daniel Pitts Guest

    Roedy Green wrote:
    > On Thu, 17 Apr 2008 13:17:41 -0700 (PDT), Chase Preuninger
    > <> wrote, quoted or indirectly quoted someone
    > who said :
    >
    >> I wonder what would would happen if u would create a class extending
    >> an array.

    >
    > You have two problems:
    >
    > the name of the class is no accessible in ordinary java. You would
    > have to write byte code.
    >
    > The class is almost certainly final.
    >
    > Consider writing an HAS-A rather than IS-A implementation, the way
    > ArrayList does.


    A Java array is actually *not* a real class. It is a special type of
    primitive. From the JVM point of view, it does not have a field called
    length, there is actually a special op-code to access the array length...

    Also, an array reference is assignable to Object, java.io.Serializable,
    and Clonable reference variables, but in many regards is very different
    than other "classes".

    The other thing to consider is that arrays are low-level
    building-blocks, and you should avoid using them in all but the
    lowest-level abstractions (such as Collection API implementations).
    Anything else should use abstractions (such as the Collections API) in
    place of these primitives.


    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
    Daniel Pitts, Apr 20, 2008
    #11
  12. Chase Preuninger

    Daniel Pitts Guest

    Lew wrote:
    > Daniel Pitts wrote:
    >> A Java array is actually *not* a real class. It is a special type of
    >> primitive. From the JVM point of view, it does not have a field
    >> called length, there is actually a special op-code to access the array
    >> length...

    [snip]
    >>> The members of an array type are all of the following:
    >>>
    >>> * The public final field length,

    > <http://java.sun.com/docs/books/jls/third_edition/html/arrays.html#10.7>
    >
    > From the Java language point of view, the array most definitely does
    > have a field called 'length'. It says so right in the JLS.

    Yes, but you'll get a surprise if you look for the "Field" using reflection.
    >
    >> Also, an array reference is assignable to Object,
    >> java.io.Serializable, and Clonable reference variables, but in many
    >> regards is very different than other "classes".

    >
    >>> Every array has an associated Class object, shared with all other
    >>> arrays with the same component type.

    > <http://java.sun.com/docs/books/jls/third_edition/html/arrays.html#10.8>
    >
    >> The other thing to consider is that arrays are low-level
    >> building-blocks, and you should avoid using them in all but the
    >> lowest-level abstractions (such as Collection API implementations).
    >> Anything else should use abstractions (such as the Collections API) in
    >> place of these primitives.

    >
    > OK, that's *a* rule of thumb. I would venture to say that there are
    > many more use cases for arrays. Also, it isn't accurate to refer to
    > arrays as "primitives"; arrays are reference (i.e., Object) types, not
    > primitives.
    >

    Sorry, my use of the word "primitive" was ambiguous... I meant primitive
    as in a low-level building block of the language that couldn't feasibly
    be created from within the language itself. I guess you could call it a
    second-level primitive, where references and numeric primitives are
    first-level.

    What other use-case for arrays can you think of that shouldn't be
    wrapped by a more specific class?


    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
    Daniel Pitts, Apr 21, 2008
    #12
  13. Chase Preuninger

    Roedy Green Guest

    On Sun, 20 Apr 2008 15:22:47 -0400, Lew <> wrote,
    quoted or indirectly quoted someone who said :

    > From the Java language point of view, the array most definitely does have a
    >field called 'length'. It says so right in the JLS.


    You are each talking different levels here. At the source code level,
    which the JLS describes, an array is very much like a class, except it
    is missing a name.

    At the JVM level, which the JVM spec describes, arrays are
    specialised classes with op codes for dealing with them. See
    http://mindprod.com/jgloss/jasm.html
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Apr 22, 2008
    #13
  14. Chase Preuninger

    Daniel Pitts Guest

    Lew wrote:
    > Daniel Pitts wrote:
    >> What other use-case for arrays can you think of that shouldn't be
    >> wrapped by a more specific class?

    >
    > Any place you'd use ArrayList but not change its size once filled.
    >

    List<MyType> myFixedSizeList = Arrays.asList(new MyType[32]);

    Even if you didn't want to wrap it with List semantics, you would
    probably want to wrap it with something specific.

    Also, thats not a real use-case, thats simply an example of something
    that standard ArrayList doesn't fix as is.

    One case I use an array is for a high-performant Vector3D class, where
    the x,y,z components are all stored in a double[]. However, this fact
    does not/should not leak out of the Vector3D interface. Therefor,
    Vector3D does not expose the fact that its using the Array primitive.
    It does, however, expose that its using a double :) Can't win it all :)

    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
    Daniel Pitts, Apr 22, 2008
    #14
  15. Chase Preuninger

    Daniel Pitts Guest

    Lew wrote:
    > Daniel Pitts wrote:
    >> Lew wrote:
    >>> Daniel Pitts wrote:
    >>>> What other use-case for arrays can you think of that shouldn't be
    >>>> wrapped by a more specific class?
    >>>
    >>> Any place you'd use ArrayList but not change its size once filled.
    >>>

    >> List<MyType> myFixedSizeList = Arrays.asList(new MyType[32]);
    >>
    >> Even if you didn't want to wrap it with List semantics, you would
    >> probably want to wrap it with something specific.

    >
    > Probably not, actually.
    >
    >> Also, thats not a real use-case, thats simply an example of something
    >> that standard ArrayList doesn't fix as is.

    >
    > It's a real use case. There's no need for ArrayList or anything else to
    > "fix" it because it isn't broken. Arrays are simple enough to use that
    > when they fit, there's no reason to go hunting around for more
    > complicated solutions.
    >
    >> One case I use an array is for a high-performant Vector3D class, where
    >> the x,y,z components are all stored in a double[].

    >
    > Ok, so you didn't need my help answering your own question.
    >

    My point is that the array should be wrapped by a more specific class
    (as my Vector3D wrapped the double[]). Not that they shouldn't ever be
    used.

    My point is more that they shouldn't be passed from one class onto
    another (except maybe to the java.util.Arrays class).
    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
    Daniel Pitts, Apr 23, 2008
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexandra Stehman
    Replies:
    5
    Views:
    30,507
    Chris Smith
    Jun 17, 2004
  2. Bill Reyn
    Replies:
    3
    Views:
    2,216
    Bob Hairgrove
    Jun 22, 2004
  3. Mantorok Redgormor

    initializing arrays of arrays

    Mantorok Redgormor, Sep 10, 2003, in forum: C Programming
    Replies:
    4
    Views:
    547
  4. Soeren Sonnenburg
    Replies:
    11
    Views:
    491
    Soeren Sonnenburg
    Jul 25, 2005
  5. Philipp
    Replies:
    21
    Views:
    1,093
    Philipp
    Jan 20, 2009
Loading...

Share This Page