将特定索引处的数组值复制到另一个数组

时间:2022-10-23 12:48:26

I came up to a situation where I have an array and I need to copy some specific attributes (i.e. values at specific indinces) not the whole array to another array.

我遇到了一种情况,我有一个数组,我需要将一些特定属性(即特定indinces的值)复制到另一个数组。

For example if the initial array is:

例如,如果初始数组是:

double[] initArray = {1.0, 2.0, 1.5, 5.0, 4.5};

then if I wanted to copy only 2nd, 4th and 5th attribute (i.e. values at these indices) the desired output array would be:

那么如果我只想复制第2,第4和第5个属性(即这些索引处的值),那么所需的输出数组将是:

double[] reducedArray = {2.0, 5.0, 4.5};

I know that if the indices appear in a sequential form, e.g. 1-3 then I can use System.arraycopy() but my indices does not have that aspect.

我知道如果指数以顺序形式出现,例如1-3然后我可以使用System.arraycopy()但我的索引没有那个方面。

So, is there any official way to do this, besides the trivial loop through each value and copy the ones needed:

那么,有没有任何官方方法可以做到这一点,除了通过每个值的琐碎循环并复制所需的:

double[] includedAttributes = {1, 4, 5};
double[] reducedArray = new double[includedAttributes.length];
for(int j = 0; j < includedAttributes.length; j++) {
    reducedArray[j] = initArray[includedAttributes[j]];
}

3 个解决方案

#1


2  

Using streams, it's a one-liner.

使用流,它是一个单行。

Given:

鉴于:

int[] indices;
double[] source;

Then:

然后:

double[] result = Arrays.stream(indices).mapToDouble(i -> source[i]).toArray();

#2


0  

Simply said, its not possible, unless you have a specific case.

简单地说,除非你有特定的案例,否则它是不可能的。

for example:

例如:

You want the top N items with highest value (in your case {2.0,4.5,5.0})

您希望前N个项目具有最高值(在您的情况下为{2.0,4.5,5.0})

A quick(and dirty) way of doing it:

这样做的快速(和肮脏)方式:

public static double[] topvalues(int n,double[] original){
 double[] output=new double[n];
 Arrays.sort(original);
 System.arraycopy(original,0,output,0,n);
 return output;
}

NOTE: this method also sorts your original array as well. if you don't want this behaviour there are different methods to use, here is a list of them :

注意:此方法也会对原始数组进行排序。如果你不想要这种行为,可以使用不同的方法,这里有一个列表:

#3


0  

Answering your question in a way perhaps not sought-after, you could write a class for this kind of operation:

以某种可能不受欢迎的方式回答你的问题,你可以为这种操作写一个类:

public class PointerArray <T> {

    private T[] arr;
    private int[] indices;

    public PointerArray(T[] arr, int[] indices) {
        this.arr = arr;
        this.indices = indices;
    }

    public T get(int index) {
        return this.arr[this.indices[index]];
    }

    public void set(int index, T value) {
        this.arr[this.indices[index]] = value;
    }

    public int size() {
        return this.indices.length;
    }

}

This is untested code, but the idea in the very least should get through.

这是未经测试的代码,但这个想法至少应该通过。

Using it would look something like this:

使用它看起来像这样:

int[] includedAttributes = {0, 3, 4};

PointerArray<Double> reducedArray =
    new PointerArray<Double>(initArray, includedAttributes);

for(int j = 0; j < reducedArray.size(); j++) {
    System.out.println(reducedArray.get(j));
}

This is performance- and memory-wise, I think, a good solution, since nothing is copied (nor created). Only drawback is the need to call get(), but I have no idea how expensive method calls really are.

我认为这是性能和内存方面的一个很好的解决方案,因为没有任何东西被复制(也没有被创建)。唯一的缺点是需要调用get(),但我不知道方法调用的确是多么昂贵。

#1


2  

Using streams, it's a one-liner.

使用流,它是一个单行。

Given:

鉴于:

int[] indices;
double[] source;

Then:

然后:

double[] result = Arrays.stream(indices).mapToDouble(i -> source[i]).toArray();

#2


0  

Simply said, its not possible, unless you have a specific case.

简单地说,除非你有特定的案例,否则它是不可能的。

for example:

例如:

You want the top N items with highest value (in your case {2.0,4.5,5.0})

您希望前N个项目具有最高值(在您的情况下为{2.0,4.5,5.0})

A quick(and dirty) way of doing it:

这样做的快速(和肮脏)方式:

public static double[] topvalues(int n,double[] original){
 double[] output=new double[n];
 Arrays.sort(original);
 System.arraycopy(original,0,output,0,n);
 return output;
}

NOTE: this method also sorts your original array as well. if you don't want this behaviour there are different methods to use, here is a list of them :

注意:此方法也会对原始数组进行排序。如果你不想要这种行为,可以使用不同的方法,这里有一个列表:

#3


0  

Answering your question in a way perhaps not sought-after, you could write a class for this kind of operation:

以某种可能不受欢迎的方式回答你的问题,你可以为这种操作写一个类:

public class PointerArray <T> {

    private T[] arr;
    private int[] indices;

    public PointerArray(T[] arr, int[] indices) {
        this.arr = arr;
        this.indices = indices;
    }

    public T get(int index) {
        return this.arr[this.indices[index]];
    }

    public void set(int index, T value) {
        this.arr[this.indices[index]] = value;
    }

    public int size() {
        return this.indices.length;
    }

}

This is untested code, but the idea in the very least should get through.

这是未经测试的代码,但这个想法至少应该通过。

Using it would look something like this:

使用它看起来像这样:

int[] includedAttributes = {0, 3, 4};

PointerArray<Double> reducedArray =
    new PointerArray<Double>(initArray, includedAttributes);

for(int j = 0; j < reducedArray.size(); j++) {
    System.out.println(reducedArray.get(j));
}

This is performance- and memory-wise, I think, a good solution, since nothing is copied (nor created). Only drawback is the need to call get(), but I have no idea how expensive method calls really are.

我认为这是性能和内存方面的一个很好的解决方案,因为没有任何东西被复制(也没有被创建)。唯一的缺点是需要调用get(),但我不知道方法调用的确是多么昂贵。