gpt4 book ai didi

java - 数组作为适当的对象

转载 作者:行者123 更新时间:2023-12-04 07:09:45 24 4
gpt4 key购买 nike

很难说出这里问的是什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或言辞激烈,无法以目前的形式合理回答。如需帮助澄清此问题以便可以重新打开,visit the help center .




12 年前关闭。




我在这里写了一篇关于使用数组作为具有自己的方法的正确对象而不是依赖于诸如 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 (....)

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.

需要将诸如 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 需要接受数组具有不同的父类(super class)。
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.

数组的父类(super class)需要返回 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.

更改应反射(reflect)在 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() 的调用将会改变。这可能是 Not Acceptable ,在这种情况下,这些方法可以保持原样,而不是调用 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.

这就是我正在寻找的帮助、错误报告或其他引用资料

最佳答案

我建议您查看各种集合类。

关于java - 数组作为适当的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/558839/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com