以图表形式打印2D int数组(Java) [英] Printing a 2D int array in chart form (Java)
问题描述
对于我的计算机科学实验室,我必须创建一个返回字符串的方法:
public static String arrayToString(int [] [] a)
a是一个二维整数数组。字符串必须用空格分隔每个索引,并在每行(\ n)之后分隔一个新行。行在行的最后一个值之前或之后不能有空格,并且列在最后一列之前或之后不能有新行。我怎样才能做到这一点?我知道它涉及一个嵌套的for循环,但我不知道如何编码它。在此先感谢!
这是一个非常基本的问题。无论你在哪里学习,都应该得到现场人员的帮助。
你显然需要2个嵌套循环(1个用于数组行,1个用于列)。你想输出每个值,在每个元素之前或之后提供一个空格(建议:检测行中的第一个元素而不打印空格),并在每行的末尾添加一个新行。
For my Computer Science lab, I have to make a method that returns a string:
public static String arrayToString(int[] [] a)
a is a two dimensional integer array. The string has to separate each index with a space, and a new line after every row (\n). The rows can''t have a space before or after the last value on the row, and the columns can''t have a new line before or after the last column. How can I do this? I know it involves a nested for loop but I can''t figure out how to code it. Thanks in advance!
This is a pretty basic problem. You should get some help from a live person wherever you are a student.
You obviously need 2 nested loops (1 for array row, 1 for column). You want to output each value, providing a space either before or after each element (suggestion: detect the first element in the row and don''t print a space), and a new line at the end of each row.
这篇关于以图表形式打印2D int数组(Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!