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+ 瀏覽量

開啟您的職業生涯

完成課程獲得認證

開始學習
廣告