我是java数组的新手。如果我想显示这个:
0 ....
1 ....
2 ....
3 ....
4 ....
地点:
....
....
....
....
.... is a 2-d char array called char[][] squares.
如何在平方数组前添加数字?
如果我选了一个数字,比如说3,我想在3旁边加上">",我该怎么做?因此,我想要的是:
0 ....
1 ....
2 ....
3>....
4 ....
try this
private void print_array(char[][] squares, int selectedIndex){
for(int i =0;i<squares.length;i++){
System.out.print(i);
if(i == selectedIndex){
System.out.print(">");
}
for(int j = 0;j<squares[i].length;j++){
System.out.print(squares[i][j]);
}
System.out.println();
}
}
我不确定这类问题是否被允许,但这里是答案。让我们假设你有一个字符[m][n]数组。你想在x行
前打印>for(int i = 0; i < m; i++)
{
String s = i+"";
if(i == x) s = s + ">";
else s = s+ " ";
s = s + new String(chars[i])
System.out.println(s);
}
你的问题相当隐晦,但我还是试试吧:
class Whatever {
public static void Main(String[] args) {
char[][] squares = new char[5][2];
/*
Here goes your code to assign values to the array
*/
for(int i:=0, i<5; ++i) {
System.out.println(i);
if (i==3) {
System.out.print("> ");
} else {
System.out.print(" ")
}
for(j:=0; j<2; ++j) {
System.out.println(aquares[i][j]);
}
}
}
for(int y = 0; y< array.lenght;y++) {
for(int x = 0; x< array[y].lenght;x++) {
System.out.println(array[y][x]);
}
System.out.println();
}