无法获取多维未排序数组中的所有公共元素

时间:2016-10-02 02:55:52

标签: java arrays multidimensional-array

解决问题的标准: 将复杂性降低到线性。我只能将数组可比较的集合类型一起使用。应该允许重复元素。

我的担忧: 我的代码片段(获取公共元素(字符串)的方法):

//Comparable[] items1 = {'a', 'c', 'd', 'e', 'f','d',};
//Comparable[] items2 = {'a', 'c','d', 'e', 'd','f', 'g', 'h'};
//Comparable[] items3 = {'a', 'c','d', 'e','d', 'g'};

//Comparable[][]  items = {items1, items2, items3};

public static ArrayList<Comparable> findCommonElements(Comparable items[][]) {
    int i, j=0; 

    int rows = items.length;

    ArrayList common = new ArrayList(); 
    int []x = new int[rows]; // 

    for(i=0; i<items.length; i++) {
        // here is where I have the problem. It doesn't loop over the entire list in first row.   Therefore, I can only get the first 3. 
        Comparable value = collections[0][x[0]];// obtain the first row. 


        for(j=0;j<items[i].length; j++) {     


             if(items[i][j].compareTo(value)==0) {// compare the values. 
                common.add(value); 
                x[0]++;      

             }
        }
    }
    return common;
}

我的输出:

  

[a,c,d]

正确输出:

  

[A,C,d,E,d]

0 个答案:

没有答案
相关问题