Should Java treat arrays as objects?

I have often thought it would be a good idea to allow the use of arrays as proper objects with their own methods instead of relying on helper classes like Arrays, Arrays and ArrayUtils.

For example:

ints.sort();                 // Arrays.sort(ints);
int[] onemore = ints.add(8); // int[] onemore = ArrayUtils.add(ints, 8);

I am sure I am not the first with this idea but I have had trouble searching for others who have written about this before. Can anyone help me with some references on this topic?

Is this thought to be a good or bad idea, and why?

How easy would this be to implement?

Some other examples might include (but don't get hung up on them, they're extraneous to the question itself):

int[] ints = {5,4,3,2,1};

// Arrays.sort (ints);
ints.sort();

// int pos = Arrays.asList(ints).indexOf (5);
// int pos = ArraysUtils.indexOf (ints, 5);
int pos = ints.indexOf (5);

// Arrays.reverse (ints);
ints.reverse();

Array<Integer> array = ints; // cast to super class.

// int length = Array.getLength (array);
int length = array.getLength();

// Object n = Array.get (array, 3);
Object n = array.get (3);

// Array.set (array, 3, 7);
array.set (3, 7);

Object obj = array;
// if (obj instanceof int[])
//     System.out.println(Array.toString((int[]) obj));
// else if (....)
System.out.println (obj);

Arrays are not classes in Java for a good reason - they map well onto people's understanding of how an array should work from experience with C-style languages. There are also performance reasons for making arrays low-level containers rather than objects. Because of this, sometimes there are performance benefits to using a primitive array rather than a Collection.

If you want to use objects, you should just use a Collection (an ArrayList is an example of a collection). It can be clunky, but Collections provide the type of nice methodological access that you seem to want.


Those methods start to look an awful lot like ruby or python idioms. Unfortunately you don't get to do that in java (wish you could).

For one, as others have pointed out, the collections classes do it for you. For another, myarray.sort() isn't so nice because you can create arrays of objects for which sorting has not been defined. Suppose I have

 Foo[] foos; 

And Foo is not Comparable. What happens on foos.sort()? We definitely wouldn't want to have it only work for primitives

int[] ints; 
ints.sort(); //legal
Object[] objects;
objects.sort(); //illegal

and you certainly couldn't have the compiler only allow the syntax for comparable objects. And once you get to something like

 myarray.add(new Foo());

it's sort of pointless, as arrays in java aren't growable.

It would be nice if printing out an array didn't give you that useless

([I'm an array(*&(*

rubbish, though.


Yes. But I'm of the opinion that Java should not have any primitives at all . I think primitives in Java are a break from the cleanness of the language. Everything in Java should be objects. Whether or not the objects are allocated on the stack or the heap should be an implementation detail of the JVM not a language construct. But I think my opinion might be more radical than most.

Before autoboxing, dealing with primitives and objects was very cumbersome.

If arrays were objects and could be autoboxed (and generisized!), we could have something like

Array<Integer> myArray = new Integer[];
myArray.add(8);
int x = myArray[0];
....

or

Array<Class<? extends MyBaseObject>> myArray = {ExtendedObject.class}
ExtendedObject object = myArray[0].newInstance();
....
链接地址: http://www.djcxy.com/p/76104.html

上一篇: 为什么当我们已经有矢量时需要同步ArrayList?

下一篇: Java应该将数组视为对象吗?