java reverse_Java中reverse()和reverseOrder()之间的区别

java reverse

Java中的reverse()vs reverseOrder() (reverse() vs reverseOrder() in Java)

Here, we will see how reverse() method differs from reverseOrder() in Java?

在这里,我们将看到reverse()方法与Java中的reverseOrder()有何不同?

逆转() (reverse())

  • This method is available in java.util package.

    此方法在java.util包中可用。

  • This method is static so this method is accessible with the class name too.

    此方法是静态的,因此也可以使用类名访问此方法。

  • With the help of reverse() method to reverse the elements of Collection.

    借助reverse()方法来反转Collection的元素。

  • This method takes one argument of the method of Collections class and the argument will be Collection object List.

    此方法采用Collections类的方法的一个参数,该参数将为Collection对象List。

  • This return type of this method is void so it does not return anything.

    此方法的此返回类型为void,因此它不返回任何内容。

  • The syntax of this method is given below:

    该方法的语法如下:

    public static void reverse(List obj){}
    

Example:

例:

// Java program to demonstrate the behavior of reverse() method

import java.util.Collections;
import java.util.LinkedList;

class CollectionsReverseMethod {
    public static void main(String[] args) {

        // Creating an instance of LinkedList
        LinkedList ll = new LinkedList();

        // By using add() method to add few elements in a lits
        ll.add(10);
        ll.add(30);
        ll.add(70);
        ll.add(60);
        ll.add(50);

        // Display Current Linked List
        System.out.println("Current List is :" + ll);

        // Implement reverse() method
        Collections.reverse(ll);

        // Display New Reversed Linked List
        System.out.println("New Reversed List is " + ll);
    }
}

Output

输出量

E:\Programs>javac CollectionsReverseMethod.java

E:\Programs>java CollectionsReverseMethod
Current List is :[10, 30, 70, 60, 50]
New Reversed List is [50, 60, 70, 30, 10]

Here, we will see how reverseOrder() method differs from reverse() in Java?

在这里,我们将看到reverseOrder()方法与Java中的reverse()有何不同?

相反的顺序() (reverseOrder())

  • This method is available in java.util package.

    此方法在java.util包中可用。

  • This method is also a static method so this method is accessible with the class name too.

    此方法也是静态方法,因此也可以使用类名访问此方法。

  • With the help of the reverseOrder() method to reverse the original sorting order and return Comparator.

    借助reverseOrder()方法来反转原始排序顺序并返回Comparator。

  • This method is an overloaded method. The two flavors are available of this method first reverseOrder() and second, reverseOrder(Comparator c).

    此方法是重载方法。 此方法有两种形式,第一种方法verseverseOrder()和第二种方法reverseOrder(Comparator c)

  • This reverseOrder() does not take any argument of the method and return Comparator and this method is applicable for the natural order of reversing that implement the Comparable interface.

    reverseOrder()不接受该方法的任何参数并返回Comparator,并且此方法适用于实现Comparable接口的自然逆序

  • This reverseOrder(Comparator) takes only one argument of the method and return Comparator and this method is applicable for the order of reversing of the given Comparator.

    reverseOrder(Comparator)仅采用方法的一个参数,然后返回Comparator,此方法适用于给定Comparator的反向顺序。

  • The return type of this method is Comparator so it returns Comparator.

    此方法的返回类型为Comparator,因此它返回Comparator。

  • The syntax of this method is given below:

    该方法的语法如下:

    public static Comparator reverseOrder()
    public static Comparator reverseOrder(Comparator comp)
    

Example:

例:

// Java program to demonstrate the behavior of reverseOrder() method

import java.util.Collections;
import java.util.LinkedList;
import java.util.Comparator;

class CollectionsReverseOrderMethod {
    public static void main(String[] args) {
        // Creating an instance of LinkedList
        LinkedList ll = new LinkedList();

        // By using add() method to add few elements in a lits
        ll.add(10);
        ll.add(30);
        ll.add(70);
        ll.add(60);
        ll.add(50);

        // Display Current Linked List
        System.out.println("Current List is :" + ll);

        // creates a comparator object and then reverse
        Comparator comp =
            Collections.reverseOrder(null);

        Collections.sort(ll, comp);

        // Display New Reversed Linked List
        System.out.println("New Reversed List is given below " + ll);
    }
}

Output

输出量

E:\Programs>javac CollectionsReverseOrderMethod.java

E:\Programs>java CollectionsReverseOrderMethod
Current List is :[10, 30, 70, 60, 50]
New Reversed List is given below [70, 60, 50, 30, 10]


翻译自: https://www.includehelp.com/java/differences-between-the-reverse-and-reverseorder-in-java.aspx

java reverse

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值