2

I have written a page here on using arrays as proper objects with their own methods instead of relying on helper classes like Arrays, Arrays and ArrayUtils.

ints.sort(); // instead of Arrays.sort(ints);
// instead of int[] onemore = ArrayUtils.add(ints, 8);
int[] onemore = ints.add(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?

Can you add comments if you have a reference on why this is a bad idea or a good idea if you have one?

Link deleted. Adding main points

This follows from the idea of Project Coin

OVERVIEW
Provide a two sentence or shorter description of these five aspects of the feature:
FEATURE SUMMARY: Should be suitable as a summary in a language tutorial.

Treat array as objects with their own methods rather than values to be passed to helper methods. This leads to more natural coding and gives the methods more immediacy. e.g. through code completion.

MAJOR ADVANTAGE: What makes the proposal a favorable change?

It bring OO programming to arrays , supporting methods already available and written.

MAJOR BENEFIT: Why is the platform better if the proposal is adopted?

Object Orientated consistency for arrays.

MAJOR DISADVANTAGE: There is always a cost.

Someone has to write it and test it.

ALTERNATIVES: Can the benefits and advantages be had some way without a language change?

Call helper methods.

EXAMPLES
Show us the code!
SIMPLE EXAMPLE: Show the simplest possible program utilizing the new feature.

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 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.
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 (....)

ADVANCED EXAMPLE: Show advanced usage(s) of the feature.

int[] ints = {5,4,3,2,1};
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();

DETAILS
SPECIFICATION: Describe how the proposal affects the grammar, type system, and meaning of expressions and statements in the Java Programming Language as well as any other known impacts.

A class such as java.lang.Array would need to be added as the parent of all arrays. Subclasses for specific int[], boolean[] might also be needed. The grammar shouldn't be dramatically different.

COMPILATION: How would the feature be compiled to class files? Show how the simple and advanced examples would be compiled. Compilation can be expressed as at least one of a desugaring to existing source constructs and a translation down to bytecode. If a new bytecode is used or the semantics of an existing bytecode are changed, describe those changes, including how they impact verification. Also discuss any new class file attributes that are introduced. Note that there are many downstream tools that consume class files and that they may to be updated to support the proposal!

In the provides a new parent for arrays could be used, the compilation would be the same as it is now. However, it is the JVM which would need to accept that an array has a different super class.

TESTING: How can the feature be tested?

Check the new methods do the same things as the helper methods. (Should be simple if indeed they just call the same helper methods)

LIBRARY SUPPORT: Are any supporting libraries needed for the feature?

This should be added to the rt.jar

REFLECTIVE APIS: Do any of the various and sundry reflection APIs need to be updated? This list of reflective APIs includes but is not limited to core reflection (java.lang.Class and java.lang.reflect.*), javax.lang.model.*, the doclet API, and JPDA.

The super class for an array would need to return java.lang.Array or the like instead of java.lang.Object. However, again this may be a change for the JVM rather than the rt.jar code.

OTHER CHANGES: Do any other parts of the platform need be updated too? Possibilities include but are not limited to JNI, serialization, and output of the javadoc tool.

The change should be reflected in the javadoc.

MIGRATION: Sketch how a code base could be converted, manually or automatically, to use the new feature.

Replace calls to Arrays.xxx(array, args) to array.xxx(args);

COMPATIBILITY
BREAKING CHANGES: Are any previously valid programs now invalid? If so, list one.

Calls to hashCode() and equals() would be changed if every method were taken. This may be unacceptable in which case these methods could be left as they are rather than call Arrays.hashCode() or Arrays.equals();

EXISTING PROGRAMS: How do source and class files of earlier platform versions interact with the feature? Can any new overloadings occur? Can any new overriding occur?

No.

REFERENCES
EXISTING BUGS: Please include a list of any existing Sun bug ids related to this proposal.

This is what I am looking for help on, bug reports or other references

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • What exactly is your question? – Jérôme Feb 17 '09 at 21:41
  • I think a good question could come from this though – OscarRyz Feb 17 '09 at 21:44
  • The question is; Can anyone help me with some references on this topic? – Peter Lawrey Feb 17 '09 at 21:54
  • The ? at the end is the hint. :-P – Peter Lawrey Feb 17 '09 at 21:55
  • @Peter: You have the option to edit your question to make your point clear. The only thing in the original that was clear was promoting your blog. – GEOCHET Feb 17 '09 at 21:56
  • You will need to work more on your suggestion to get some references. I don't believe that cost of such change would justify benefits. And it would be HUGE cost. JVM would need to be modified in non-compatible ways. New programs would not run on old JVMs, and vice-versa. – Peter Štibraný Feb 17 '09 at 21:56
  • Its not a blog, it a web page. I provided it as an example (I am looking for more exmaples) but if you would prefer I could copy and paste the whole thing here. – Peter Lawrey Feb 17 '09 at 21:57
  • One specific example of change: when new array is craeated, Object. is not called. When you add new class into hierarchy, would you call its constructor or not? Would you then call Object. too? This can break many assumptions, e.g. when doing instrumentation of Object's constructor. – Peter Štibraný Feb 17 '09 at 21:58
  • Any language change would have to come as part of a new release of Java such as Java 7. This is the point of Project Coin. They ask for references for related material. There must be some, I just cannot find it. – Peter Lawrey Feb 17 '09 at 21:59
  • Other change is that list of methods on array is now fixed. You would change that. Again... this may break many programs doing stuff with reflection. – Peter Štibraný Feb 17 '09 at 22:00
  • Closing this as spam is crazy. It should have simply been edited to rephrase the question. Ie, its a pretty basic question as to whether this has been proposed (it has) and whether its a good idea (its not). Its times like these that I wish I had more rep. – jsight Feb 17 '09 at 22:00
  • native methods can create objects. e.g. Unsafe.allocateInstance() I would image a new int[] would produce the same bytecode it does now. I don't see the need for a constructor to be called. e.g. You cannot call new Class() even via reflections, but this is not a problem. – Peter Lawrey Feb 17 '09 at 22:02
  • Adding any method anywhere can break a program which uses reflections incorrectly. Currently all arrays have the same methods as Object even though most of them are not useful on an array. – Peter Lawrey Feb 17 '09 at 22:04
  • @jsight this is the sort of discussion (I am sure it out there somewhere) which I am looking for and might save everyone some time. – Peter Lawrey Feb 17 '09 at 22:05
  • @jsight can you point me to something which explains why its not a good idea. I haven't found the right keywords to search for if you can just give me those... – Peter Lawrey Feb 17 '09 at 22:08
  • @Rich B, I cannot re-open the question alone. You have removed the link and hopefully the comments clear up any confusion. – Peter Lawrey Feb 17 '09 at 22:13
  • Just because the reason for closure is no longer valid does not mean to me it should be reopened. – GEOCHET Feb 17 '09 at 23:38
  • @Rich B: Yes, actually the fact that the reason for closure is invalid does mean that it should be reopened!!!!!! I'm amazed that people could see this so poorly. – jsight Feb 18 '09 at 03:37
  • If you want to get this reopened then I would suggest getting rid of 75% of it then posting a clear *concise* question. – EBGreen Feb 18 '09 at 03:44
  • @EBGreen - Conciseness isn't a rule. The earlier version was a simple question (why is this a bad idea and have other people thought of it), but some arrogant folks closed it as spam. What other options are there? – jsight Feb 18 '09 at 03:47
  • The other option is the one that I gave you. Take a question and ask it. Don't post a wall of text and ask for references to support it. – EBGreen Feb 18 '09 at 03:49
  • See http://stackoverflow.com/questions/559779/should-java-treat-arrays-as-objects for how I would suggest asking questions in future. It's hard enough reading some of them without having to wade through pages of waffle or web links, no offence intended. – paxdiablo Feb 18 '09 at 03:59
  • @Pax - Of course, you could have just edited this question. It is a good question, so why was this ever closed when the only issue was a little editing? And the original was very easy to read, but it was closed b/c it had 1 link that someone didn't like. – jsight Feb 18 '09 at 04:54
  • @EBgreen, I did ask a short an concise question with a link for further explanation, however the link was disallowed and I asked, should in cut-and-paste the relevant pieces into the question. You say that was too much. I agree, that is why I used a link in the first place! – Peter Lawrey Feb 18 '09 at 06:51
  • @Pax, thank you for reposting the question. At least that way I might get discussion of substance than about form. – Peter Lawrey Feb 18 '09 at 06:53
  • @jsight Thank you for your support of the closure. Not sure a lot of thought went into it. – Peter Lawrey Feb 18 '09 at 06:55
  • 1
    I am amazed at the audacity of people to write horrible questions and then /expect/ people to edit for them for free. I like to help make bad questions useful, but certainly not when the author cannot be bothered to help themselves and expects everyone to help them. – GEOCHET Feb 18 '09 at 14:42
  • @Rich B Your intervention was neither expected, desired nor very useful as far as I can see. In the end, the question was repeated so it could be answered as was originally intended. I understand your frustration but I think it was miss applied here. – Peter Lawrey Feb 18 '09 at 20:50

1 Answers1

3

I suggest you look into the various collections classes instead.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • doesn't work for primitives, though; it's been some time since I've done extensive Java programming, but afaik there's no equivalent of `ArrayList` for `int`, `long`, ... (and no, boxing is no viable alternative!) – Christoph Feb 17 '09 at 21:46
  • It doesn't work for Object[] of any type either. – Peter Lawrey Feb 17 '09 at 21:56
  • There are the GNU Trove collections, but these are not standard. (Even in the naming of their methods) – Peter Lawrey Feb 17 '09 at 22:10