Java 如何返回等价数组

Java 如何返回等价数组,java,Java,一个名为Equivalentarray的函数,它有两个数组参数,如果两个数组包含相同的值但不一定是相同的顺序,则返回1,否则返回0。请注意,数组不必具有相同数量的元素,它们只需要具有相同值的一个或多个副本 public class Equavalenarray { public static void main(String[] args) { int result= equivalentArrays(new int[] {}, new int[] {})

一个名为Equivalentarray的函数,它有两个数组参数,如果两个数组包含相同的值但不一定是相同的顺序,则返回1,否则返回0。请注意,数组不必具有相同数量的元素,它们只需要具有相同值的一个或多个副本

public class Equavalenarray {

    public static void main(String[] args) {

            int  result= equivalentArrays(new int[] {}, new int[]  {});
            System.out.println(result);     

            result=equivalentArrays (new int [] {0,2,1,2}, new int [] {0,2,1,2,1});
            System.out.println(result);
            result=equivalentArrays (new int [] {3,1,2,0}, new int [] {0,2,1,0});
            System.out.println(result);

    }

    public static int equivalentArrays(int[ ] a1, int[ ] a2) {
        if(a1==null || a2==null) return 0;

        for(int i=0; i<a1.length; i++) {
            for(int j=0; j<a2.length; j++) {
                if(a1[i]==a2[j] ) 
                {
                return 1;               
                }
        }


    }
        return 0;

}
}

你的功能就快到了。为了完成,我们需要在这里做一些变通

您可以检查较小数组中的每个值,并在重复值的情况下删除较大数组中的比较值。 如果在某些比较中它们不同,则返回0。如果到达较小数组的末尾,则它们相等

将for循环替换为以下代码段:

// converted the values to ArrayList to remove values easily.
// PS: to convert them to ArrayList, the array types must of the Integer
List<Integer> l1 = new ArrayList<Integer>(Arrays.asList(a1.length <= a2.length ? a1 : a2)); // to save in l1 the smaller array
List<Integer> l2 = new ArrayList<Integer>(Arrays.asList(a2.length >= a1.length ? a2 : a1)); // to save in l2 the bigger array

for(int i=0; i<l1.size(); i++) {
    if(!l2.contains(l1.get(i)))
        return 0;
    else
        l2.remove(l2.indexOf(l1.get(i)));
}
return 1;

更新了实时示例。

您的问题是什么?不必具有相同数量的元素。那么,如何将它们视为等价物呢?你是说这个{1,2,3,3}等价于{1,2,3}吗?代码返回1,这意味着在扫描整个数组之前,两个数组都有相同的值。数组的arraysize的长度大小可能不同,但数组中的值相同,如果两个数组中的值相同,则返回1,除非其中一个值不在另一个数组中,否则返回0。问题的标题和正文都不清楚。标题应该总结您的具体技术问题,是什么使您的问题在Stack Overflow上发布的数百万个问题中独一无二。你在正文中的文章应该更清楚地解释形势和目标。还要提到这是学校作业还是实际工作。示例代码应该指出预期的结果,什么定义了成功或失败。检查长度是多余的。Arrays.equals调用检查了这一点。这个答案并没有解决这个问题,这似乎是关于不同的值。用这个方法执行一些测试,因为如果数组有不同的长度,但次要的包含在主要的长度中,例如:{1,2,3,4}和{1,2,3,4,5},它将返回true。在您的情况下,您是对的,我的想法是,数组的arraysize的长度大小可能不同,但数组中的值是相同的,如果两个数组中的值相同,则返回1,除非其中一个值在另一个数组中不存在,否则返回0。
public class Equavalenarray {
    public static void main(String[] args) {
            System.out.println(equivalentArrays(new int[]{0,1,2}, new int[]{2,0,1}));
            System.out.println(equivalentArrays(new int[]{0,1,2,1}, new int[]{2,0,1}));
            System.out.println(equivalentArrays( new int[]{2,0,1}, new int[]{0,1,2,1}));
            System.out.println(equivalentArrays( new int[]{0,5,5,5,1,2,1}, new int[]{5,2,0,1}));
            System.out.println(equivalentArrays( new int[]{5,2,0,1}, new int[]{0,5,5,5,1,2,1}));
            System.out.println(equivalentArrays( new int[]{0,2,1,2}, new int[]{3,1,2,0}));
            System.out.println(equivalentArrays( new int[]{3,1,2,0}, new int[]{0,2,1,2}));
            System.out.println(equivalentArrays( new int[]{1,1,1,1,1,1}, new int[]{1,1,1,1,1,2}));
            System.out.println(equivalentArrays( new int[]{ }, new int[]{3,1,1,1,1,2}));
            System.out.println(equivalentArrays( new int[]{ }, new int[]{ }));

        }

        public static int equivalentArrays(int[] a1, int[] a2) {
            if(a1==null && a2==null) return 0;
            boolean found;

            for(int i : a1) {
                found = false;
                for(int j : a2) {
                    if(i==j) {
                        found = true;
                        break;
                    }
                }

                if(found==false) {
                    return 0;
                }
            }

            for(int i : a2) {
                found = false;
                for(int j : a1) {
                    if(i==j) {
                        found = true;
                        break;
                    }
                }

                if(found==false) {
                    return 0;
                }
            }
            return 1;
        }
    }
}