Answer the question
In order to leave comments, you need to log in
How to change array elements?
public static void main(String[] args) {
int[] arrey = {1, 2, 3, 4, 5};
int d = arrey.length;
System.out.println("Original:" + Arrays.toString(arrey));
for (int index = 0; index < arrey.length; index++) {
switch (index) {
case 0:
arrey[index] = arrey[d = d - 1];
break;
case 1:
arrey[index] = arrey[d = d - 1];
break;
case 2:
arrey[index] = arrey[d = d - 1];
break;
case 3:
arrey[index] = arrey[d = d - 1];
break;
case 4:
arrey[index] = arrey[d = d - 1];
}
System.out.println(Arrays.toString(arrey));
Answer the question
In order to leave comments, you need to log in
Good afternoon.
Answer to your question: https://www.baeldung.com/java-invert-array
Simple solution using Stream API:
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5};
int[] result = IntStream.rangeClosed(1, array.length)
.map(i -> array[array.length - i])
.toArray();
System.out.println(Arrays.toString(result));
}
public static void main(String[] args) {
Integer[] array = {1, 2, 3, 4, 5};
List<Integer> list = Arrays.asList(array);
Collections.reverse(list);
System.out.println(Arrays.toString(list.toArray()));
}
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5};
for (int i = 0; i < array.length / 2; i++) {
int tmp = array[i];
array[i] = array[array.length - 1 - i];
array[array.length - 1 - i] = tmp;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question