我在这里写了一个页面,介绍使用数组作为具有自己的方法的适当对象,而不是依赖像 Arrays、Arrays 和 ArrayUtils 这样的辅助类。
ints.sort(); // instead of Arrays.sort(ints);
// instead of int[] onemore = ArrayUtils.add(ints, 8);
int[] onemore = ints.add(8);
我确信我不是第一个有这个想法的人,但我很难找到以前写过这个想法的其他人。
任何人都可以帮助我提供有关此主题的一些参考资料吗?
如果您有关于为什么这是一个坏主意或一个好主意的参考,您可以添加评论吗?
链接已删除。添加要点
这源于 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.
将数组视为具有自己方法的对象,而不是传递给辅助方法的值。这导致更自然的编码并使方法更加直接。例如通过代码完成。
MAJOR ADVANTAGE: What makes the proposal a favorable change?
它将面向对象编程引入数组,支持已经可用和编写的方法。
MAJOR BENEFIT: Why is the platform better if the proposal is adopted?
数组的面向对象一致性。
MAJOR DISADVANTAGE: There is always a cost.
必须有人编写并测试它。
ALTERNATIVES: Can the benefits and advantages be had some way without a language change?
调用辅助方法。
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 (....)
高级示例:显示功能的高级用法。
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.
需要将诸如 java.lang.Array 之类的类添加为所有数组的父级。可能还需要特定 int[]、boolean[] 的子类。语法不应该有很大的不同。
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!
在提供了可以使用数组的新父级时,编译将与现在相同。但是,JVM 需要接受数组具有不同的超类。
TESTING: How can the feature be tested?
检查新方法与辅助方法的作用相同。(如果他们确实只是调用相同的辅助方法,应该很简单)
LIBRARY SUPPORT: Are any supporting libraries needed for the feature?
这应该添加到 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.
数组的超类需要返回 java.lang.Array 等而不是 java.lang.Object。但是,这可能是 JVM 而不是 rt.jar 代码的更改。
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.
更改应反映在 javadoc 中。
MIGRATION: Sketch how a code base could be converted, manually or automatically, to use the new feature.
将对 Arrays.xxx(array, args) 的调用替换为 array.xxx(args);
COMPATIBILITY
BREAKING CHANGES: Are any previously valid programs now invalid? If so, list one.
如果采用每种方法,对 hashCode() 和 equals() 的调用将被更改。这可能是不可接受的,在这种情况下,这些方法可以保持原样,而不是调用 Arrays.hashCode() 或 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?
不。
REFERENCES
EXISTING BUGS: Please include a list of any existing Sun bug ids related to this proposal.
这就是我正在寻求帮助、错误报告或其他参考的内容