Java copy section of array [duplicate]
Asked Answered
M

6

21

Is there a method that will copy a section of an array(not arraylist) and make a new array from it?

Example:
[1,2,3,4,5]

and you create a new array from it:

[1,2,3]

Are there any one line/methods that will do this?

Marabelle answered 2/5, 2012 at 23:30 Comment(0)
M
24

Here's a java 1.4 compatible 1.5-liner:

int[] array = { 1, 2, 3, 4, 5 };
int size = 3;

int[] part = new int[size];
System.arraycopy(array, 0, part, 0, size);

You could do this in one line, but you wouldn't have a reference to the result.

To make a one-liner, you could refactor this into a method:

private static int[] partArray(int[] array, int size) {
    int[] part = new int[size];
    System.arraycopy(array, 0, part, 0, size);
    return part;
}

then call like this:

int[] part = partArray(array, 3);
Marimaria answered 2/5, 2012 at 23:38 Comment(0)
A
26

See the method Arrays.copyOfRange

Adiana answered 2/5, 2012 at 23:31 Comment(0)
M
24

Here's a java 1.4 compatible 1.5-liner:

int[] array = { 1, 2, 3, 4, 5 };
int size = 3;

int[] part = new int[size];
System.arraycopy(array, 0, part, 0, size);

You could do this in one line, but you wouldn't have a reference to the result.

To make a one-liner, you could refactor this into a method:

private static int[] partArray(int[] array, int size) {
    int[] part = new int[size];
    System.arraycopy(array, 0, part, 0, size);
    return part;
}

then call like this:

int[] part = partArray(array, 3);
Marimaria answered 2/5, 2012 at 23:38 Comment(0)
S
9

There is a pre-existing method in the java.util.Arrays: newArray = Arrays.copyOfRange(myArray, startindex, endindex). Or you could easily write your own method:

public static array[] copyOfRange(array[] myarray, int from, int to) {
    array[] newarray = new array[to - from];
    for (int i = 0 ; i < to - from ; i++) newarray[i] = myarray[i + from];
    return newarray;
}
Slip answered 2/5, 2012 at 23:37 Comment(0)
R
6
int [] myArray = [1,2,3,4,5];

int [] holder = new int[size];

System.arraycopy(myArray,0,holder,size);

where 0 stands for the index of source array from where copying should start. and

size stands for the number of copy operations. That you can changes according to your need.

copyOfRange of Arrays is there and many other ways by which this can be accomplished

Roscoeroscommon answered 2/5, 2012 at 23:36 Comment(1)
You're missing one argument for it requires another int for destinationPosition.Sham
C
5

Arrays#copyOfRange does the trick.

Cramped answered 2/5, 2012 at 23:32 Comment(0)
K
4

As others have stated, you can use Arrays.copyOfRange method. An example is :

String[] main = {"one", "two", "three", "four", "five"};
int from = 2;
int to = 4;
String[] part = Arrays.copyOfRange(main, from, to);

Now part will be : {"two", "three", "four"}

Konstance answered 18/10, 2017 at 20:57 Comment(1)
There is an error, part is {"three", "four", "five"}, because arrays index starts at 0Illiberal

© 2022 - 2024 — McMap. All rights reserved.