有没有比使用嵌套 for 循环“更快”的方法来迭代二维数组?

Kev*_*and 3 java arrays for-loop

我编写了一个程序,用于加密和解密使用四方密码读取的文件。目前,我将文件存储到字符数组中,将其传递到将其分解为二元组的函数中,并使用另一个函数中的嵌套 for 循环以这种方式对其进行加密。此时,我基本上正在尝试优化运行时间。是否有另一种方法可以迭代我正在使用的二维数组,该方法不使用两个 for 循环,或者最多只使用一个 for 循环?下面是相关代码:

文件处理程序.java

   import java.io.*;
    import java.util.*;

    public class FileHandler {
    private List<String> characters = new ArrayList<String>();
    public char fileToChar[];

    public void preEncryptionFile(String fileText) throws IOException {
        String line;

        FileInputStream fileReader = new FileInputStream(fileText);
        DataInputStream dataInputStream = new DataInputStream(fileReader);
        BufferedReader bufferedReader = 
               new BufferedReader(new InputStreamReader(dataInputStream));

        while ((line = bufferedReader.readLine()) != null) {
            characters.add(line);
        }

        String charsToString = characters.toString();

        charsToString = charsToString.replaceAll("[^a-zA-Z]", "").toUpperCase();

        fileToChar = charsToString.toCharArray();

        bufferedReader.close();
    }
}
Run Code Online (Sandbox Code Playgroud)

FourSquareCipher.java

    import java.util.*;

    public class FourSquareCipher {
        List<Character> encryptionList = new ArrayList<Character>();
        List<Character> decryptionList = new ArrayList<Character>();

        private char[][] matrix = { 
                { 'A', 'B', 'C', 'D', 'E', 'Z', 'G', 'P', 'T', 'F' },
                { 'F', 'G', 'H', 'I', 'K', 'O', 'I', 'H', 'M', 'U' }, 
                { 'L', 'M', 'N', 'O', 'P', 'W', 'D', 'R', 'C', 'N' },
                { 'Q', 'R', 'S', 'T', 'U', 'Y', 'K', 'E', 'Q', 'A' }, 
                { 'V', 'W', 'X', 'Y', 'Z', 'X', 'V', 'S', 'B', 'L' },
                { 'M', 'F', 'N', 'B', 'D', 'A', 'B', 'C', 'D', 'E' }, 
                { 'C', 'R', 'H', 'S', 'A', 'F', 'G', 'H', 'I', 'K' },
                { 'X', 'Y', 'O', 'G', 'V', 'L', 'M', 'N', 'O', 'P' }, 
                { 'I', 'T', 'U', 'E', 'W', 'Q', 'R', 'S', 'T', 'U' },
                { 'L', 'Q', 'Z', 'K', 'P', 'V', 'W', 'X', 'Y', 'Z' } };

        public void encryptionBigram(char[] fileToText) {
            int i;
            char x, y;

            for (i = 0; i < fileToText.length - 1; i += 2) {
                x = fileToText[i];
                y = fileToText[i + 1];

                encryption(x, y);
            }
        }

        private void encryption(char x, char y) {
            int i, j;
            int a, b, c, d;

            a = b = c = d = 0;

            for (i = 0; i < 5; i++) {
                for (j = 0; j < 5; j++) {
                    if (x == matrix[i][j]) {
                        a = i;
                        b = j;
                    }
                }
            }

            for (i = 5; i < 10; i++) {
                for (j = 5; j < 10; j++) {
                    if (y == matrix[i][j]) {
                        c = i;
                        d = j;
                    }
                }
            }

            encryptionList.add(matrix[a][d]);
            encryptionList.add(matrix[c][b]);
        }
}
Run Code Online (Sandbox Code Playgroud)

Vin*_*igh 5

使用单个循环迭代二维数组。

更具体地说,此代码根据当前索引和子数组的宽度计算行和列。

仅当子数组具有固定长度/宽度时,这才有效。

我还没有对此进行基准测试,所以我不能确定它是否比您目前拥有的更有效。检查第二个条件和增加第二个变量的开销可能与计算行和列相同(甚至更少)。

尽管如此:

char[][] letters = {
    {'A', 'B', 'C'},
    {'D', 'E', 'F'},
    {'G', 'H', 'I'}
};

int width = 3;
int maxIndex = letters.length * width;
for(int i = 0; i < maxIndex; i++) {
    int row = i / width; // determines row
    int column = i % width; // determines column

    System.out.println("Value["+letters[row][column]+"] Row["+row+"] Column["+column+"]");
}
Run Code Online (Sandbox Code Playgroud)

通过将索引(由 表示i)除以宽度来确定行。由于上例中子数组的宽度为3

  • 如果索引为6,则行为 2。
  • 如果索引为9,则行为 3。
  • 如果索引为11,则该行为 3.66(仍然是 3)

该列由模运算确定。由于上例中的每个子数组的宽度为3

  • 如果索引为6,则该列为 0。
  • 如果索引为9,则该列为 0。
  • 如果索引为11,则列为 2。