Top community members
All Wiki Articles Create Wiki Article

Welcome to Dirask IT community! ❤ 💻
We are community of people that helps each other.

If you think you have some skills to help others

help someone and become a part of our community - List Q & A

Java - create subarray of ints from another array

0 contributions
11 points

In java there is couple of ways how to create subarray, the simples way I know with simple code example is presented here.

1. Java subarray with Arrays.copyOfRange

Syntax:

int[] copyOfRange(int[] original, int from, int to)

Code example:

int[] arr = {10, 20, 30, 40, 50, 60};
int[] subArr1 = Arrays.copyOfRange(arr, 0, 2);
int[] subArr2 = Arrays.copyOfRange(arr, 2, 6);

System.out.println(Arrays.toString(arr));     // [10, 20, 30, 40, 50, 60]
System.out.println(Arrays.toString(subArr1)); // [10, 20]
System.out.println(Arrays.toString(subArr2)); // [30, 40, 50, 60]

Output:

[10, 20, 30, 40, 50, 60]
[10, 20]
[30, 40, 50, 60]

Reference:

  1. Arrays.copyOfRange - java docs
0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more