C
Chase Preuninger
I wonder what would would happen if u would create a class extending
an array.
class CustomArray extends Object[ ]
{
}
an array.
class CustomArray extends Object[ ]
{
}
Chase said:I wonder what would would happen if u would create a class extending
an array.
class CustomArray extends Object[ ]
{
}
Probably you could override these two methodsChase said:i know, just wondering if it would work
Probably you could override these two methodsChase said:i know, just wondering if it would work
public Object operator[](int index);
public void operator[](int index, Object value);
But you could not change the 'length' because it is final.
Jan said:Probably you could override these two methodsChase said:i know, just wondering if it would work
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 ?
Andrea Francia said:Hypothetic Java
I wonder what would would happen if u would create a class extending
an array.
I am not took good at reflection, but would it be possible the change
the size of an array with reflection.
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.
Yes, but you'll get a surprise if you look for the "Field" using reflection.Lew said:<http://java.sun.com/docs/books/jls/third_edition/html/arrays.html#10.7>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,
From the Java language point of view, the array most definitely does
have a field called 'length'. It says so right in the JLS.
Sorry, my use of the word "primitive" was ambiguous... I meant primitiveOK, 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.
From the Java language point of view, the array most definitely does have a
field called 'length'. It says so right in the JLS.
List<MyType> myFixedSizeList = Arrays.asList(new MyType[32]);Lew said:Any place you'd use ArrayList but not change its size once filled.
My point is that the array should be wrapped by a more specific classLew said:Daniel said:List<MyType> myFixedSizeList = Arrays.asList(new MyType[32]);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.
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.
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.