Java 如何逐行访问多维数据?

Java 如何逐行访问多维数据?,java,arrays,multidimensional-array,Java,Arrays,Multidimensional Array,我试图在java中逐行访问多维数据,但无法实现 我有这段代码,但它逐列打印出数组: for(int i = 0; i<array.length; i++) { for(int j = 0; j<array[i].length; j++) { System.out.print(array[i][j]); } } 我怎样才能用这种方式打印出来 adg beh cfi 完整代码: import java.util.Scanner; public cla

我试图在java中逐行访问多维数据,但无法实现

我有这段代码,但它逐列打印出数组:

for(int i = 0; i<array.length; i++) {
    for(int j = 0; j<array[i].length; j++) {
        System.out.print(array[i][j]);
    }
}
我怎样才能用这种方式打印出来

adg
beh
cfi
完整代码:

import java.util.Scanner;

public class forcabruta {
    public static void main (String[] args) {
        Scanner keyboard = new Scanner(System.in);
        char[] words = new char[] {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', ' '};
        String text;
        System.out.print("Enter a text to decode: ");
        text = keyboard.nextLine();
        char[][] combinations = new char[text.length()][words.length];
        for(int i = 0; i<text.length(); i++) {
            for(int j = 0; j<words.length; j++) {
                if(words[j] == text.charAt(i)) {
                    for(int k = 1; k<words.length; k++) {
                        combinations[i][k] = words[Math.floorMod(j-k, 27)];
                    }

                }
            }
        }
        for(int i = 0; i<combinations.length; i++) {
            for(int j = 0; j<combinations[i].length; j++) {
                System.out.print(combinations[j][i]);
            }
        }
    }
}
import java.util.Scanner;
卡布鲁塔公务舱{
公共静态void main(字符串[]args){
扫描仪键盘=新扫描仪(System.in);
字符[]单词=新字符[]{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',''};
字符串文本;
System.out.print(“输入要解码的文本:”);
text=键盘.nextLine();
字符[][]组合=新字符[text.length()][words.length];

对于(int i=0;i您几乎做到了。只需替换:

System.out.print(array[i][j]);
为此:

System.out.print(array[j][i]);

UPD 1:
因此,您的代码变成:

String[][] array = {{"a", "b", "c"}, {"d", "e", "f"}, {"g", "h", "i"}};
for(int i = 0; i < array.length; i++) {
    for(int j = 0; j < array[i].length; j++) {
        System.out.print(array[j][i]);
    }
    System.out.println();
}
String[][]数组={{{“a”、“b”、“c”}、{“d”、“e”、“f”}、{“g”、“h”、“i”};
for(int i=0;i
小心切换索引! 例如,这将抛出一个错误

String[][] array = {{"a", "b", "c"}, {"d", "e", "f"}, {"g", "h", "i"}, {"j", "k"}};

for(int i = 0; i<array.length; i++) {
    for(int j = 0; j<array[i].length; j++) {
       System.out.print(array[j][i]);
    }
    System.out.println();
}
String[][]数组={{{“a”、“b”、“c”}、{“d”、“e”、“f”}、{“g”、“h”、“i”}、{“j”、“k”};
for(int i=0;i maxColCount){
maxColCount=数组[j]。长度;
}
}
System.out.println();
}而(i
最基本的想法是,你不知道你有多少列,你必须在工作中自己找到它。但是你还必须在每次访问它之前检查当前行是否有这么多列

如果列的数量始终相同,这就足够了。
String[][]数组={{{“a”、“b”、“c”}、{“d”、“e”、“f”}、{“g”、“h”、“i”};
对于(int i=0;i
您必须确保不会因不混合行和列索引而引发此问题。 必须在列索引上启动循环,然后将rowIndex循环嵌套到该列索引循环中。 下面是它的工作原理

String[][] array = {{"a", "b", "c"}, {"d", "e", "f"}, {"g", "h", "i"}, {"j", "k", "l"}};
    for(int colIndex= 0; colIndex < array[0].length; colIndex++){
        for(int rowIndex = 0; rowIndex < array.length; rowIndex++){
            System.out.print(array[rowIndex][colIndex]);
        }
    }
String[][]数组={{{“a”、“b”、“c”}、{“d”、“e”、“f”}、{“g”、“h”、“i”}、{“j”、“k”、“l”};
对于(int-colIndex=0;colIndex
内部数组是否总是具有相同的长度(在您的示例中为三个)?如果没有,那么如果您具有,例如
[“a”]、[“b”、“c”]、[“d”、“e”、“f”]]
?不,列的长度总是相同的,但行的长度却不相同。因此,例如,有时我会有
[“a”、“b”、“c”]、[“d”、“e”、“f”]、[“g”、“h”,“i”]
[[“a”、“b”、“c”],[“d”、“e”、“f”]
[“a”、“b”、“c”]
我已经尝试过这样做,但我得到了这个错误:线程“main”中出现异常“java.lang.ArrayIndexOutOfBoundsException:索引3超出长度3请发布导致该异常的完整代码。谢谢!但它始终具有相同的列长度,只具有不同的行长度。因此,例如,有时我会使用
[[“a”、“b”、“c”]、[“d”、“e”、“f”]、[“g”、“h”、“I”]]
[[“a”、“b”、“c”],[“d”、“e”、“f”]
[[“a”、“b”、“c”]
@AdriánT95我现在修好了,你能检查一下吗?
String[][] array = {{"a", "b", "c"}, {"d", "e", "f"}, {"g", "h", "i"}, {"j", "k"}};

int maxColCount = 0;
int i = -1;
do {
    i++;
    for(int j = 0; j < array.length; j++) {
       if (array[j].length > i) {
          System.out.print(array[j][i]);
       }
       if (array[j].length > maxColCount) {
          maxColCount = array[j].length;
       }
    }
    System.out.println();
} while (i < maxColCount);
String[][] array = {{"a", "b", "c"}, {"d", "e", "f"}, {"g", "h", "i"}};
for(int i = 0; i < array[0].length; i++) {
    for(int j = 0; j < array.length; j++) {
        System.out.print(array[j][i]);
    }
    System.out.println();
}
String[][] array = {{"a", "b", "c"}, {"d", "e", "f"}, {"g", "h", "i"}, {"j", "k", "l"}};
    for(int colIndex= 0; colIndex < array[0].length; colIndex++){
        for(int rowIndex = 0; rowIndex < array.length; rowIndex++){
            System.out.print(array[rowIndex][colIndex]);
        }
    }