Java中的 Collections.reverse() 方法及示例
顾名思义,Collections 类的reverse()方法用于反转存储它们的对象中的元素。它反转作为参数传递的列表中元素的顺序。此类存在于Java.util 包中,因此语法如下:
import java.util.Collections;
Collections.reverse(class_obj);
插图:
Input : {1, 2, 3, 4}
Output : {4, 3, 2, 1}
参数:要反转其元素的类的对象。
public static void reverse(List myList)
抛出异常:如果指定列表或其列表迭代器不支持集合操作,则抛出UnsupportedOperationException 。
让我们通过下面列出的用例来看看这种方法的用法,如下所示:
- 反转 ArrayList
- 反转链表
- 反转数组
让我们通过在 clan Java代码中实现相同的方法来实现 Collections 类的这个方法,如下所示:
案例 1:反转 ArrayList
JAVA
// Java program to illustrate reverse() method
// of Collections class over ArrayList
// Importing utility classes
import java.util.*;
// Main class
public class GFG {
// main driver method
public static void main(String[] args)
{
// Let us create a list of strings
List mylist = new ArrayList();
// Adding elements to the List
// Custom input elements
mylist.add("practice");
mylist.add("code");
mylist.add("quiz");
mylist.add("geeksforgeeks");
// Print all elements originally
System.out.println("Original List : " + mylist);
// Using reverse() method to
// reverse the element order of mylist
Collections.reverse(mylist);
// Print all elements of list in reverse order
// using reverse() method
System.out.println("Modified List: " + mylist);
}
}
Java
// Java program to illustrate reverse() method
// of Collections class over ArrayList
// Importing utility classes
import java.util.*;
// Main class
public class GFG {
// main driver method
public static void main(String[] args)
{
// Let us create a list of strings
List mylist = new LinkedList();
// Adding elements to the List
// Custom input elements
mylist.add("practice");
mylist.add("code");
mylist.add("quiz");
mylist.add("geeksforgeeks");
// Print all elements originally
System.out.println("Original List : " + mylist);
// Using reverse() method to
// reverse the element order of mylist
Collections.reverse(mylist);
// Print all elements of list in reverse order
// using reverse() method
System.out.println("Modified List: " + mylist);
}
}
Java
// Java program to Illustrate Reversal of Array
// using reverse() method of Collections class
// Importing utility classes
import java.util.*;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating input integer array
Integer arr[] = { 10, 20, 30, 40, 50 };
// Print elements of array
System.out.println("Original Array : "
+ Arrays.toString(arr));
// Reversing elements of array using reverse()
// method of Arrays class and fetching as
// list via asList()
Collections.reverse(Arrays.asList(arr));
// Print and display reverse updated array
System.out.println("Modified Array : "
+ Arrays.toString(arr));
}
}
输出
Original List : [practice, code, quiz, geeksforgeeks]
Modified List: [geeksforgeeks, quiz, code, practice]
案例 2:反转 LinkedList
Java
// Java program to illustrate reverse() method
// of Collections class over ArrayList
// Importing utility classes
import java.util.*;
// Main class
public class GFG {
// main driver method
public static void main(String[] args)
{
// Let us create a list of strings
List mylist = new LinkedList();
// Adding elements to the List
// Custom input elements
mylist.add("practice");
mylist.add("code");
mylist.add("quiz");
mylist.add("geeksforgeeks");
// Print all elements originally
System.out.println("Original List : " + mylist);
// Using reverse() method to
// reverse the element order of mylist
Collections.reverse(mylist);
// Print all elements of list in reverse order
// using reverse() method
System.out.println("Modified List: " + mylist);
}
}
输出
Original List : [practice, code, quiz, geeksforgeeks]
Modified List: [geeksforgeeks, quiz, code, practice]
如果我们看一下上面的程序,那么只有一分钟的签名细节,我们只是在创建一个 LinkedList 类的对象,而不是在 example1A 中看到的 Array 类。对于 LinkedList,我们只是在上面的代码中做了如下更改:
LinkedList in "List mylist = new ArrayList();".
案例 3:反转数组: Java中的 Arrays 类没有反转方法。我们可以使用 Collections.reverse() 来反转数组,如下所示:
例子
Java
// Java program to Illustrate Reversal of Array
// using reverse() method of Collections class
// Importing utility classes
import java.util.*;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating input integer array
Integer arr[] = { 10, 20, 30, 40, 50 };
// Print elements of array
System.out.println("Original Array : "
+ Arrays.toString(arr));
// Reversing elements of array using reverse()
// method of Arrays class and fetching as
// list via asList()
Collections.reverse(Arrays.asList(arr));
// Print and display reverse updated array
System.out.println("Modified Array : "
+ Arrays.toString(arr));
}
}
输出
Original Array : [10, 20, 30, 40, 50]
Modified Array : [50, 40, 30, 20, 10]