我想采用下面的矩阵并将字符TOAD重新排列为ADOT并将下面的相应列重新排列到上面移动的字符的位置,例如A移动到col 0所以现在VGD应该都在col 0等
TOAD是一个独立的阵列!我正在使用该关键字按字母顺序对martrix进行排序。
T O A D
V V V X
D V G G
D F D V
我已经走到了这一步,基本上我想知道的是,如果A移动到开始我如何让下面的整个列随之移动?
public class CipherKey {
Scanner write = new Scanner (System.in);
Matrix polyCipher;
private final char[] PHRASE_KEY = { 'J', 'A', 'V', 'A'};
String[][] matrixStore = new String[PHRASE_KEY.length][PHRASE_KEY.length];
String outmrk = "|";
String inmrk = ",";
CipherKey(Matrix m) {
polyCipher = m;
}
public void assembleMatrix() {
String s = matrixFormatter(polyCipher.getTranslation());
matrixStore = buildMatrix(s,outmrk,inmrk);
System.out.print(printMatrix(matrixStore));
System.out.print(sortMatrix(matrixStore));
}
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 String sortMatrix(String a [][]){
System.out.println("\nAlphabetically rearranged: ");
char[] key = polyCipher.getKeyword().toCharArray();
char[] alphaKey = polyCipher.getKeyword().toCharArray();
String alphaOut = "";
for(int i=0;i<alphaKey.length;i++){
alphaOut += alphaKey[i] + " ";
}
char[][] x = new char[4][36];
x[0] = new char[]{key[0], key[1], key[2], key[3]};
x[1] = new char[]{'V', 'V', 'V', 'X'};
x[2] = new char[]{'D', 'V', 'G', 'G'};
x[3] = new char[]{'D', 'F', 'D', 'V'};
String [] strArray = new String[x[0].length];
for (int loop1 = 0; loop1 < x.length; loop1++){
for (int loop2 = 0; loop2 < x[0].length; loop2++){
if(strArray[loop2] == null){
strArray[loop2] = "";
}
strArray[loop2] += x[loop1][loop2];
}
}
Arrays.sort(strArray);
alphaOut += "\n";
for (int i = 0; i < strArray.length; i++) {
// keyOut += PHRASE_KEY[i] + " ";
for (int j = 0; j < s[i].length; j++) {
alphaOut += s[i][j] + " ";
}
alphaOut += "\n";
}
alphaOut.toUpperCase();
}
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 :(得分:1)
为此,您必须跟踪移动并相应地移动其他元素。但是看看你的程序,我可以看到你依靠java API进行排序。
如果你想继续这样做,我可以给你一个适合你的小技巧。
第1步:定义数组:
char[][] x = new char[4][4];
x[0] = new char[]{'T', 'O', 'A', 'D'};
x[1] = new char[]{'V', 'V', 'V', 'X'};
x[2] = new char[]{'D', 'V', 'G', 'G'};
x[3] = new char[]{'D', 'F', 'D', 'V'};
步骤2:将其转换为垂直连接字符的单维字符串数组。
String [] strArray = new String[x[0].length];
for (int loop1 = 0; loop1 < x.length; loop1++){
for (int loop2 = 0; loop2 < x[0].length; loop2++){
if(strArray[loop2] == null){
strArray[loop2] = "";
}
strArray[loop2] += x[loop1][loop2];
}
}
第3步:对字符串数组进行排序
Arrays.sort(strArray);
第4步:将其转换回char数组
for (int loop1 = 0; loop1 < strArray.length; loop1++){
for (int loop2 = 0; loop2 < strArray[loop1].length(); loop2++){
x[loop2][loop1] = strArray[loop1].charAt(loop2);
}
}
这应该可以解决问题。
修改强> 假设您必须根据char [] alpha的位置切换String a [] []的位置,您必须将其转换为char [] []数组,就像我在下面所做的那样:
char[] alpha = {'T', 'O', 'A', 'D'};
String a [][] = {{"V", "V", "V", "X"},{"D", "V", "G", "G"},{"D", "F", "D", "V"}};
char[][] x = new char[a.length+1][];
x[0] = alpha;
for(int loop1 = 0; loop1 < a.length; loop1++){
x[loop1+1] = new char[a[loop1].length];
for (int loop2 = 0; loop2 < a[loop1].length; loop2++){
x[loop1+1][loop2] = a[loop1][loop2].charAt(0);
}
}
这会将您的两个数组转换为一个二维数组,您可以在其上运行其余步骤:
char[][] x = new char[4][4];
x[0] = new char[]{'T', 'O', 'A', 'D'};
x[1] = new char[]{'V', 'V', 'V', 'X'};
x[2] = new char[]{'D', 'V', 'G', 'G'};
x[3] = new char[]{'D', 'F', 'D', 'V'};
答案 1 :(得分:0)
由于矩阵是数组(列)的数组(行),因此最好首先转置数据。然后可以有效地完成切换行(较早的列):
void switchRows(String[][] a, int i, int j) {
String[] temp = a[i];
a[i] = a[j];
a[j] = temp;
}
转置意味着a [i] [j]应该被填充为旧a [j] [i]。
也许您可以考虑使用char
代替String
。
答案 2 :(得分:0)
转置数据,使上面显示的行成为列,反之亦然。由于二维数组是一个数组数组,因此您希望每个子数组包含一列中的所有字符。然后你可以编写一个函数,用每个子数组的第一个索引的内容对子数组进行排序。
char[][] unsorted = new char[4][4];
unsorted[0] = new char[]{'T', 'V', 'D', 'D'};
unsorted[1] = new char[]{'O', 'V', 'V', 'F'};
unsorted[2] = new char[]{'A', 'V', 'G', 'D'};
unsorted[3] = new char[]{'D', 'X', 'G', 'V'};
// Now sort unsorted[0] through unsorted[3] by the entries at unsorted[i][0]
您可以编写一个简单的Comparator类,并使用public static <T> void sort(T[] a, Comparator<? super T> c)
中的java.util.Arrays
方法。或者写一个简单的插入排序。