如何从Java中的另一个数组创建子数组? [英] How to create a sub array from another array in Java?
本文介绍了如何从Java中的另一个数组创建子数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从另一个数组创建子数组?是否有一种方法可以从第一个数组中获取索引,例如:
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.
我不断收到错误:
找不到符号方法 copyOfRange(int[],int,int)
cannot find symbol method copyOfRange(int[],int,int)
这是我的代码:
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);
}
}
推荐答案
您可以使用
Arrays.copyOfRange(Object[] src, int from, int to)
System.arraycopy(Object[] src, int srcStartIndex, Object[] dest, int dstStartIndex, int lengthOfCopiedIndices);
这篇关于如何从Java中的另一个数组创建子数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文