如何在Java中重复数组的元素?
例如,给定数组{a,b,c,d,e,f}
和数字n
,我希望生成一个类似于{a,b,c,d,e,f,a,b,c,d,e,f,a,b,c,...}
的n
-element数组。
如果我事先知道输入和输出数组的长度,我可以这样写:
int a=input[0], b=input[1], c=input[2], d=input[3], e=input[4], f=input[5];
int[] array = new int[n];
array[0]=a; array[1]=b; array[2]=c; array[3]=d; array[4]=e; array[5]=f;
array[6]=a; array[7]=b; array[8]=c; array[9]=d; array[10]=e; array[11]=f;
array[12]=a; array[13]=b; array[14]=c; // .. and so on
但是如果我还不知道长度,我怎么做呢?我假设我必须使用某种类型的循环,但我不确定如何编写。或者,在Java中是否有一些内置的方法来重复数组,就像其他语言一样?
发布于 2015-08-31 10:26:19
这个实现比这里显示的其他实现更干净、更快。
public static <T> T[] repeat(T[] arr, int newLength) {
T[] dup = Arrays.copyOf(arr, newLength);
for (int last = arr.length; last != 0 && last < newLength; last <<= 1) {
System.arraycopy(dup, 0, dup, last, Math.min(last << 1, newLength) - last);
}
return dup;
}
理论
System.arraycopy
是本机调用。因此它是非常快的,但这并不意味着它是最快的方式。
其他解决方案逐个复制数组元素。我的解决方案是复制更大的块。每次迭代都会复制数组中的现有元素,这意味着循环最多会运行log2(n)次。
分析报告
下面是我用来重现结果的基准代码:
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Threads;
import org.openjdk.jmh.annotations.Warmup;
@Fork(3)
@BenchmarkMode(Mode.AverageTime)
@Measurement(iterations = 10, timeUnit = TimeUnit.NANOSECONDS)
@State(Scope.Benchmark)
@Threads(1)
@Warmup(iterations = 5, timeUnit = TimeUnit.NANOSECONDS)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public class MyBenchmark {
private static final String[] TEST_ARRAY = { "a", "b", "c", "d", "e", "f" };
private static final int NEW_LENGTH = 10_000;
@Benchmark
public String[] testMethod() {
String[] dup = Arrays.copyOf(TEST_ARRAY, NEW_LENGTH);
for (int last = TEST_ARRAY.length; last != 0 && last < NEW_LENGTH; last <<= 1) {
System.arraycopy(dup, 0, dup, last, Math.min(last << 1, NEW_LENGTH) - last);
}
return dup;
}
@Benchmark
public String[] testMethod1() {
String[] arr = new String[NEW_LENGTH];
for (int i = 0; i < NEW_LENGTH; i++) {
arr[i] = TEST_ARRAY[i % TEST_ARRAY.length];
}
return arr;
}
@Benchmark
public String[] testMethod2() {
List<String> initialLetters = Arrays.asList(TEST_ARRAY);
List<String> results = new ArrayList<>();
int indexOfLetterToAdd = 0;
for (int i = 0; i < 10000; i++) {
results.add(initialLetters.get(indexOfLetterToAdd++));
if (indexOfLetterToAdd == initialLetters.size()) {
indexOfLetterToAdd = 0;
}
}
return results.toArray(new String[results.size()]);
}
@Benchmark
public String[] testMethod3() {
String result[] = new String[NEW_LENGTH];
for (int i = 0, j = 0; i < NEW_LENGTH && j < TEST_ARRAY.length; i++, j++) {
result[i] = TEST_ARRAY[j];
if (j == TEST_ARRAY.length - 1) {
j = -1;
}
}
return result;
}
@Benchmark
public String[] testMethod4() {
String[] result = Stream.iterate(TEST_ARRAY, x -> x).flatMap(x -> Stream.of(TEST_ARRAY)).limit(NEW_LENGTH)
.toArray(String[]::new);
return result;
}
}
结果
Benchmark Mode Cnt Score Error Units
MyBenchmark.testMethod avgt 30 4154,553 ± 11,242 ns/op
MyBenchmark.testMethod1 avgt 30 19273,717 ± 235,547 ns/op
MyBenchmark.testMethod2 avgt 30 71079,139 ± 2686,136 ns/op
MyBenchmark.testMethod3 avgt 30 18307,368 ± 202,520 ns/op
MyBenchmark.testMethod4 avgt 30 68898,278 ± 2488,104 ns/op
编辑
我重新表述了这个问题,并按照建议使用了更精确的基准来回答它。Fastest way to create new array with length N and fill it by repeating a given array
发布于 2015-09-05 06:19:54
我假设您知道输入的大小(例如,您知道上面的输入中有六个元素。让我们将其命名为iSize
。或者,您可以使用arr.length
找到它,其中arr
是输入数组。)
对于上述问题,这可能是一种更干净的解决方案。
for(int i=iSize; i<10000; i++)
arr[i] = arr[i%iSize];
发布于 2015-08-31 11:35:03
你可以这样试一下
String arr[] = {"a", "b", "c", "d", "e", "f"};
int repeat = 10000;
String result[] = new String[repeat];
for(int i=0, j=0; i<repeat && j<arr.length; i++, j++)
{
result[i] = arr[j];
if(j == arr.length -1)
j = -1;
System.out.println("array["+i+"] : "+result[i]);
}
https://stackoverflow.com/questions/32305652
复制