Get the Subset of an Array in Java

Sheeraz Gul Jan 30, 2023 Apr 21, 2022
  1. Use the Arrays.copyOf() Method to Get the Subset of an Array
  2. Use the Arrays.copyOfRange() Method to Get the Subset of an Array
  3. Use the Stream.IntStream Method From Java 8 to Get the Subset of an Array
  4. Use the System.arraycopy() Method to Get the Subset of an Array
  5. Use Apache Commons Lang to Get the Subset of an Array
  6. Use List Conversion to Get the Subset of an Array
  7. Use Custom Method to Get the Subset of an Array
Get the Subset of an Array in Java

This tutorial demonstrates a few methods to get the subset of an array in Java.

Use the Arrays.copyOf() Method to Get the Subset of an Array

Arrays.copyOf() is a built-in method in Java Util, which copies a specified array to another array of the specified type. This method can get the subset of an array, but it won’t work if the subarray starts at an index other than the first index.

Example:

package delftstack;

import java.util.Arrays;

public class Array_Subset {
    public static void main(String[] args) {

        String[] demo_array = new String [] {"Delftstack1", "Delftstack2", "Delftstack3",
                "Delftstack4", "Delftstack5", "Delftstack6", "Delftstack7"};
        int End_Index = 5;

        String[] Subset = Arrays.copyOf(demo_array, End_Index, String[].class);
        System.out.println(Arrays.toString(Subset));
    }
}

The code above will copy a subset of the given array from the starting index to the given End_index.

Output:

[Delftstack1, Delftstack2, Delftstack3, Delftstack4, Delftstack5]

Use the Arrays.copyOfRange() Method to Get the Subset of an Array

Arrays.copyOfRange() method is used to copy a subset from an array with given beginning and ending indexes. It will copy the subset to another array.

Example:

package delftstack;

import java.util.Arrays;

public class Array_Subset {
    public static<Demo> Demo[] Array_Subset(Demo[] Array, int Begin_Index, int End_Index) {
        return Arrays.copyOfRange(Array, Begin_Index, End_Index);
    }

    public static void main(String[] args) {
        String[] Demo_Array = {"Delftstack1", "Delftstack2", "Delftstack3",
                "Delftstack4", "Delftstack5", "Delftstack6", "Delftstack7" };
        int Begin_Index = 2;
        int End_Index = 7;

        String[] Subset = Array_Subset(Demo_Array, Begin_Index, End_Index);
        System.out.println(Arrays.toString(Subset));
    }
}

Output:

[Delftstack3, Delftstack4, Delftstack5, Delftstack6, Delftstack7]

Use the Stream.IntStream Method From Java 8 to Get the Subset of an Array

We can also use Java Stream to get the subset of an array. It will get a stream of elements between the specified range and put it into an array.

Example:

package delftstack;

import java.util.Arrays;
import java.util.stream.IntStream;

public class Array_Subset {

    public static void main(String[] args) {

        String[] Demo_Arrary = { "Delftstack1", "Delftstack2", "Delftstack3",
                "Delftstack4", "Delftstack5", "Delftstack6", "Delftstack7" };
        int Begin_Index = 2;
        int End_Index = 7;

        String[] Subset = IntStream.range(Begin_Index, End_Index)
                                .mapToObj(x -> Demo_Arrary[x])
                                .toArray(String[]::new);

        System.out.println(Arrays.toString(Subset));
    }
}

Output:

[Delftstack3, Delftstack4, Delftstack5, Delftstack6, Delftstack7]

Use the System.arraycopy() Method to Get the Subset of an Array

System.arraycopy() method can copy a subset from an array with given beginning and ending indexes. It will copy the subset to another array.

Example:

package delftstack;

import java.util.Arrays;

public class Array_Subset {

    public static void main(String[] args) {
        String[] Demo_Array = new String [] { "Delftstack1", "Delftstack2", "Delftstack3",
                "Delftstack4", "Delftstack5", "Delftstack6", "Delftstack7" };
        int Begin_Index = 2;
        int End_Index = 7;

        String[] Subset = new String[End_Index - Begin_Index];
        System.arraycopy(Demo_Array, Begin_Index, Subset, 0, Subset.length);

        System.out.println(Arrays.toString(Subset));
    }
}

System.arraycopy() works similar like Arrays.copyOfRange(). It will copy a subset of an array within a specified range.

Output:

[Delftstack3, Delftstack4, Delftstack5, Delftstack6, Delftstack7]

Use Apache Commons Lang to Get the Subset of an Array

The ArrayUtils class provides the functionality to get a subset of an array using the subarray() method. This method returns an object with the specified range of elements.

Example:

package delftstack;

import org.apache.commons.lang3.ArrayUtils;
import java.util.Arrays;

public class Array_Subset {
    public static void main(String[] args)
    {
        String[] Demo_Array = new String[] { "Delftstack1", "Delftstack2", "Delftstack3",
                "Delftstack4", "Delftstack5", "Delftstack6", "Delftstack7" };
        int Begin_Index = 2;
        int End_Index = 7;

        Object[] Subset = ArrayUtils.subarray(Demo_Array, Begin_Index, End_Index);
        System.out.println(Arrays.toString(Subset));
    }
}

Output:

[Delftstack3, Delftstack4, Delftstack5, Delftstack6, Delftstack7]

Use List Conversion to Get the Subset of an Array

This method converts an array to a list and uses the subList() method to get a subset of an array within the specified range and copy it to another array.

Example:

package delftstack;

import java.util.Arrays;

public class Array_Subset {
    public static void main(String[] args)
    {
        String[] Demo_Array = new String[] { "Delftstack1", "Delftstack2", "Delftstack3",
                "Delftstack4", "Delftstack5", "Delftstack6", "Delftstack7" };
        int Begin_Index = 2;
        int End_Index = 7;

        String[] Subset = Arrays.asList(Demo_Array)
                .subList(Begin_Index, End_Index)
                .toArray(new String[0]);
        System.out.println(Arrays.toString(Subset));
    }
}

Output:

[Delftstack3, Delftstack4, Delftstack5, Delftstack6, Delftstack7]

Use Custom Method to Get the Subset of an Array

Let’s write our method to get the subset of an array. We can use the for loop to get the subset of an array within the specified range.

Example:

package delftstack;

import java.util.Arrays;

public class Array_Subset {
    public static void main(String[] args)
    {
        String[] Demo_Array = new String[] { "Delftstack1", "Delftstack2", "Delftstack3",
                "Delftstack4", "Delftstack5", "Delftstack6", "Delftstack7" };
        int Begin_Index = 2;
        int End_Index = 7;

        String[] Subset = new String[End_Index - Begin_Index];
        for (int x = 0; x < Subset.length; x++) {
            Subset[x] = Demo_Array[Begin_Index + x];
        }
        System.out.println(Arrays.toString(Subset));
    }
}

Output:

[Delftstack3, Delftstack4, Delftstack5, Delftstack6, Delftstack7]
Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - Java Array