Should Java treat arrays as objects?

I often thought that it would be nice to let arrays be used as the right objects with my own methods instead of relying on helper classes like arrays, arrays, and ArrayUtils objects.

For example:

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

I am sure that I am not the first with this idea, but I had problems finding others who wrote about this before. Can someone help me with some links on this topic?

Is this idea a good or bad idea and why?

How easy is it to implement?

Some other examples may include (but not depend on them, they are extraneous to the issue 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); 
+7
java arrays
source share
10 answers

Arrays are not Java classes for a good reason - they display people well who understand how an array should work with the C language. There are also reasons for creating low-level containers, not objects. Because of this, performance advantages sometimes arise when using a primitive array rather than a collection.

If you want to use objects, you just have to use Collection (ArrayList is an example of a collection). This may be awkward, but Collections provide the type of good methodological access you think you need.

+7
source share

These methods are starting to look awfully similar to rubies or python idioms. Unfortunately, you cannot do this in java (preferably you could).

First, as others have noted, collection classes do this for you. For another, myarray.sort () is not so good because you can create arrays of objects for which sorting is not defined. Let's pretend that

  Foo[] foos; 

And Foo is not comparable. What happens on foos.sort ()? We definitely would not want it to work only for primitives.

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

and you, of course, could not force the compiler to allow syntax for comparable objects. And once you achieve something like

  myarray.add(new Foo()); 

this is useless since arrays in java don't grow up.

It would be nice if the listing of the array did not give you this useless

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

trash.

+5
source share

Yes. But I believe that Java should not have any primitives at all . I think Java primitives are a break with the purity of the language. Everything in Java should be objects. Regardless of whether objects are allocated on the stack, or the heap should be a detail of the JVM implementation , not a language construct. But I think that my opinion may be more radical than most.

Before autoboxing, processing primitives and objects was very cumbersome.

If arrays were objects and could be autoboxes (and generalized!), 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(); .... 
+3
source share

Before answering NEXT, I will tell you about the status of this problem.

In Java, arrays are considered objects - they can be stored in a list, for example. However, they are special objects in that they inherit the object, but they are not created or accessed with the same syntax (thanks for correcting Peter). They are aggressively optimized by the JVM for obvious performance issues, and thus the way the array is stored is implementation dependent.

So Sun’s argument will be that if they provided an object API for the array, it might require some hardware, software, or other specification functions.

The only real interface for arrays is the static System.arrayCopy method or the static Array methods. * which will be most efficiently copied to / from arrays.

In response, SHOULD; it was resolved, although the standard would be better than the answer: use an ArrayList.

+2
source share

Yes, I believe that arrays should have an API defined outside the limits specified in the language itself. In particular, it is annoying that a Foo[] does not implement Iterable<Foo> . Yes, I know it’s easy to wrap, but it’s annoying what you need.

In .NET, this is basically the right one, an Array type, which is mainly intended for non-shared access, and various common interfaces that are thought to be implemented using real arrays. For example:

 IList<int> intList = new int[10]; 

(This helps .NET generators handle primitive types better than Java.)

The only drawbacks I saw in this approach is that arrays can be covariant in .NET, but there are no normal generics, and that things get a little confused with non-zero arrays and rectangular arrays.

As an aside, various people referred to arrays as primitives in this thread. Although they certainly have special processing, they are not defined as primitives. From the language specification, section 4.2 :

The primitive type is predefined by the Java programming language and its reserved keyword is called (§3.9):

 PrimitiveType:
         Numerictype
         boolean

 NumericType:
         IntegralType
         FloatingPointType

 IntegralType: one of
         byte short int long char

 FloatingPointType: one of
         float double
+2
source share

This is not a bad idea. The fact is that this has already been done in collections. Try expanding ArrayList if you want to go crazy.

+1
source share

