Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将特定索引处的数组值复制到另一个数组_Java_Arrays - Fatal编程技术网

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

Java 将特定索引处的数组值复制到另一个数组,java,arrays,Java,Arrays,我遇到了这样一种情况:我有一个数组,我需要将一些特定属性(即特定索引处的值)而不是整个数组复制到另一个数组 例如,如果初始数组为: double[] initArray = {1.0, 2.0, 1.5, 5.0, 4.5}; 然后,如果我只想复制第二、第四和第五个属性(即这些索引处的值),则所需的输出数组将是: double[] reducedArray = {2.0, 5.0, 4.5}; 我知道,如果索引以顺序形式出现,例如1-3,则我可以使用,但我的索引没有该方面 那么,除了通过每个

我遇到了这样一种情况:我有一个数组,我需要将一些特定属性(即特定索引处的值)而不是整个数组复制到另一个数组

例如,如果初始数组为:

double[] initArray = {1.0, 2.0, 1.5, 5.0, 4.5};
然后,如果我只想复制第二、第四和第五个属性(即这些索引处的值),则所需的输出数组将是:

double[] reducedArray = {2.0, 5.0, 4.5};
我知道,如果索引以顺序形式出现,例如1-3,则我可以使用,但我的索引没有该方面

那么,除了通过每个值进行简单的循环并复制所需的值之外,还有什么官方方法可以做到这一点:

double[] includedAttributes = {1, 4, 5};
double[] reducedArray = new double[includedAttributes.length];
for(int j = 0; j < includedAttributes.length; j++) {
    reducedArray[j] = initArray[includedAttributes[j]];
}
double[]includedAttributes={1,4,5};
double[]reducedArray=新的double[includedAttributes.length];
对于(int j=0;j
简单地说,这是不可能的,除非你有具体的案例

例如:

您需要具有最高值的前N项(在您的示例中为{2.0,4.5,5.0})

一种快速(肮脏)的方法:

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;
}

注意:此方法还可以对原始数组进行排序。如果您不希望出现这种行为,可以使用不同的方法,下面是它们的列表:

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

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;
    }

}
公共类指针阵列{
私人T[]arr;
私人指数;
公共点阵列(T[]arr,int[]index){
this.arr=arr;
该指数=指数;
}
公共T获取(整数索引){
返回this.arr[this.index[index]];
}
公共无效集(整数索引,T值){
this.arr[this.index[index]]=值;
}
公共整数大小(){
返回此.index.length;
}
}
这是未经测试的代码,但这个想法至少应该得到通过

使用它看起来像这样:

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));
}
int[]includedAttributes={0,3,4};
点阵列还原阵列=
新的PointerArray(initArray,includedAttributes);
对于(int j=0;j

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

使用流,它是一个单行程序

鉴于:

int[] indices;
double[] source;
然后:


例如,使用ArrayList有什么限制吗?这样,您就可以拥有一个
arraylisttemp
,然后只需将所有需要的数字添加到那里。之后,您可以通过调用
temp.toArray(newdouble[]{})
获得
double[]
数组。当然,您也可以使用纯数组来实现这一点。这只是一个肮脏、简单和简单的方法,Java 8 Streams可能就是您要寻找的。或者是8之前的版本?@olavimutanoja虽然没有任何具体的限制,但由于自动装箱,我会选择
ArrayList
。@Fildor我使用的是Java 7,但无论如何,了解这一点可能会很有趣。然后,也许可以看看这里:
double[] result = Arrays.stream(indices).mapToDouble(i -> source[i]).toArray();