集册 Java实例教程 数组等于忽略顺序

数组等于忽略顺序

欢马劈雪     最近更新时间:2020-01-02 10:19:05

524
数组等于忽略顺序

/*

 * oxCore is available under the MIT License (2008). See http://opensource.org/licenses/MIT for full text.

 *

 * Copyright (c) 2014, Gluu

 */

//package com.nowjava;/*来 自 N  o w  J a v a . c o m*/

import java.lang.reflect.Array;

import java.util.Arrays;


public class Main {

    public static void main(String[] argv) throws Exception {

        String[] values1 = new String[] { "1", "abc", "level", null,

                "nowjava.com", "asdf 123" };

        String[] values2 = new String[] { "1", "abc", "level", null,

                "nowjava.com", "asdf 123" };

        System.out.println(equalsIgnoreOrder(values1, values2));

    }

    /**
    N o w  J a v a  . c o m 提供 
    **/

    public static boolean equalsIgnoreOrder(String[] values1,

            String[] values2) {

        String[] valuesSorted1 = sortAndClone(values1);

        String[] valuesSorted2 = sortAndClone(values2);


        return Arrays.equals(valuesSorted1, valuesSorted2);

    }


    public static String[] sortAndClone(String[] array) {

        if (array == null) {

            return array;

        }


        String[] clonedArray = arrayClone(array);

        Arrays.sort(clonedArray);


        return clonedArray;

    }


    @SuppressWarnings("unchecked")

    public static <T> T[] arrayClone(T[] array) {

        if (array == null) {

            return array;

        }

        
展开阅读全文