转换1-D字符串数组到2-D字符数组 [英] Converting 1-D String array to 2-D char array

查看:144
本文介绍了转换1-D字符串数组到2-D字符数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作,需要我采取从文件一维字符串数组,并把它变成一个二维数组的程序。以数组中从文件工作正常,但我不能得到第二部分的工作。

I'm working on a program that requires me to take in a 1-D String array from a file and turn it into a 2-D array. Taking in the array from the file works fine, but I can't get the second part to work.

在code我工作是:

char[][] array2 = new char [7][5];

for (int i = 0; i < array1.length; i++)
{
    array2[i]= array[i].toCharArray();   
}

for (int i = 0; i < 5; i++)
{
    for (int j = 0; j < 7; j++)
    {
         System.out.println(array2[i][j]);
    }
}

阵列应该在网格格式打印,但是被向下打印。

The array is supposed to print in a grid format, but is printing downward.

任何帮助是AP preciated,谢谢。

Any help is appreciated, thanks.

推荐答案

使用打印而不是的println 在内环和每次循环后打印为空白行的println

Use print instead of println in inner loop and after each loop print a blank line with println.

for (int i = 0; i < 7; i++) // see changes 5/7. You did "new char[7][5]" not [5][7]
{
    for (int j = 0; j < 5; j++) // see changes 7/5
    {
         System.out.print(array2[i][j]);
    }
    System.out.println();
}

更新:

下面是一个转换String数组二维字符数组的程序。

Following is a program that convert String array to 2D char array.

public class StringToChar {
    public static void main(String[] args) {
        String[] strArr = { "HELLO", "WORLD" };
        char[][] char2D = new char[strArr.length][];

        for (int i = 0; i < strArr.length; i++) {
            char2D[i] = strArr[i].toCharArray();
        }

        for (char[] char1D : char2D) {
            for (char c : char1D)
                System.out.print(c + " ");

            System.out.println();
        }
    }
}

这篇关于转换1-D字符串数组到2-D字符数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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