I am trying to print a string of the multi arrays but it's says "The primitive type.." Here is the code:
int[][] nums={ {1,2,3} , {4,5,6} };
String result = "" ;
for (int row =0; row < nums.length; row++) { // iterate rows
for (int col=0; col < nums[row].length; col++) { // iterate col of each row
result = result + nums[row,col] ; // add value and TAB
}
result = result + "/t" ; // add NEW LINE for each row
}
result = result + "/n" ; }
I don't understand why it says that row does not have field col.
Thanks To All of you,
You have to replace nums[row,col]
on nums[row][col]
public class Main {
public static void main(String[] args) {
int[][] nums = {{1,2,3}, {4,5,6}};
String result = "";
for (int row = 0; row < nums.length; row++) { // iterate rows
for (int col=0; col < nums[row].length; col++) { // iterate col of each row
result = result + nums[row][col] ;
}
result = result + "/t"; // add value and TAB
}
result = result + "/n" ; // add NEW LINE for each row
System.out.println(result);
}
}
123/t456/t/n
PS: /t
and /n
you can replace on \t
(tab) and '\n' (new line)
and also move some lines to the loop:
public class Main {
public static void main(String[] args) {
int[][] nums = {{1,2,3}, {4,5,6}};
String result = "";
for (int row = 0; row < nums.length; row++) { // iterate rows
for (int col=0; col < nums[row].length; col++) { // iterate col of each row
result = result + nums[row][col] + "\t"; // add value and TAB
}
result = result + "\n" ; // add NEW LINE for each row
}
System.out.println(result);
}
}
1 2 3
4 5 6
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.