Click here to Skip to main content
11,646,838 members (73,351 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: Java array
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 20-Apr-13 16:24pm
ThePhantomUpvoter at 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

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.

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

  Print Answers RSS
0 510
1 Sergey Alexandrovich Kryukov 478
2 OriginalGriff 428
3 CPallini 334
4 F-ES Sitecore 309
0 OriginalGriff 1,253
1 994
2 DamithSL 896
3 Sergey Alexandrovich Kryukov 833
4 CPallini 720

Advertise | Privacy | Mobile
Web02 | 2.8.150731.1 | Last Updated 22 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100