【问题标题】:Check if multidimensional array has values from an ArrayList检查多维数组是否具有来自 ArrayList 的值
【发布时间】:2015-10-16 04:04:03
【问题描述】:

假设我的 ArrayList 是:

["b1, "b2", "b3"]

我的多维数组是:

[["b", "b2", "b3"], ["b4", "b5", "b6"], ["b7", "b8", "b9"], ["b", "b4", "b7"], ["b2", "b5", "b8"], ["b3", "b6", "b9"], ["b", "b5", "b9"], ["b3", "b5", "b7"]];

我想检查我的 ArrayList 中的所有三个值是否也在我的多维数组中。

【问题讨论】:

  • 我不清楚。你的意思是如果“元组”[“b1,“b2”,“b3”]出现在多维数组中,或者每个字符串都在多维数组中的某个地方?
  • 你知道你也应该提交一个代码 sn-p,对吧?
  • ["b1", "b2", "b3"] 存在于多维数组@luksch
  • 顺序重要吗? IE。您是否希望 ["b2", "b", "b3"] 使用给定的多维数组返回 true
  • 顺序无所谓,可以是 ["b2", "b1", "b3"] @Turing85

标签: java android arrays multidimensional-array arraylist


【解决方案1】:

目前的问题是顺序无关紧要。为避免进一步复杂化,您可以简单地“排序”给定数组中的每一行(我们的名字是wordsArray2D),以及您的列表(我们称之为wordsList)。这是一个示例实现:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {
    public static void main(String... args) {
        // This is our "dictionary" of given "lines".
        String[][] wordsArray2D = {{"b1", "b2", "b3"},
                                  {"b4", "b5", "b6"},
                                  {"b7", "b8", "b9"},
                                  {"b", "b4", "b7"},
                                  {"b2", "b5", "b8"},
                                  {"b3", "b6", "b9"},
                                  {"b", "b5", "b9"},
                                  {"b3", "b5", "b7"}};

        // To neglect order, sort each line (since String is Comparable,
        // let's do Array.sort(...) the work for us)
        for (int idx = 0; idx < wordsArray2D.length; ++idx) {
            Arrays.sort(wordsArray2D[idx]);
        }

        // Our input, as List<String>. Add some content
        List<String> wordsList = new ArrayList<String>();
        wordsList.add("b2");
        wordsList.add("b1");
        wordsList.add("b3");

        // To start form even ground, convert the List into an Object[]...
        Object[] wordsListAsArray = wordsList.toArray();
        // ... and sort it as well. Again - this gets rid of ordering problems.
        Arrays.sort(wordsListAsArray);

        for (String[] wordsArray : wordsArray2D) {
            // Tow String[] are equal, iff their Strings at the correspinding
            // indices are equal. For this, we can use Arrays.equals(...)
            if (Arrays.equals(wordsArray, wordsListAsArray)) {
                System.out.println("found.");
                break;
            }
        }
    }
}

【讨论】:

    【解决方案2】:

    使用Collections.sort(Some collection) 对多维数组中的每个列表进行排序。稍后使用equals()检查两个列表是否相同

        ArrayList all=new ArrayList();
    
        ArrayList<String> al=new ArrayList<String>();
        al.add("a");
        al.add("b");
        al.add("c");
    
        ArrayList<String> al1=new ArrayList<String>();
        al1.add("c");
        al1.add("b");
        al1.add("a");
    
        ArrayList<String> al2=new ArrayList<String>();
        al2.add("a");
        al2.add("c");
        al2.add("b");
    
        ArrayList<String> al3=new ArrayList<String>();
        al3.add("a");
        al3.add("b");
        al3.add("c");
    
        all.add(al1);all.add(al2);all.add(al3);
    
        for(int i=0;i<all.size();i++)
        {
            ArrayList tmp=(ArrayList)all.get(i);
            Collections.sort(tmp);
            if(tmp.equals(al))
            System.out.println(all.get(i));
            //if both are same then do some action here.
        }
    

    输出:

    [a, b, c]
    [a, b, c]
    [a, b, c]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-11
      • 2013-03-15
      • 2010-09-26
      • 2010-09-13
      • 1970-01-01
      • 2011-07-11
      • 1970-01-01
      相关资源
      最近更新 更多