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);
Answer from NPE on Stack Overflow
๐ŸŒ
CodeGym
codegym.cc โ€บ java blog โ€บ strings in java โ€บ substring in java
Java String substring()
String x = "CodeGymIsTheBest"; String y = x.substring (2,6); String z = x.substring (0,3); So, in JDK 7 and later, objects y and z created as a result of the substring() method applied to object x will refer to two newly created arrays (on the ...
Published: July 23, 2024
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ ref_string_substring.asp
Java String substring() Method
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Practice Problems Java Server Java Syllabus Java Study Plan Java Interview Q&A ... The substring() method returns a substring from the string.
๐ŸŒ
HappyCoders
happycoders.eu โ€บ home โ€บ java โ€บ java substring() method
Java substring() Method
June 12, 2025 - Therefore, a copy of the requested section of the char array is created. In Java 9, the substring method has been modified to take into account the encoding used (1-byte Latin 1 vs. 2-byte UTF-16).
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 29382983 โ€บ getting-a-substring-of-an-array-in-java
Getting a substring of an array in java - Stack Overflow
I know for strings you have substring(inclusive, exclusive) is there anything similar for an array or do I have to convert the array to a string in order to do such a thing? ... what do you mean by "reverse" order? why comparing {1, 2, 1} with {4, 1, 2} and how? Please share more examples with clear requirement and understanding. ... java.util.Arrays.copyOfRange (specialized for each primitive element type or Object) gives a portion, in the same order, like String.substring.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ extracting-a-substring-as-an-array-of-characters-in-java
Java - String getChars() Method
In the following example, we are creating an object of the string class with the value "Hello World". Then, we create a char array with size 20. Using the getChars() method, we are trying to copy the string characters from the current string ...
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ 8 โ€บ docs โ€บ api โ€บ java โ€บ lang โ€บ String.html
String (Java Platform SE 8 ) - Oracle Help Center
July 21, 2026 - The replacement proceeds from the beginning of the string to the end, for example, replacing "aa" with "b" in the string "aaa" will result in "ba" rather than "ab". ... Splits this string around matches of the given regular expression. The array returned by this method contains each substring ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ substring-in-java
Java String substring() Method - GeeksforGeeks
April 11, 2025 - So it will make substring from end of the given string and we get the substring as "Geeks". ... Example 2: Java program to extract a substring from specified start index to specified end index using substring(int beginIndex, int endIndex).
Find elsewhere
๐ŸŒ
Blogger
javarevisited.blogspot.com โ€บ 2011 โ€บ 10 โ€บ how-substring-in-java-works.html
How SubString method works in Java - Memory Leak Fixed in JDK 1.7
Because I think the new string created by substring will keep the reference of the array not the original string. So it won't stop original string to be garbage collected. It will stop the byte array to be garbage collected if the new string object is referenced by other objects. ... Join My Newsletter .. Its FREE ยท Everything Bundle (Java + Spring Boot + SQL Interview) for a discount ... How to sort an Array in Java? Ascending and Descen... How to loop through an Array in Java? Example ...
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 70026085 โ€บ substring-into-an-array-in-java
Substring into an array in Java - Stack Overflow
If your goal is to store the String in an array you can pass it in with a regular array declaration and assignment: String[] data = { line.substring(i, i + 1) }; or just make a new array and pass it into the desired index.
๐ŸŒ
LinkedIn
linkedin.com โ€บ pulse โ€บ working-java-substring-6-jyoti-jindal
Implementation of Java substring() in Java 6, and why it was changed in Java 7?
February 14, 2023 - String substr = new String(bigString.substring(0, 2)); Now, new String object is created in java heap, having its own char[] array, eventually original bigString will eligible for garbage collection process.
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2013 โ€บ 12 โ€บ java-string-substring-method-example
Java String substring() Method with examples
Note: The method is called like this: substring(start + 1, end), here start index is +1 because, we want to get the substring after the delimiter position, however the end index is not +1 because end index is not inclusive in substring method. public class JavaExample{ public static void ...
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 486735 โ€บ java โ€บ fast-substring-search-Array-strings
How to do a fast substring search on an Array of strings? (Java in General forum at Coderanch)
March 11, 2010 - Searching the needle using Pattern.matches(".*needle*.*"). Found 1,400,000 needles in 149,735 milliseconds. Searching the needle using pattern.matcher(hayStack[i]).matches(). Found 1,400,000 needles in 34,379 milliseconds. Hope this is useful. Matt PS: I wanted to attach the source code I've used.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 25566232 โ€บ how-can-i-store-substrings-generated-from-a-string-into-a-string-array
java - How can I store substrings generated from a String into a String array? - Stack Overflow
April 21, 2017 - String word = "WOMAN"; String[] res = new String[20]; String sub; int i, j, k; int len = word.length(); for(i = 0; i < len; i++) { for(j = 0; j <= len-i; j++) { sub = word.substring(i, i+j); for(k = 0; k < res.length; k++) res[k] = sub; } }
๐ŸŒ
How to do in Java
howtodoinjava.com โ€บ home โ€บ string โ€บ java string substring()
Java String substring() with Examples - HowToDoInJava
January 10, 2023 - As mentioned earlier, if we pass an invalid index (such as negative index value) that does not exist in the character array, or the begin index is greater than end index then we will get the IndexOutOfBoundsException in runtime. Assertions.assertThrows(IndexOutOfBoundsException.class, () -> { //invalid index str.substring(-1); }); Assertions.assertThrows(IndexOutOfBoundsException.class, () -> { //begin index > end index str.substring(6, 4); }); In this short Java tutorial, we learned to get the substring of a specified string using the begin and end indices.
๐ŸŒ
Educative
educative.io โ€บ blog โ€บ how-do-you-master-java-strings-and-arrays-for-beginners
How do you master Java strings and arrays for beginners
Substring extraction: The substring() method has two variants, one taking a start index only and another taking both start and end indexes, where the end index character is excluded from the result. Array initialization and operations: Arrays can be initialized with default values, after declaration using the new keyword, or inline with assigned values, and Java's Arrays.sort() and Arrays.binarySearch() handle sorting and searching efficiently.