Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
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 (46k 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

Related questions

0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...