I believe that it is very difficult to implement without breaking compatibility at many levels.

  • The JVM processes arrays a little differently than other objects. Creation / initialization is different: no constructor is called for one thing. How do you want to add new methods? If adding a superclass would you call it a constructor?
  • Arrays know the type of element at runtime; there are no generics. If you want to add a new superclass for all arrays (for example, you suggested in the original question), would you make it general? Also keep in mind that arrays are covariant in Java, there are no generics.
  • Arrays have a fixed set of methods / fields specified in the Java Language Specification (all methods from objects that are explicitly specified in JLS, length field). Depending on this, adding new members is likely to break existing customers. (i.e. arrays are not your random class)
  • Array serialization may also be affected.
  • I am sure there are more implementation details that will make this extremely difficult: - (
  • Programs compiled to work with new methods will not work on older JVMs. Worse, depending on the implementation, programs compiled for older JVMs may not work with modified arrays on new JVMs.

I would like to have methods directly in the "Array class", I do not think that it is now possible to implement it.

+1
source share

I personally like the idea that everything is an object, and this has already been done, for example, in a small amount. However, the consequences of creating everything, even the methods / functions of an object, are very far-reaching (take a look at smalltalk to understand what I mean). Being consistent in the application of "everything is an object", an increase leads to the fact that the language no longer looks like C (or Java).

Java was very deliberately designed to be accessible to C programmers, and it succeeded, but in reality it is not very object oriented compared to smalltalk. This legacy as option C appears everywhere, as in arrays, and the fact that there are primitives.

So, to answer the question SHOULD, I would say no, because Java will no longer be Java with this change. As others have pointed out, there are other classes that allow you to handle tasks that could be performed using an array in C using Java objects, but to get rid of primitive data types and alltogether arrays, the task is better left in another language, IMHO .

+1
source share

For those who missed the previous post, which was closed. Some other examples include

 int[] ints = {5,4,3,2,1}; ints.sort(); // instead of Arrays.sort(ints); int pos = ints.indexOf(5); // instead of Arrays.asList(ints).indexOf(5); or ArraysUtils.indexOf(ints, 5); ints.reverse(); // instead of Arrays.reverse(ints); Array<Integer> array = ints; // cast to super class. int length = array.getLength(); // instead of Array.getLength(array); Object n = array.get(3); // instead of Array.get(array, 3); array.set(3, 7); // instead of Array.set(array, 3, 7); Object obj = array; System.out.println(obj); // prints [5,4,7,2,1] instead of having to // if (obj instanceof int[]) System.out.println(Array.toString((int[]) obj)); else if (....) int[] ints2 = ints.copyOf(2); int[] ints3 = ints.subArray(2,4); ints.sort(myComparator); List<Integer> list = ints.asList(); Set<Integer> set = ints.asSet(); long total = ints.sum(); double avg = int.average(); int max = ints.max(); int max2 = ints.max(myComparator); http://commons.apache.org/lang/api/org/apache/commons/lang/ArrayUtils.html int[] onemore = ints.add(8); // instead of ArrayUtils.add(ints, 8); int[] moreInts = ints.addAll(ints2); // instead of ArraysUtils.addAll(ints, ints2); int[] oneless = int.remove(3); // instead of ArrayUtils.remove(ints, 3); Integer[] integers = int.toObject(); int[] intsAgain = integers.toPrimitive(); 
0
source share

One of the reasons why I think it will not be so difficult is that you can add methods indirectly by changing the object (I really agree with the hack, but it shows that it works) By adding methods to the object, the following code

 int[] ints = {5, 4, 3, 2, 1}; System.out.println("ints= "+ints); ints.sort(); System.out.println("after sort() ints= "+ints); ints = ints.add(6); System.out.println("after add(6) ints= "+ints); 

Print

 ints= [5, 4, 3, 2, 1] after sort() ints= [1, 2, 3, 4, 5] after add(6) ints= [1, 2, 3, 4, 5, 6] 

This works with Java 5 and 6, and both the compiler and the IDE handled this as I expected.

0
source share

All Articles