如何从Java中的另一个数组创建子数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4439595/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 17:30:51  来源:igfitidea点击:

How to create a sub array from another array in Java?

javaarrays

提问by Gain

How to create a sub-array from another array? Is there a method that takes the indexes from the first array such as:

如何从另一个数组创建子数组?是否有一种方法可以从第一个数组中获取索引,例如:

methodName(object array, int start, int end)

I don't want to go over making loops and making my program suffer.

我不想重复制作循环并使我的程序受到影响。

I keep getting error:

我不断收到错误:

cannot find symbol method copyOfRange(int[],int,int)

找不到符号方法 copyOfRange(int[],int,int)

This is my code:

这是我的代码:

import java.util.*;

public class testing 
{
    public static void main(String [] arg) 
    {   
        int[] src = new int[] {1, 2, 3, 4, 5}; 
        int b1[] = Arrays.copyOfRange(src, 0, 2);
    }
}

回答by Jigar Joshi

You can use

您可以使用

JDK > 1.5

JDK > 1.5

Arrays.copyOfRange(Object[] src, int from, int to)

Javadoc

Javadoc

JDK <= 1.5

JDK <= 1.5

System.arraycopy(Object[] src, int srcStartIndex, Object[] dest, int dstStartIndex, int lengthOfCopiedIndices); 

Javadoc

Javadoc

回答by Gerco Dries

Yes, it's called System.arraycopy(Object, int, Object, int, int).

是的,它被称为System.arraycopy(Object, int, Object, int, int)

It's still going to perform a loop somewhere though, unless this can get optimized into something like REP STOSWby the JIT (in which case the loop is inside the CPU).

尽管如此,它仍然会在某处执行一个循环,除非它可以REP STOSW通过 JIT优化成类似的东西(在这种情况下,循环在 CPU 内部)。

int[] src = new int[] {1, 2, 3, 4, 5};
int[] dst = new int[3];

System.arraycopy(src, 1, dst, 0, 3); // Copies 2, 3, 4 into dst

回答by Bozho

Arrays.copyOfRange(..)was added in Java 1.6. So perhaps you don't have the latest version. If it's not possible to upgrade, look at System.arraycopy(..)

Arrays.copyOfRange(..)在 Java 1.6 中添加。所以也许你没有最新版本。如果无法升级,请查看System.arraycopy(..)

回答by AlexR

I you are using java prior to version 1.6 use System.arraycopy()instead. Or upgrade your environment.

我使用 1.6 版之前的 javaSystem.arraycopy()代替。或者升级你的环境。

回答by Milan Jaros

int newArrayLength = 30; 

int[] newArray = new int[newArrayLength];

System.arrayCopy(oldArray, 0, newArray, 0, newArray.length);

回答by Merky

The code is correct so I'm guessing that you are using an older JDK. The javadoc for that method says it has been there since 1.6. At the command line type:

代码是正确的,所以我猜你使用的是旧版 JDK。该方法的 javadoc 说它从 1.6 开始就存在了。在命令行输入:

java -version

I'm guessing that you are not running 1.6

我猜你没有运行 1.6

回答by Alessandro Muzzi

Using Apache ArrayUtilsdownloadable at this linkyou can easy use the method

使用可在此链接下载的Apache ArrayUtils,您可以轻松使用该方法

subarray(boolean[] array, int startIndexInclusive, int endIndexExclusive) 

"boolean" is only an example, there are methods for all primitives java types

“boolean”只是一个例子,所有原语java类型都有方法

回答by Dhirendra Pratap

Use copyOfRange method from java.util.Arrays class:

使用 java.util.Arrays 类中的 copyOfRange 方法:

int[] newArray = Arrays.copyOfRange(oldArray, startIndex, endIndex);

int[] newArray = Arrays.copyOfRange(oldArray, startIndex, endIndex);

For more details:

更多细节:

Link to similar question

类似问题的链接

回答by enator

JDK >= 1.8

JDK >= 1.8

I agree with all the answers above. There is also a nice way with Java 8 Streams:

我同意上面的所有答案。Java 8 Streams 还有一个很好的方法:

int[] subArr = IntStream.range(startInclusive, endExclusive)
                        .map(i -> src[i])
                        .toArray();

The benefit about this is, it can be useful for many different types of "src" array and helps to improve writing pipeline operations on the stream.

这样做的好处是,它可以用于许多不同类型的“src”数组,并有助于改进流上的写入管道操作。

Not particular about this question, but for example, if the source array was double[]and we wanted to take average()of the sub-array:

不是特别关注这个问题,但例如,如果源数组是double[]并且我们想要取average()子数组:

double avg = IntStream.range(startInclusive, endExclusive)
                    .mapToDouble(index -> src[index])
                    .average()
                    .getAsDouble();