Java列表反转程序


什么是列表反转?

列表反转是指交换或互换列表中元素位置的操作。在编写Java代码时,您可以轻松反转特定流程的顺序。这是计算机科学中任何编程语言的常规方法。reverse()方法是一个集合类的方法,它将第一个元素的位置反转到最后一个元素。在流程结束后,最后一个元素将占据第一个位置。

列表是一个接口,其中类方法表示为没有定义的签名。类将由此实现,方法获得特定定义。在本文中,我们将学习如何使用Java条件和不同的方法反转列表。

如何使用Java反转列表?

在这种方法中,我们必须提到一个指针,以通过更改节点来对链表执行反转过程。

  • 可以使用类反转方法,也称为Collections.reverse(),来反转Java中的ArrayList。在此方法中,数组列表将以线性时间进行,时间复杂度为O(n)。此方法接受List类型参数来执行程序。

  • 有时,当您使用Java编写代码时,需要从最后一个元素开始操作以反转数组。通过更改第一个和最后一个元素的位置,需要掌握该过程将运行直到中间元素交换其位置。

  • 要以反向方式打印数组,编码人员需要使用for循环从该特定数据集(即数组)的末尾启动打印操作。这是反转列表的常规方法。

  • Java中有许多接口可以反转列表,但是就地反转是节省机器内存的更好选择。

反转列表的算法

以下是使用Java反转链表的通用算法:

  • 步骤1 - 创建一个新的ArrayList。

  • 步骤2 - 使用add(E e) API输入一些数据。

  • 步骤3 - 反转列表的这些元素,并使用invoke reverse(List list) API。

语法

import java.util.Collections; (the Java Package)
Collections.reverse(the class_obj);

集合类的Reverse()方法建议对元素进行反转,以便可以对它们进行排序。

有几种方法可以使用Java反转列表:

  • 方法1 - 使用Collections.reverse()方法反转数组打印

  • 方法2 - 使用for循环反转数组

  • 方法3 - 就地方法反转数组

  • 方法4 - 使用Java 8 Stream API

  • 方法5 - 使用ListIterator

使用Collections.reverse()方法反转数组打印

Collections.reverse()方法是使用Java反转列表最可接受的方法。reverse方法遵循语法:public static void reverse(List<?> list) 来执行代码。

示例

public class reverseclassArray {
   static void reverse(int a[], int n){
      int[] b = new int[n];
      int j = n;
         for (int i = 0; i < n; i++) {
         b[j - 1] = a[i];
         j = j - 1;
      }
      System.out.println("Here the reversed array is:");
      for (int k = 0; k < n; k++) {
         System.out.println(b[k]);
      }
   }
   public static void main(String[] args){
      int [] arr12 = {101, 202, 303, 404, 505};
      reverse (arr12, arr12.length);
   }
}

输出

Here the reversed array is: 
505
404
303
202
101

使用for循环反转数组

我们可以使用for循环反转数组。在此方法中,使用现有数组注入一个新数组,并以此方式反向显示。

示例

public class Main { 
   static void reverse_array(char char_array[], int a) { 
      char[] dest_array = new char[a]; 
      int j = a; 
      for (int i = 0; i < a; i++) { 
         dest_array[j - 1] = char_array[i]; 
         j = j - 1; 
      } 
      System.out.println("Reversed array from this operation is: "); 
      for (int r = 0; r < a; r++){ 
         System.out.print(dest_array[r] + " "); 
      } 
   } 
   public static void main(String[] args){ 
      char [] char_array = {'I','N','D','I','A'}; 
      System.out.println("Original array print after the operation: "); 
      for (int s = 0; s <char_array.length; s++) { 
         System.out.print(char_array[s] + " ");
      }
      System.out.println();
      reverse_array(char_array, char_array.length); 
   } 
}

输出

Original array print after the operation: 
I N D I A 
Reversed array from this operation is: 
A I D N I 

就地方法反转数组

无需使用另一种类型的数组即可完成此操作。可以通过交换数组的第一个和最后一个数据来遵循该方法。

示例

public class reverseArray {
   static void reverse(int a[], int n){
      int[] b = new int[n];
      int j = n;
      for (int i = 0; i < n; i++) {
         b[j - 1] = a[i];
         j = j - 1;
      }
      System.out.println("Reversed array is after the operation:");
      for (int l = 0; l < n; l++) {
         System.out.println(b[l]);
      }
   }
   public static void main(String[] args){
      int [] arr = {1000, 2000, 3000, 4000, 5000};
      reverse(arr, arr.length);
   }
}

输出

Reversed array is after the operation: 
5000
4000
3000
2000
1000

使用Java 8 Stream API

使用Stream API,创建一个表示列表索引的int流。

示例

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class FavFashion{ 
   public static void main(String[] args) {
      List<String> clothesfavv = new ArrayList<>();
      clothesfavv.add("Raymond Shirt");
      clothesfavv.add("Impact Pants");
      clothesfavv.add("Socks of Champion");
      clothesfavv.add("Shoes By Bata");
      System.out.println("Before reversing the whole data:");
      System.out.println(clothesfavv);
      List reverseClothesfavv = IntStream.range(0, clothesfavv.size()).map(i -> clothesfavv.size() - 1-i).mapToObj(clothesfavv::get).collect(Collectors.toList());
      System.out.println("After reversing the whole data:");
      System.out.println(reverseClothesfavv);
   }
}

输出

Before reversing the whole data:
[Raymond Shirt, Impact Pants, Socks of Champion, Shoes By Bata]
After reversing the whole data:
[Shoes By Bata, Socks of Champion, Impact Pants, Raymond Shirt]

使用ListIterator

Java环境有一个迭代器类,可用于迭代不同的数据集集合。

示例

import java.util.*;
public class FashionCollector{
   public static void main(String[] args) {
      List<String> clothesstore2023 = new ArrayList<>();
      clothesstore2023.add("T-shirt Of Raymond");
      clothesstore2023.add("Pants By Impact");
      clothesstore2023.add("Socks Of Champion");
      clothesstore2023.add("Shoes Of Bata");
      List<String> reverseclothesstore2023 = new ArrayList<>();
      ListIterator<String> listIterator = clothesstore2023.listIterator(clothesstore2023.size());
      while(listIterator.hasPrevious()){
         String elemenString = listIterator.previous();
         reverseclothesstore2023.add(elemenString);
      }
      System.out.println("Before reversing the storage data:");
      System.out.println(clothesstore2023);
      System.out.println("After reversing the storage data:");
      System.out.println(reverseclothesstore2023);
   }
}

输出

Before reversing the storage data:
[T-shirt Of Raymond, Pants By Impact, Socks Of Champion, Shoes Of Bata]
After reversing the storage data:
[Shoes Of Bata, Socks Of Champion, Pants By Impact, T-shirt Of Raymond]

结论

因此,从以上讨论中,我们找到了如何使用Java反转列表的方法。在实现各种编码方法后,建议仔细理解这些方法。

当我们尝试使用Java反转列表时,可能会遇到几个问题。但这里有一个解决方案,编码人员可以以巧妙的方式应对这些问题。

更新于:2024年6月14日

18K+ 浏览量

开启您的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.