Click here to Skip to main content
15,886,063 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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!
Posted
Comments
[no name] 21-Apr-13 5:36am    
"how to code it", how to code what? A for loop? A nested for loop? Or you just need the whole thing written for you?

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900