0 votes
1 view
in Java by (3.4k points)

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

    }

}

1 Answer

0 votes
by (32.7k points)


You can Try:

In JDK > 1.5

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

Javadoc

In JDK <= 1.5

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

Javadoc

...