首页 > java > 如何将字符串放入矩阵?

如何将字符串放入矩阵? (How to put strings into a matrix?)

问题

我遇到麻烦,也许你可以帮助我:我有3个字符串,如:word1,word2,word3,我必须用它们构建一个矩阵,如下所示:在第一行:word1(“ABC”),第二行:word2(“DEF”)和第三行:word3(“GHI”)。

A|B|C
D|E|F
G|H|I

我需要这个,因为在那之后我必须检查所形成的单词(“ADG”,“BEH”,“CFI”)是否在一个单词数组中。而且我不知道如何将这些字符串放在矩阵中以便我可以检查。任何帮助都很有用。谢谢

解决方法

根据这个评论:

单词大小相同,因为矩阵实际上就像一个谜题。我从一个数组中随机选择3个单词,将它们放在一个矩阵中,然后检查所得单词是否来自同一个数组。

为了使这项工作,我会假设一些事情(因为我们没有足够的信息):

  1. 你有一个String包含所有单词的数组

    private String[] words;
    
  2. 你有一个String从这个数组中随机选取3 秒的方法。

    private String s1, s2, s3;
    
    public void pickThreeRandomWords() {
        s1 = aRandomWord(words);
        s2 = aRandomWord(words);
        s3 = aRandomWord(words);
        //or maybe another fancy algorithm to get this...
    }
    

所以你需要一个char基于这些3 Strings 的s 数组。此代码可以为您完成工作:

public char[][] createMatrixFromStrings(String s1, String s2, String s3) {
    char[][] theMatrix = new char[3][]; //yes, hardcoded
    theMatrix[0] = s1.toCharArray();
    theMatrix[1] = s2.toCharArray();
    theMatrix[2] = s3.toCharArray();
    return theMatrix;
}

当然,如果你想让这个方法支持超过3 String秒,你可以使方法接收随机数量的Strings:

public char[][] createMatrixFromStrings(String ... strings) {
    if (strings == null || strings.length == 0) return null;
    char[][] theMatrix = new char[strings.length][];
    int i = 0;
    for(String s : strings) {
        theMatrix[i++] = s.toCharArray();
    }
    return theMatrix;
}

问题

I'm having trouble with this, maybe you could help me: I have 3 strings like: word1, word2, word3 and I have to build a matrix with them, like this: on the first row : word1("ABC"), second row: word2("DEF") and third row: word3("GHI").

A|B|C
D|E|F
G|H|I

I need this because after that I have to check if the formed words ("ADG","BEH","CFI") are in an array of words. And I don't know how to put those strings in the matrix so I can check. Any help is useful. Thanks

解决方法

Based on this comment:

the words have the same size, because the matrix is actually like a puzzle. I choose randomly 3 words from an array, put them in a matrix and check after that if the words resulted are from the same array.

I'll assume some things in order to make this work (since we don't have enough info):

  1. You have an array of Strings where you have all the words

    private String[] words;
    
  2. You have a method to randomly pick up 3 Strings from this array.

    private String s1, s2, s3;
    
    public void pickThreeRandomWords() {
        s1 = aRandomWord(words);
        s2 = aRandomWord(words);
        s3 = aRandomWord(words);
        //or maybe another fancy algorithm to get this...
    }
    

So you would need an array of array of chars based on these 3 Strings. This code could do the work for you:

public char[][] createMatrixFromStrings(String s1, String s2, String s3) {
    char[][] theMatrix = new char[3][]; //yes, hardcoded
    theMatrix[0] = s1.toCharArray();
    theMatrix[1] = s2.toCharArray();
    theMatrix[2] = s3.toCharArray();
    return theMatrix;
}

Of course, if you would want to make this method to support more than 3 Strings you can make the method to receive a random quantity of Strings:

public char[][] createMatrixFromStrings(String ... strings) {
    if (strings == null || strings.length == 0) return null;
    char[][] theMatrix = new char[strings.length][];
    int i = 0;
    for(String s : strings) {
        theMatrix[i++] = s.toCharArray();
    }
    return theMatrix;
}
相似信息