如何按字母顺序重新排列二维数组矩阵中的列

时间:2015-04-08 00:26:15

标签: java arrays sorting matrix multidimensional-array

我想采用下面的矩阵并将字符TOAD重新排列为ADOT并将下面的相应列重新排列到上面移动的字符的位置,例如A移动到col 0所以现在VGD应该都在col 0等

TOAD是一个独立的阵列!我正在使用该关键字按字母顺序对martrix进行排序。

//Matrix output and then the code.
        T O A D 
        V V V X 
        D V G G 
        D F D V 


public String printMatrix(String s [][]){
        char key[] = polyCipher.getKeyword().toCharArray();
        String keyOut = "";
        for(int i=0;i<key.length;i++){
            keyOut += key[i] + " ";
        }
        keyOut += "\n";
        for (int i = 0; i < s.length; i++) {
       //     keyOut += PHRASE_KEY[i] + " ";
           for (int j = 0; j < s[i].length; j++) {
                keyOut += s[i][j] + " ";
            }
            keyOut += "\n";
        }
        return keyOut.toUpperCase();
    }

    public static String [][] buildMatrix (String translation, String outermarker, String innermarker) {
        // outerdelim may be a group of characters
        String [] sOuter = translation.split("[" + outermarker + "]"); 
        int size = sOuter.length;
        // one dimension of the array has to be known on declaration:
        String [][] result = new String [size][size]; 
        int count = 0;
        for (String line : sOuter)
        {
            result [count] = line.split (innermarker);
            ++count;
        }
        return result;
    }

    public void sortArray(){
        // do tomorrow
    }

    public String matrixFormatter(String x){

         String resultstr = "";
          int i = 0;
          while(i < x.length()) {
            // If end of string: only add character.
            if (i == x.length() - 1) {
              resultstr += x.substring(i, i + 1);
            } else {
              if ( ((i + 1) % 4) == 0) {
                resultstr += x.substring(i, i + 1)  + "|";
              } else {
                resultstr += x.substring(i, i + 1)  + ",";
              }
            }
            i++;
          }
          return resultstr;

    }

}

0 个答案:

没有答案