gpt4 book ai didi

java - 我需要像表格一样打印我的数组

转载 作者:行者123 更新时间:2023-11-30 06:04:47 25 4
gpt4 key购买 nike

我需要打印一个带有字符串值的二维数组,例如表格。

到目前为止我的代码是:

import java.util.Arrays;

public class Employee {
public static void main(String[] args) {
String wages[][] =
{
{"Emp", "Hours", "Wages"},
{"Bobby", "45", "35"},
{"Rick", "15", "33"},
{"Mike", "66", "50"},
{"Jayme", "15", "45"}
};

System.out.print(Arrays.deepToString(wages));
System.out.println();
}
}

输出为:

[[Emp, Hours, Wages], [Bobby, 45, 35], [Rick, 15, 33], [Mike, 66, 50], [Jayme, 15, 45]]

我需要它看起来更像一个具有单独行的表格(5行3列)

最佳答案

您必须迭代数组并打印所有条目,如下所示。

String wages[][] = {
{"Emp", "Hours", "Wages"},
{"Bobby", "45", "35"},
{"Rick", "15", "33"},
{"Mike", "66", "50"},
{"Jayme", "15", "45"}
};

Arrays.stream(wages).map(Arrays::toString).forEach(System.out::println);

输出

[Emp, Hours, Wages]
[Bobby, 45, 35]
[Rick, 15, 33]
[Mike, 66, 50]
[Jayme, 15, 45]

Try it online !

关于java - 我需要像表格一样打印我的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48567097/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com