12

How can I create/instantiate an array to be equal to the substring of another array, where the size of the substring is unknown:

int n; //some number derived somewhere else

String[] grp = elements[i] to elements[i+n];
2
  • 1
    Do you mean you want to create and array with the same values as another just longer in length? Commented Jul 6, 2011 at 13:56
  • 1
    A good (non-obvious) question. Note that I have replaced the word "selection" with "substring" in the question, which is the terminology you are after (even though it isn't a String, it is still the correct terminology in computer science terms). Commented Jul 6, 2011 at 13:59

4 Answers 4

20

Use Arrays.copyOfRange:

public static <T> T[] copyOfRange(T[] original,
                                  int from,
                                  int to)

Copies the specified range of the specified array into a new array. The initial index of the range (from) must lie between zero and original.length, inclusive. The value at original[from] is placed into the initial element of the copy (unless from == original.length or from == to). Values from subsequent elements in the original array are placed into subsequent elements in the copy. The final index of the range (to), which must be greater than or equal to from, may be greater than original.length, in which case null is placed in all elements of the copy whose index is greater than or equal to original.length - from. The length of the returned array will be to - from.

The resulting array is of exactly the same class as the original array.

In your case:

String[] grp = Arrays.copyOfRange(elements, i, i + n);
Sign up to request clarification or add additional context in comments.

1 Comment

Arrays.copyOfRange(elements, i, i + n) would exclude elements[i+n]. To include it, you should use Arrays.copyOfRange(elements, i, i+n+1). But maybe the OP intended to copy n elements in which case your answer is correct.
9

You will use Arrays.copyOfRange().

Here is an example:

String[] original = some array;
String[] grp = Arrays.copyOfRange(original, i, i + n);

The Javadocs for the Arrays class has lots of information about the method:

1 Comment

+1. You should add +1 to include elements[i+n] in the range.
4

Use Arrays.copyOfRange():

String[] grp = Arrays.copyOfRange(grp, i, i+n);

As the name implies grp will be a copy of the original array and not a view into it. You can't have views into the array, for that you'd need to use a collection. Generally speaking collections are the more powerful and flexible, high-level alternative to arrays.

Comments

1

To get the size of the array, you would do

String [] grp = new String[n + 1];//inclusive

Then, all you have to do is copy the elements over:

for(int x = 0;x < n + 1;x++)
{
    grp[x] = elements[i + x];//I'm assuming you have "i" defined somewhere
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.