两个二维数组,怎样判断它们两个互不包容。
也就是说:它们两个中不包含各自的元素。
(例如)
a二维数组中有
{1,2,3,4,5,6}
{3,45,5,6,7,44}
b二维数组中有
{33,56,77,9,10,80}
{6,8,9,45,88,90}
c二维数组中有
{122,55,67,99,80,199}
{777,595,97,234,898,102}
a跟b有包容元素,返回false
a跟c无包容元素,返回true

解决方案 »

  1.   

    拿一个数组的元素跟另一个数组的每一个元素比较,如果有相等的则停止,输出false,否则一直下去,如果没有,输出true.
      

  2.   

    是重复元素还是什么? a 中两个3,b中两个9
    还是a和b都有6 ?
      

  3.   

    看看Collection这个类
    如果简单一点的话,可以用removeAll, retainAll方法,看看原来那个数组是不是没有改变,或者清空了
      

  4.   

    1. you need to check whether the dimensions of these array are the same or not.
    2. using for loops to detect is the most simple way...
      

  5.   

    用2重循环比较就可以完成,但是效率就慢了下。
    或者试下用用arrays的sort方法先对b.c排下序,在用binarySearch(type[] a, type key)方法看看a中的元素是否在b.c中能否找到。
      

  6.   

    两个数组中的元素添加到HashSet<Integer>
    如果HashSet地size等于数据的长度就没有重复,如果不等于就有重复
    多维数组可以把每维数组的长度加起来
      

  7.   

    public class D {
    public static void main(String[] args)  {
    int[][] a = { {1,2,3,4,5,6},
    {3,45,5,6,7,44}} ;
    int[][] b = {{33,56,77,9,10,80} ,
    {11,8,9,45,88,90}};

    System.out.println(bijiao(a,b));

    }

    public static boolean bijiao(int x[][],int y[][]) {
    boolean z = true;
    for(int i=0;i<2;i++) {
    for(int j=0;j<6;j++) {
    for(int a=0;a<2;a++) {
    for(int b=0;b<6;b++) {
    if(x[i][j] == y[a][b]) {
    z = false;
    break;
    }



    }
    }
    }
    }

    return z;

    }
    }                  
         
      

  8.   

    首先Java本身有collection类
    你可以看看代码或者可以自己写将一个数组里的数据与另一个进行循环比较
      

  9.   

    不好意思误导大家了。。
    呵呵
    重新声明问题哦。
    a二维数组中有 
    {1,2,3,4,5,6} 
    {3,45,5,6,7,44} 
    b二维数组中有 
    {1,2,3,4,5,6} 
    {6,8,9,45,88,90} 
    c二维数组中有 
    {122,55,67,99,80,199} 
    {777,595,97,234,898,102} 
    a跟b有相同的一个元素{1,2,3,4,5,6}所以要返回false
    a跟c无相同的元素  返回true
    对不起,误导了!
    呵呵
    回答给150分。
      

  10.   

    public class D {
    public static void main(String[] args)  {
    int[][] a = { {1,2,3,4,5,6},
    {3,45,5,6,7,44}} ;
    int[][] b = {{1,2,3,4,5,6} ,
    {3,8,9,45,88,90}};
    int[][] c = {{122,55,67,99,80,199} ,
    {777,595,97,234,898,102} };
    System.out.println(bijiao(a,b));
    System.out.println(bijiao(a,c));
    }

    public static boolean bijiao(int x[][],int y[][]) {
    boolean z = true;
    for(int i=0;i<2;i++) {
    int t = 0;
    for(int j=0;j<6;j++) {
    for(int a=0;a<2;a++) {
    if(x[i][j] == y[a][j]) {
    t++;
    if(t == 6) {
    z = false;
    break;
    }
    }
    }
    }
    }
    return z;
    }
    }                  
         
      

  11.   


    /**
     * 
     */
    package houlei.test;import java.util.Arrays;/**
     * 该类创建于 2008-8-29 下午04:19:04
     * 
     * @version 1.0.0
     * @author 侯磊
     */
    public class E { public static void main(String[] args) {
    int a[][] = new int[][] { { 1, 2, 3, 4, 5, 6 }, { 3, 45, 5, 6, 7, 44 } };
    int b[][] = new int[][] { { 33, 56, 77, 9, 10, 80 }, { 6, 8, 9, 45, 88, 90 } };
    int c[][] = new int[][] { { 122, 55, 67, 99, 80, 199 }, { 777, 595, 97, 234, 898, 102 } };
    System.out.println(dissimilitude(a, b));
    System.out.println(dissimilitude(a, c));
    } public static boolean dissimilitude(int a[][], int b[][]) {
    int ta[] = sort(a);
    int tb[] = sort(b);
    int i = 0, j = 0;
    while (i < ta.length && j < tb.length) {
    if (ta[i] == tb[j])
    return false;
    else if (ta[i] < tb[j])
    i++;
    else
    j++;
    }
    return true;
    } public static int[] sort(int a[][]) {
    int t[] = new int[a.length * a[0].length];
    int index = 0;
    for (int i = 0; i < a.length; i++)
    for (int j = 0; j < a[0].length; j++) {
    t[index++] = a[i][j];
    }
    Arrays.sort(t);
    return t;
    }
    }
      

  12.   

    public class seek
    {
    public static void main(String str[])
    {
    int a[][]=new int[][];
    int b[][]=new int[][];
    find(a,b);
    find(b,a);
    }
    private void find(int a[][],int b[][])
    {
    for(int i=0;i<a.length;i++)
     {  for(int j=0;j<a[i].length;j++)
          if(findcode(a,b,i,j));
            if(array1.equals(array2))
           {System.out.println(" a include b Exit ");System.exit(0);//a包含b退出系统
              else
           {System.out.println(" b include a Exit ");System.exit(0);//b包含a退出系统}
    }//find end 
    private boolean findcode(int src[][],int tar[][],int row,int col)
    {
    for(int i=0;i<tar.length;i++)
     { 
     for(int j=0;j<tar[i].length;j++)
       if(src[row][col]== tar[i][j])
         {return true;}//包含元素
        else
         continue; 
          return false;//直到结束也不包含
      }}//findcode end     
          
    }//clsss end              
      

  13.   

    我也来个代码吧import java.util.*;public class FindDuplicate {
    public static void main(String args[]){
    int a[][] = new int[][] { { 1, 2, 3, 4, 5, 6 }, { 3, 45, 5, 6, 7, 44 } };
            int b[][] = new int[][] { { 33, 56, 77, 9, 10, 80 }, { 6, 8, 9, 45, 88, 90 } };
            
            System.out.print(detect(a, b));
            
    }

    public static boolean detect(int[][] a, int[][] b){
    int sizeFirst, sizeSecond, total;

    Set<Integer> setForArrayFirst  = new TreeSet<Integer>();
        Set<Integer> setForArraySecond  = new TreeSet<Integer>();
            
        for (int i=0; i<a.length; i++){
            for(int j=0; j<a[i].length; j++)
             setForArrayFirst.add(new Integer(a[i][j]));
        }
            
        for (int i=0; i<b.length; i++){
         for(int j=0; j<b[i].length; j++)
             setForArraySecond.add(new Integer(b[i][j]));
        }
            
        sizeFirst = setForArrayFirst.size();
        sizeSecond = setForArraySecond.size();
            
        total  = sizeFirst + sizeSecond;
            
        Iterator iterator = setForArraySecond.iterator();
        while(iterator.hasNext())
         setForArrayFirst.add((Integer)iterator.next());
               
        if (setForArrayFirst.size() < total){
            return false;
    }
            
        return true;
    }
    }
      

  14.   

    import java.util.Set;
    import java.util.HashSet;public class Test10 { /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub

    int[][] a = {
    {1, 2, 3, 4, 5, 6},
    {3, 45, 5, 6, 7, 44}
    };

    int[][] b = {
    {33, 56, 77, 9, 10, 80},
    {6, 8, 9,45, 88, 90}
    };

    int[][] c = {
    {122, 55, 67, 99, 80, 199},
    {777, 595, 97, 234, 898, 102}
    }; if (isContained(a, b)) {
    System.out.println("The array B contains some same elements with A");
    } else {
    System.out.println("The array B does not contain some same elements with A");
    }

    if (isContained(a, c)) {
    System.out.println("The array C contains some same elements with A");
    } else {
    System.out.println("The array C does not contain some same elements with A");
    }
    }

    public static boolean isContained(int[][] a, int[][] b) {
    Set<Integer> set = new HashSet<Integer>();

    for (int i = 0; i < a.length; i++) {
    for (int j = 0; j < a[i].length; j++) {
    set.add(a[i][j]);
    }
    }

    for (int i = 0; i < b.length; i++) {
    for (int j = 0; j < b[i].length; j++) {
    if (set.contains((Integer)b[i][j])) {
    return true;
    }
    }
    }
    return false;
    }}
    运行结果:
    The array B contains some same elements with A
    The array C does not contain some same elements with A
      

  15.   

    还有现在我太久没做个编程了。我是工程做实施的,呵呵
    所以大家最好做成接口形式。我给些我做的给大家看看,
    不过很乱。
    大家不要见外
    import java.util.*;
    class rule {
    public Random r = new Random(); public rule() { } public int[] rule1(int[] array) {
    int i = 0;
    while (i < 8) {
    array[i] = rule2();
    i++;
    }
    return array;
    } public int rule2() {
    int F = r.nextInt(34);
    if (F == 0) {
    F = r.nextInt(34);
    }
    return F;
    } public int[] rule3(int[] array) {
    int t = 0;
    for (t = 0; t < array.length; t++) {
    for (int j = t + 1; j < array.length; j++) {
    if (array[t] == array[j]) {
    array[j] = rule2();
    }
    }
    }
    return array;
    } public int[] rule4(int[] array) {
    int i, j;
    int temp;
    for (i = 0; i < array.length; i++) {
    for (j = i + 1; j < array.length; j++) {
    if (array[i] > array[j]) {
    temp = array[i];
    array[i] = array[j];
    array[j] = temp;
    }
    }
    }
    return array;
    }}public class ciaopiao {
        public ciaopiao() {
            //得到8个数的数组。
            List<Integer> list8=Get8From33();
            System.out.println(list8);
            //for test
            int MKJ[]=new int[8];
            
            //产生56个组合。
            for (int i=0;i<8;i++){
                for (int j=i+1;j<8;j++)
                {
                    List<Integer> list6=new ArrayList<Integer>();
                    for (int m=0;m<8;m++)
                        if (m!=i&&m!=j)
                            list6.add(list8.get(m));
                    
                    Collections.sort(list6);
                    //已经产生了一个6个数的数组。
                    System.out.println(list6);    
                }
            }
        }
        
        //交换List里,指定索引的两个元素。
        void Swip(List<Integer> list,int m,int n)
        {
            Integer temp = list.get(m);
            list.set(m,list.get(n));
            list.set(n,temp);
        }
       
        //从33个数中得到8个数。
       /* List<Integer> Get8From33() {
             List<Integer> list33 = new ArrayList<Integer>();
            for(int i=0;i<33;i++)
                list33.add(i+1);
            
            //随机的交换list33中的元素若干次。
            for (int i=0;i<100;i++){
                int m=(int)(Math.random()*list33.size());
                int n=(int)(Math.random()*list33.size());
                Swip(list33,m,n);
            }
            return list33.subList(0,8);
        }
       */
        //***************任选8个红球作为一组*************************
        public List<Integer> Get8From33(){
         List<Integer> listT=new ArrayList<Integer>();
         rule mkj=new rule();
         int array[]=new int[8];
         mkj.rule4(mkj.rule3(mkj.rule1(array)));
         for(int i=0;i<8;i++)
         {    
            listT.add(array[i]);
         }
         return listT;
        }
        
        public static void main(String[] args) {
            new ciaopiao();
        }
      

  16.   

    26楼的 用到了set.contains((Integer)b[i][j]这方法我还是头一次见阿不错
      

  17.   

    不过我 要提出质疑 
    boolean equals(Object o)比较指定对象与此 set 的相等性。如果指定的对象也是一个 set,两个 set 的大小相同,并且指定 set 的所有成员都包含在此 set 中(或者,此 set 的所有成员都包含在指定的 set 中也一样),则返回 true。此定义确保了 equals 方法可在不同的 set 接口实现间正常工作。 这是api里的解释 注意 我觉得这样不能判断contains some same elements 
      

  18.   


    public static void main(String[] args) 
    {

    int[][] a = new int[][]
    {
    { 1, 2, 3, 4, 5, 6 },
    { 3, 45, 5, 6, 7, 44 } };
    int[][] b = new int[][]
    {
    { 1, 2, 3, 4, 5, 6  },
    { 6, 8, 9, 45, 88, 90 } };

    Test t = new Test();
    System.out.println(t.check(a, b));
    }

    public boolean check(int a[][],int b[][])
    {
    String str = Arrays.toString(a[0]);
    String str2 = Arrays.toString(a[1]);
    for(int i=0;i<2;i++)
    for(int j=0;j<2;j++)
    {

    if(Arrays.toString(a[i]).equals(Arrays.toString(b[j])))
    {
    return false;
    }
    }
    return true;
    }}
      

  19.   

    改正public static void main(String[] args) 
        {
            
            int[][] a = new int[][]
            {
            { 1, 2, 3, 4, 5, 6 },
            { 3, 45, 5, 6, 7, 44 } };
            int[][] b = new int[][]
            {
            { 1, 2, 3, 4, 5, 6  },
            { 6, 8, 9, 45, 88, 90 } };
            
            Test t = new Test();
            System.out.println(t.check(a, b));
        }
        
        public boolean check(int a[][],int b[][])
        {
            
            for(int i=0;i<2;i++)
                for(int j=0;j<2;j++)
            {
                
                if(Arrays.toString(a[i]).equals(Arrays.toString(b[j])))
                {
                    return false;
                }
            }
            return true;
        }}
      

  20.   

    26楼不错,用到了hashset中不能有重复的值..