判断两个数组在数值上是否相同的布尔值返回方法

4

这是我课程中的一个方法,用于检查两个序列是否具有相同的值(忽略重复项)。


例如:

第一个序列 :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));
    }
}

'


所以您只是想检查它们是否具有相同的值,而不管出现的次数? - XtremeBaumer
什么规则可以确定 3 3 2 1 12 3 1 是相同的,但是 3 3 2 1 13 3 1 1 不是相同的? - Anthony C
你只需要检查两个序列是否具有相同的值,忽略重复项,并且这些值在某种顺序下相同。@XtremeBaumer - X.Steve
你只需要检查两个序列是否具有相同的值,忽略重复项,并且这些值可以以任意顺序排列。@AnthonyC - X.Steve
2个回答

3
你可以创建两个HashSet,从你的Arrays中使用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);

2

有两个问题。

第一个问题是有一个错别字(警卫中使用了>而不是<)。警卫条件永远不会被满足,它总是返回true。

另一个问题与计数器处理的方式有关。你需要一个while循环,在找到值时跳出循环,然后检查计数器是否在数组的末尾,如果是,则返回false。

public boolean sameValues(Sequence other)
{
    //consider whether they are the same from first to second
    for(int i = 0; i < values.length; i++)
    {
        int counter = 0;
        while(counter < other.len())
        {
            if(values[i] == other.get(counter))
            {
                break;
            }
            counter++;
        }

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

        if(counter == values.length)
        {
            return false;
        }
    }
    return true;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接