【问题标题】:Search index of one Dimensional array in two dimensional array and return row by row java在二维数组中搜索一维数组的索引并逐行返回java
【发布时间】:2015-04-20 23:49:51
【问题描述】:

我有个问题是我有一个一维数组

String[] purposeAll= {"P1","P2"};

和数组二维

String[][] purpose = { 
        { "P1", "a", "b", "c", "d", "e", "f", "h" },
        { "P2", "a", "b", "c", "d", "e", "g", "i" },
        { "P3", "l", "m", "n", "o", "p", "q" } };

我需要在二维数组中搜索一维数组的每个索引,结果会得到:

arrayOfPurpose[][]={ 
        { "P1", "a", "b", "c", "d", "e", "f", "h" },
        { "P2", "a", "b", "c", "d", "e", "g", "i" }  };

我的代码是

public static void ArrayOfPurpose(String[][] purpose, String[] purposeAll) {

String[][][] arrayOfPurpose = new String[purposeAll.length][purpose.length][];
    ArrayList<String[]> list = new ArrayList<String[]>();

    for (int i = 0; i < purpose.length; i++) {
        list.add(purpose[i]);
    }
    String[] tmp;
    for (int k = 0; k < purposeAll.length; k++) {
        arrayOfPurpose[k]= new String[purposeAll.length][];
        System.out.print("ok");
        for (int i = 0; i < list.size(); i++) {

            tmp = list.get(i);
            arrayOfPurpose[i]= new String[purposeAll.length][tmp.length];
            for (int j = 0; j < purpose[i].length; j++) {
                 if (tmp[0] == (purposeAll[k])) {
                    arrayOfPurpose[k][i][j] = tmp[j];
                  System.out.println(arrayOfPurpose[k][i][j]);
                }

            }

        }
    }
}

非常感谢!! :)

【问题讨论】:

  • 为了清楚起见......您想从purposeAll[] 中获取每个值并检查它的值是否是二维数组值列表中的第一个值。如果是这样,将其拉出并将整个 String[] 放入 arrayOfPurpose?
  • 为什么你的代码中有arrayOfPurpose 3d?
  • @AdrianLeonhard - 这不是 3D
  • 有什么问题?到目前为止,您的代码有什么问题?
  • @Ascalonian String[][][] arrayOfPurpose = new String[purposeAll.length][purpose.length][];

标签: java arrays search multidimensional-array bigdata


【解决方案1】:

Java8 流解决方案:

public class blah {

    static String[] purposeAll= {"P1","P2"};

    static String[][] purpose = { 
            { "P1", "a", "b", "c", "d", "e", "f", "h" },
            { "P2", "a", "b", "c", "d", "e", "g", "i" },
            { "P3", "l", "m", "n", "o", "p", "q" } };
    public static String[][] arrayOfPurpose(String[][] purpose, String[] purposeAll) {
        return Arrays.asList(purpose).stream()
            .filter(arr -> Arrays.asList(purposeAll).contains(arr[0]))
            .toArray(String[][]::new);
    }
    public static void main(String[] args) {
        System.out.println(Arrays.deepToString(arrayOfPurpose(purpose, purposeAll)));
        // prints [[P1, a, b, c, d, e, f, h], [P2, a, b, c, d, e, g, i]]
    }
}

【讨论】:

  • 我得到了结果:P3lmnopq 但不是 P1 和 P2。
【解决方案2】:

使用您在问题中的内容,以非 Java8 方式,您可以这样做:

for (String valueToCheck : purposeAll) {
    for (String[] values : purpose) {
        if (values[0].equals(valueToCheck)) {
            list.add(values);
        }
    }
}

String[][] arrayOfPurpose = new String[list.size()][];

int index = 0;

for (String[] a : list) {
    arrayOfPurpose[index++] = a;
}


System.out.println("Size of arrayOfPurpose: " + arrayOfPurpose.length);

【讨论】:

  • 没有 Arrays.asList().contains()?
  • 哥们,它几乎与头脑相似,但我面临的问题是无法插入数组[][]。 :(
  • @DaraTith - 看看新的 cmets。我用那个逻辑让它工作了:-)
  • @AdrianLeonhard - 有很多方法可以检查。由于 OP 似乎正在学习这一点,我想我会举一个更直接的例子
【解决方案3】:

我希望我正确理解了这个问题。

    String[] purposeAll= {"P1","P2"};
    //String[] purposeAll= {"P3","P1"}; Testing Stuff.

    String[][] purpose = { 
    { "P1", "a", "b", "c", "d", "e", "f", "h" },
    { "P2", "a", "b", "c", "d", "e", "g", "i" },
    { "P3", "l", "m", "n", "o", "p", "q" } };

    List<String[]> arrayOfPurposes = new ArrayList<>();
    //Looping through the purposeAll to check purpose's first 
    //entry and seeing if it equals the first purposeAll value
    for(int i = 0; i < purpose.length; i++){
        for(int j = 0; j < purposeAll.length; j++){
            if(purpose[i][0].equals(purposeAll[j])){
                arrayOfPurposes.add(purpose[i]);
            }
        }
    }

    //One way to get the results into a String[][]
    String[][] result = {arrayOfPurposes.get(0),arrayOfPurposes.get(1)};

    //Displaying to console
    for(int i = 0; i < result.length; i++){
        for(int j = 0; j < result[i].length; j++){
            System.out.print(result[i][j]);
        }
        System.out.println();
    }

我觉得你应该使用 String[] 的 List 而不是 String[][] 因为 result 的初始化非常简单。在大多数情况下,列表也更有效率和帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-27
    • 1970-01-01
    • 2021-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-06
    • 2017-08-16
    相关资源
    最近更新 更多