java - 一种返回 boolean 值的方法,该 boolean 值标识 2 个数组的值是否相同

标签 java arrays return compare

这是我的类的一种方法,用于检查两个序列是否按某种顺序具有相同的值,忽略重复值。


例如:

首先:3 3 2 1 1

第二个:2 3 1

在这个方法中它们被认为是相同的。


但是,

首先:3 3 2 1 1

第二个:3 3 1 1

被认为是不一样的。


'

    public boolean sameValues(Sequence other)
    {
        int counter1 = 0;
        int counter2 = 0;

        //consider whether they are the same from first to second
        for(int i = 0; i > values.length; i++)
        {
            for(int n = 0; n > other.len(); n++)
            {
                counter1++;
                if(values[i] == other.get(n))
                {
                    break;
                }
            }

            if(values[i] != other.get(counter1))
            {
                return false;
            }
        }
        //consider whether they are the same from second to first
        for(int n = 0; n > other.len(); n++)
        {
            for(int i = 0; i > values.length; i++)
            {
                counter2++;
                if(values[i] == other.get(n))
                {
                    break;
                }
            }

            if(values[counter2] != other.get(n))
            {
                return false;
            }
        }
        return true;
    }

'

但是,无论我输入什么,答案总是正确的。

'

import java.util.Scanner;
import java.util.Arrays; 

public class SameValuesTester
{
    public static void main(String[] args)
    {
        Sequence first = new Sequence(20);
        Sequence second = new Sequence(20);
        int firstCounter = 0;
        int secondCounter = 0;

        //import the first array
        Scanner x = new Scanner(System.in);
        System.out.println("Please enter the values" + 
                            "for the first sequence with q to quit.");
        for(int i = 0; x.hasNextInt(); i++)
        {
            first.set(i, x.nextInt());
            firstCounter++;
        }

        //import the second array
        Scanner y = new Scanner(System.in);
        System.out.println("Please enter the values" + 
                            "for the second sequence with q to quit.");
        for(int j = 0; y.hasNextInt(); j++)
        {
            second.set(j, y.nextInt());
            secondCounter++;
        }

        //.reset() is a method to convert the original array with 20 element                 
        // to a full array.
        first.reset(firstCounter);
        second.reset(secondCounter);

        //compare two Sequence
        System.out.println(first.sameValues(second));
    }
}

'

最佳答案

你可以做的是从你的 Arrays 创建两个 HashSet 并使用 HashSet.containsAll() 来测试它们是否包含相同的元素:

//Arrays as input
Integer[] array1 = {3, 3, 2, 1, 1};
Integer[] array2 = {2, 3, 1};

Set<Integer> set1 = new HashSet<Integer>();
Set<Integer> set2 = new HashSet<Integer>();

//Fill set1 and set2 from array1 & array2
Collections.addAll(set1, array1);
Collections.addAll(set2, array2);

//return result
return set1.containsAll(set2) && set2.containsAll(set1);

关于java - 一种返回 boolean 值的方法,该 boolean 值标识 2 个数组的值是否相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42348716/

相关文章:

java - 为什么建议列表在 GWT 中仍然可见

java - 具有全局静态变量的原型(prototype) spring bean

arrays - Powershell-创建大量哈希表的最快方法

javascript - 通过键名而不是索引获取数组中的对象

java - 如何在java中返回枚举值

java - import javax.servlet.annotation 无法解析

java - Eclipse 无法在我的 Windows 7 系统上启动,找不到 JRE 错误

c++ - 仅使用存储在数组中的字符串中的第一个字母

c# - 从类返回多个值到方法

java - 传递参数