我正在尝试在Java中反转一个int数组。
该方法无法反转数组。
for(int i = 0; i < validData.length; i++)
{
int temp = validData[i];
validData[i] = validData[validData.length - i - 1];
validData[validData.length - i - 1] = temp;
}
它有什么问题?
我正在尝试在Java中反转一个int数组。
该方法无法反转数组。
for(int i = 0; i < validData.length; i++)
{
int temp = validData[i];
validData[i] = validData[validData.length - i - 1];
validData[validData.length - i - 1] = temp;
}
它有什么问题?
// solution 1 - boiler plated
Integer[] original = {100, 200, 300, 400};
Integer[] reverse = new Integer[original.length];
int lastIdx = original.length -1;
int startIdx = 0;
for (int endIdx = lastIdx; endIdx >= 0; endIdx--, startIdx++)
reverse[startIdx] = original[endIdx];
System.out.printf("reverse form: %s", Arrays.toString(reverse));
// solution 2 - abstracted
// convert to list then use Collections static reverse()
List<Integer> l = Arrays.asList(original);
Collections.reverse(l);
System.out.printf("reverse form: %s", l);
以下是简化版:
我的解决方案创建了一个新的反转数组 在每次 i 的迭代中,for 循环将最后一个索引 [array.length - 1] 插入当前索引 [i] 然后通过从最后一个索引中减去当前迭代 array[(array.length - 1) - i] 并将元素插入反转数组的下一个索引来继续相同的过程!
private static void reverse(int[] array) {
int[] reversed = new int[array.length];
for (int i = 0; i < array.length; i++) {
reversed[i] = array[(array.length - 1) - i];
}
System.out.println(Arrays.toString(reversed));
}
/// Reverse Iterator
public class ReverseIterator<T> implements Iterator<T> {
private int index;
private final List<T> list;
public ReverseIterator(List<T> list) {
this.list = list;
this.index = list.size() - 1;
}
public boolean hasNext() {
return index >= 0 ? true : false;
}
public T next() {
if(index >= 0)
return list.get(index--);
else
throw new NoSuchElementException();
}
}
对于数组情况的实现非常相似。当然,迭代器也可以是流或集合的来源。
因此,并不总是最好创建一个新数组来提供反向视图,当你想要做的只是遍历数组/列表或将其馈送到流或新的集合/数组时。
这有两个解决方案
循环
递归
public class _1_ReverseArray {
public static void main(String[] args) {
int array[] = {2, 3, 1, 4, 9};
//reverseArray(array, 0, array.length - 1);
reverseArrayWhileLoop(array, 0, array.length - 1);
printArray(array);
}
private static void printArray(int[] array) {
for (int a : array) {
System.out.println(a);
}
}
private static void reverseArray(int[] array, int start, int end) {
if (start > end) {
return;
} else {
int temp;
temp = array[start];
array[start] = array[end];
array[end] = temp;
reverseArray(array, start + 1, end - 1);
}
}
private static void reverseArrayWhileLoop(int[] array, int start, int end) {
while (start < end) {
int temp;
temp = array[start];
array[start] = array[end];
array[end] = temp;
start++;
end--;
}
}
}
一种简短的反转方式,无需额外的库、导入或静态引用。
int[] a = {1,2,3,4,5,6,7,23,9}, b; //compound declaration
var j = a.length;
b = new int[j];
for (var i : a)
b[--j] = i; //--j so you don't have to subtract 1 from j. Otherwise you would get ArrayIndexOutOfBoundsException;
System.out.println(Arrays.toString(b));
当然,如果您需要将a
数组反转,只需使用以下代码:
a = b; //after the loop
int[] validData = {1, 2, 3};
validData = Arrays.stream(validData)
.boxed()
.sorted(Collections.reverseOrder())
.mapToInt(Integer::intValue)
.toArray();
import java.util.Scanner;
class ReverseArray
{
public static void main(String[] args)
{
int[] arra = new int[10];
Scanner sc = new Scanner(System.in);
System.out.println("Enter Array Elements : ");
for(int i = 0 ; i <arra.length;i++)
{
arra[i] = sc.nextInt();
}
System.out.println("Printing Array : ");
for(int i = 0; i <arra.length;i++)
{
System.out.print(arra[i] + " ");
}
System.out.println();
System.out.println("Printing Reverse Array : ");
for(int i = arra.length-1; i >=0;i--)
{
System.out.print(arra[i] + " ");
}
}
}
public class TryReverse {
public static void main(String[] args) {
int [] array = {2,3,4,5,6,7,8,9};
reverse(array);
for(int i=0; i<array.length; ++i)
System.out.print(array[i] + " ");
}
public static void reverse (int [] array){
for(int start=0, end=array.length-1; start<=end; start++, end--){
int aux = array[start];
array[start]=array[end];
array[end]=aux;
}
}
}
public static void main (String args[]){
//create array
String[] stuff ={"eggs","lasers","hats","pie","apples"};
//print out array
for(String x :stuff)
System.out.printf("%s ", x);
System.out.println();
//print out array in reverse order
for(int i=stuff.length-1; i >= 0; i--)
System.out.printf("%s ",stuff[i]);
}
尝试使用JAVA编写此程序:
import java.util.Scanner;
public class Rev_one_D {
static int row;
static int[] trans_arr = new int[row];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
row = n;
int[] arr = new int[row];
for (int i = 0; i < row; i++) {
arr[i] = sc.nextInt();
System.out.print(arr[i] + " ");
System.out.println();
}
for (int i = 0; i < arr.length / 2; i++) {
int temp = arr[i];
arr[i] = arr[arr.length - i - 1];
arr[arr.length - i - 1] = temp;
}
for (int i = 0; i < row; i++) {
System.out.print(arr[i] + " ");
System.out.println();
}
}
}