Extending Arrays

C

Chase Preuninger

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

class CustomArray extends Object[ ]
{
}
 
A

Andrea Francia

Chase said:
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 {
}
 
A

Andrea Francia

Chase said:
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.
 
J

Jan Thomä

Chase said:
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 ?
 
A

Andrea Francia

Jan said:
Chase said:
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
 
L

Lasse Reichstein Nielsen

Andrea Francia said:
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
 
R

Roedy Green

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

Chase Preuninger

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

Jan Thomä

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
 
D

Daniel Pitts

Roedy said:
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.
 
D

Daniel Pitts

Lew said:
Daniel said:
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.
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?
 
R

Roedy Green

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
 
D

Daniel Pitts

Lew said:
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 :)
 
D

Daniel Pitts

Lew said:
Daniel said:
Lew said:
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).
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top