Java 程序反转数组

编写一个 Java 程序,使用 for 循环、while 循环和函数来反转数组。要反转数组,我们必须创建一个全新的数组,使用 for 循环从后往前迭代元素,并将它们添加到新数组中。

使用 for 循环反转数组的程序

在此示例中,for 循环 (for(i = Size – 1, j = 0; i >= 0; i–, j++)) 从下到上迭代,将每个值赋给另一个数组。接下来,我们使用另一个 for 循环打印反转后的数组。

package RemainingSimplePrograms;

import java.util.Scanner;

public class Example1 {
	private static Scanner sc;
	public static void main(String[] args) {
		int Size, i, j;
		
		sc = new Scanner(System.in);		
		System.out.print("Enter the size = ");
		Size = sc.nextInt();
		
		double[] a = new double[Size];
		double[] b = new double[Size];
		
		System.out.format("Enter %d elements  = ", Size);
		for(i = 0; i < Size; i++) 
		{
			a[i] = sc.nextDouble();
		}
		
		for(i = Size - 1, j = 0; i >= 0; i--, j++) 
		{
			b[j] = a[i];
		}
		
		System.out.println("\nThe Result");
		for(i = 0; i < Size; i++) 
		{
			System.out.print(b[i] + "  ");
		}
	}
}
Program to Reverse an Array using For Loop

在这个 示例 中,我们没有使用另一个循环,而是在一个 for 循环 中反转并打印了数组。

package RemainingSimplePrograms;

import java.util.Scanner;

public class ReverseAnArray3 {
	
	private static Scanner sc;
	
	public static void main(String[] args) {
		
		int Size, i;
		
		sc = new Scanner(System.in);
		
		System.out.print("Enter the Array size = ");
		Size = sc.nextInt();	
		
		double[] a = new double[Size];
		
		System.out.format("Enter %d Array elements  = ", Size);
		for(i = 0; i < Size; i++) 
		{
			a[i] = sc.nextDouble();
		}
		
		System.out.println("\nThe Result of the Reverse Array");
		for(i = a.length - 1; i >= 0; i--) 
		{
			System.out.print(a[i] + "  ");
		}
	}
}
Reverse an Array using for loop

使用 while 循环

此示例使用 while 循环 和临时变量来交换数组元素并反转它们。

package RemainingSimplePrograms;

import java.util.Scanner;

public class Example2 {
	private static Scanner sc;
	public static void main(String[] args) {
		int Size, i, j;
		double temp;
		
		sc = new Scanner(System.in);		
		System.out.print("Enter the size = ");
		Size = sc.nextInt();
		
		double[] a = new double[Size];
		
		System.out.format("Enter %d elements  = ", Size);
		for(i = 0; i < Size; i++) 
		{
			a[i] = sc.nextDouble();
		}
		
		j = i - 1;
		i = 0;
			
		while(i < j) 
		{
			temp = a[i];
			a[i] = a[j];
			a[j] = temp;
			i++;
			j--;
		}
		
		System.out.println("\nThe Result");
		for(i = 0; i < Size; i++) 
		{
			System.out.print(a[i] + "  ");
		}
	}
}
Enter the size = 6
Enter 6 elements  = 11 98.4 29.6 12.9 14.4 12.1

The Result
12.1  14.4  12.9  29.6  98.4  11.0  

使用递归函数反转数组的 Java 程序

在此示例中,我们创建了一个接受开始和结束值的 revArr() 函数。在 If 语句 中,revArr(a, start + 1, end – 1) 行通过递增的开始值和递减的结束值递归调用 revArr() 函数。

package RemainingSimplePrograms;

import java.util.Scanner;

public class Example4 {
	private static Scanner sc;
	public static void main(String[] args) {
		int Size, i;
		
		sc = new Scanner(System.in);		
		System.out.print("Enter the size = ");
		Size = sc.nextInt();
		
		int[] a = new int[Size];
		
		System.out.format("Enter %d elements  = ", Size);
		for(i = 0; i < Size; i++) 
		{
			a[i] = sc.nextInt();
		}
		
		revArr(a, 0, Size - 1);
		
		System.out.println("\nThe Result");
		for(i = 0; i < Size; i++) 
		{
			System.out.print(a[i] + "  ");
		}
	}
	
	public static void revArr(int[] a, int start, int end) {
		int temp;
		
		if(start < end)
		{
			temp = a[start];
			a[start] = a[end];
			a[end] = temp;
			revArr(a, start + 1, end - 1);
		}
	}
}
Reverse an Array using recursion function