Java-如何将For Loop与(多维)字符串数组一起使用 [英] Java- How to use For Loop with (Multidimensional) Array of Strings

查看:86
本文介绍了Java-如何将For Loop与(多维)字符串数组一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用int多维数组进行for循环,但是无法使用多维数组重现它。

I was able to use for loop with int multidimensional arrays but I'm unable to reproduce it with multi arrays.

public class array {

public static void main(String[] args) {
    String[][] words = new String[2][3];
    words[0][0] = "a";
    words[0][1] = "b";
    words[0][2] = "c";
    words[1][0] = "d";
    words[1][1] = "e";
    words[1][2] = "f";
   }
}

很乐意为您提供一些迭代方法的帮助

Would love some help on how to iterate that

作为参考,这就是我对int所做的

For reference, this was what I did for int

int[][] multi = {
        {3, 4, 5},
        {2, 3, 5, 6, 7},
        {112, 3}
    };
    for (int row = 0; row < multi.length; row++) {
        for (int col = 0; col < multi[row].length; col++) {
            System.out.print(multi[row][col] + " ");


推荐答案

您快到了,改用for循环,不要忘记每一行也是一个数组.....

You are almost there, adapt the for loops, dont forget every row is an array as well.....

    String[][] words = new String[2][3];
    words[0][0] = "a";
    words[0][1] = "b";
    words[0][2] = "c";
    words[1][0] = "d";
    words[1][1] = "e";
    words[1][2] = "f";
    for (int row = 0; row < words.length; row++) {
        for (int col = 0; col < words[row].length; col++) {
            System.out.println(words[row][col]);
        }
    }

这篇关于Java-如何将For Loop与(多维)字符串数组一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