[wp_ad_camp_1]
This post will show you how to copy a subset elements of an array by range. For example, copy elements from index 3 to 4 (exclusive). The range values work in the same way as the String
‘s substring
method.
Using Arrays.copyOfRange
The java.util.Arrays
has numerous helpful methods such as copyOfRange
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package com.turreta.array; import java.util.Arrays; public class CopyOfRangeArrayDemo { public static void main(String[] args) { String[] originalArray = { "I", "want", "to", "break", "free" }; String[] subSetArray = Arrays.copyOfRange(originalArray, 2, 4); System.out.println(Arrays.toString(subSetArray)); } } |
Output
From the codes, we specified 2
and 4
.
1 | [to, break] |
[wp_ad_camp_2]