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 andoriginal.length, inclusive. The value atoriginal[from]is placed into the initial element of the copy (unlessfrom == original.lengthorfrom == 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 thanoriginal.length, in which case null is placed in all elements of the copy whose index is greater than or equal tooriginal.length - from. The length of the returned array will beto - 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 OverflowUse 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 andoriginal.length, inclusive. The value atoriginal[from]is placed into the initial element of the copy (unlessfrom == original.lengthorfrom == 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 thanoriginal.length, in which case null is placed in all elements of the copy whose index is greater than or equal tooriginal.length - from. The length of the returned array will beto - 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);
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:
Getting a substring of an array in java - Stack Overflow
Array and .substring in java - Stack Overflow
Substring into an array in Java - Stack Overflow
Java manipulating substring/char in a string array - Stack Overflow
You could do it pretty much as described, first create a new array of the same length as names (since you want to keep all of the names, just rotate them). Next, copy every name with an offset of one from names to that second array. Finally, copy the first name to the last element in the second array (and print it). Like,
String[] names = { "Jeremy", "Aude", "David" };
String[] names2 = new String[names.length];
System.arraycopy(names, 1, names2, 0, names.length - 1);
names2[names2.length - 1] = names[0];
System.out.println(Arrays.toString(names2));
Outputs (as requested)
[Aude, David, Jeremy]
extract the index 1 and 2
names[1] and names[2]
element Jeremy also
names[0]
concatenate ... result that I want to get: Aude, David, Jeremy
String result = names[1] + ", " + names[2] + ", " + names[0];
would like to use the method .substring()
You can't, since that method is not for array manipulation.
Code
String[] names = {"Jeremy", "Aude", "David"};
String result = names[1] + ", " + names[2] + ", " + names[0];
System.out.println(result);
Output
Aude, David, Jeremy
Java's Strings are immutable, meaning you can't change them. Instead, as @AshishSingh notes in the comments, you'll need to create a new String.
Just do this:
fruits[0] = manipulate(fruits[0]);
Here, manipulate() is your function which takes an input string, manipulates it however you want, and then returns the manipulated string.
public String manipulate(String oldStr) {
StringBuilder newStr = new StringBuilder(oldStr);
newStr.setChar(1, 'b')
return newStr.toString();
}
I'm using StringBuilder which is a mutable object, so can have elements reassigned. I set the second character to 'b' and then return the new String.
The Java String object is immutable, so you can't modify its internal value.
char charArray[] = fruits[index].toCharArray();
charArray[2] = 'b';
After modifying the elements in the character array, put it back into fruits array.
fruits[index] = String.valueOf(charArray);
The existing fruits[index] will be replaced by the new String.