有很多次我认为使用clone()并不是一种糟糕的做法.是的,我知道这些论点.布洛赫说这很糟糕.他的确做到了,但他说实施clone()很糟糕.另一方面,使用clone,特别是如果它由可信库(如JDK)正确实现,则可以.
就在昨天,我讨论了我的一个答案,仅仅暗示使用clone()for ArrayList是可以的(并且由于这个原因没有投票,我猜).
如果我们看@author的ArrayList,我们可以看到一个熟悉的名字-乔希布洛赫.因此,clone()在ArrayList(和其他收藏品)是完全正常的(看看他们的实现).
同样的Calendar,也许是大多数java.lang和java.util类.
那么,请告诉我为什么不使用 clone() JDK类?
这对我来说是一种耻辱,但我不知道:
您应该使用clone来复制数组,因为这通常是最快的方法.
正如Josh Bloch在本博客中所述:http://www.artima.com/intv/bloch13.html
我总是用System.arraycopy(...).这两种方法都是原生的,所以可能没有深入到我无法弄清楚的库的来源,为什么会如此.
我的问题很简单:为什么它是最快的方式?
有什么区别这里
解释了不同之处,但它没有回答为什么Josh Bloch认为System.arraycopy?clone()最快的方式.
这是我的基准代码:
def bm(duration: Long)(f: => Unit)={
val end = System.currentTimeMillis + duration
var count = 0
while(System.currentTimeMillis < end) { f; count += 1 }
count
}
val array = new scala.util.Random().alphanumeric.take(1000).toArray
(1 to 20).map { _ => bm(1000) { array.slice(100,200) } }.sum / 20
Run Code Online (Sandbox Code Playgroud)
运行这几次,我总是得到每秒大约150万片的数字.介于1.4和1.6之间.
现在,我这样做:
implicit class FastSlicing(val a: Array[Char]) extends AnyVal {
def fastSlice(from: Int, until: Int) = Arrays.copyOfRange(a, from, until)
}
(1 to 20).map { _ => bm(1000) { array.fastSlice(100,200) } }.sum / 20
Run Code Online (Sandbox Code Playgroud)
我得到的结果是每秒1600到1800万个切片.这速度提高了10倍以上 …
我不知道不可变类应该是什么样子但是我很确定这个是.我对吗?如果我不是请指定应添加/删除的内容.
import java.io.Serializable;
public class Triangle implements IShape, Serializable {
private static final long serialVersionUID = 0x100;
private Point[] points;
public Triangle(Point a, Point b, Point c) {
this.points = new Point[]{a, b, c};
}
@Override
public Point[] getPoints() {
return this.points;
}
@Override
public boolean equals(Object obj) {
if (obj == null) return false;
if (this == obj) return true;
if (getClass() != obj.getClass()) return false;
Point[] trianglePoints = ((Triangle) obj).getPoints();
for (int i = 0; i < points.length; …Run Code Online (Sandbox Code Playgroud) 我最近玩了一些基准测试,发现了非常有趣的结果,我现在无法解释.这是基准:
@BenchmarkMode(Mode.Throughput)
@Fork(1)
@State(Scope.Thread)
@Warmup(iterations = 10, time = 1, batchSize = 1000)
@Measurement(iterations = 10, time = 1, batchSize = 1000)
public class ArrayCopy {
@Param({"1","5","10","100", "1000"})
private int size;
private int[] ar;
@Setup
public void setup() {
ar = new int[size];
for (int i = 0; i < size; i++) {
ar[i] = i;
}
}
@Benchmark
public int[] SystemArrayCopy() {
final int length = size;
int[] result = new int[length];
System.arraycopy(ar, 0, result, 0, length);
return result; …Run Code Online (Sandbox Code Playgroud) String.toCharArray()java中的运行时间是多少?源代码是
public char[] toCharArray() {
// Cannot use Arrays.copyOf because of class initialization order issues
char result[] = new char[value.length];
System.arraycopy(value, 0, result, 0, value.length);
return result;
}
Run Code Online (Sandbox Code Playgroud)
难道System.arrayCopy?有 O(n) 的运行时间吗?源代码并没有真正说明它是如何实现的。它是否遍历每个元素并复制它?谢谢。
我是android的新手.我想在函数中调整字节数组的大小.有可能吗?如有任何问题,请提出解决方案.
public void myfunction(){
byte[] bytes = new byte[1024];
....................
.... do some operations........
................................
byte[] bytes = new byte[2024];
}
Run Code Online (Sandbox Code Playgroud) 我注意到在Java中,当您将数组传递给函数时,它会修改原始数组.我试图实现使用递归的回溯方法,我希望每次调用它都有自己的数组复制传入的数组的内容.
例如,假设我有一个原始数组,并且我通过一个调用该函数的循环.我希望每个调用都有一个包含原始数组中所有内容的数组,但它修改的任何内容都保留在其自身内,而不是修改原始数组.这可能吗?
如果有解决方案,那么arraylists也可以吗?