且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何创建从Java中另一个阵列子阵?

更新时间:2023-11-27 12:05:10

在Java 1.6的加入 Arrays.copyOfRange(..)。因此,也许你没有最新的版本。如果这是不可能的升级,看看 System.arraycopy(..)

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)

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

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(..)