黑马程序员技术交流社区
标题:
遍历二维数组取值的问题
[打印本页]
作者:
syusikoku
时间:
2014-3-12 09:11
标题:
遍历二维数组取值的问题
int[][] parameters = { { 112, 1, 15, 77, 29 }, { 33, 14, 27, 187 } };
取出每一个二维数组中的值,要打印成这样的效果:{ 112, 1, 15, 77, 29 }, { 33, 14, 27, 187 }
for (int x = 0; x < parameters.length; x++) {
System.out.print("{");
for (int j = 0; j < parameters[x].length; j++) {
if (parameters[x][j] != (parameters[x].length - 1)) {
System.out.print(parameters[x][j] + ",");
}
}
System.out.print("}");
}
现在打印出来的结果是:{112,1,15,77,29,}{33,14,27,187,}请各位高手帮忙指点一二。小弟不胜感激。
作者:
张贺
时间:
2014-3-12 09:38
public class ParamTest
{
public static void main(String[] args)
{
int[][]paramers= {{112,1, 15, 77, 29 }, {33, 14, 27, 187}};
//方式一:
for(int x=0;x<paramers.length;x++)
{
System.out.print("{");
int len=paramers[x].length;
for(int y=0;y<len;y++)
{
if(y!=len-1)
{
System.out.print(paramers[x][y]+",");
}
else
{
System.out.print(paramers[x][y]);
}
}
System.out.print("}");
}
//方式二:
for(int i=0;i<paramers.length;i++)
{
String temp="{";
int len=paramers[i].length;
for(int j=0;j<len;j++)
{
temp+=paramers[i][j]+",";
}
temp=temp.substring(0,temp.length()-1);
temp+="}";
System.out.print(temp);
}
}
}
复制代码
作者:
chen_x
时间:
2014-3-12 09:41
你代码的打印结果应该是:{112,1,15,77,}{33,14,27,},因为每个一维数组的最后一位索引是(parameters[x].length - 1,你只是判断了,没有打印
每个一维数组之间的逗号打印也要处理下,代码如上:
public class Test{
public static void main(String[] args){
int[][] parameters = { { 112, 1, 15, 77, 29 }, { 33, 14, 27, 187 } };
//取出每一个二维数组中的值,要打印成这样的效果:{ 112, 1, 15, 77, 29 }, { 33, 14, 27, 187 }
for (int x = 0; x < parameters.length; x++) {
System.out.print("{");
for (int j = 0; j < parameters[x].length; j++) {
if (j != (parameters[x].length - 1)) {
System.out.print(parameters[x][j] + ",");
}else{
System.out.print(parameters[x][j]);
}
}
if(x!=parameters.length-1){
System.out.print("},");
}else{
System.out.print("}");
}
}
}
}
打印结果是:{112,1,15,77,29},{33,14,27,187}
作者:
Aimer_WJY
时间:
2014-3-12 09:48
我这样做可能有点繁琐了,但是不知道还有没有简单些的
class ArrDemo
{
public static void main(String [] args)
{
int[][] parameters = { { 112, 1, 15, 77, 29 }, { 33, 14, 27, 187 } };
//取出每一个二维数组中的值,要打印成这样的效果:{ 112, 1, 15, 77, 29 }, { 33, 14, 27, 187 }
for (int x = 0; x < parameters.length; x++)
{
System.out.print("{");
for (int j = 0; j < parameters[x].length; j++)
{
if (j == (parameters[x].length - 1))//使用j判断就可以了
System.out.print(parameters[x][j] );
else
System.out.print(parameters[x][j]+"," );
}
if(x==parameters.length-1)
System.out.print("}");
else
System.out.print("},");
}
}
}
作者:
12560zhang
时间:
2014-3-12 10:10
public class Demo {
public static void main(String[] args) {
int[][] para={ { 112, 1, 15, 77, 29 }, { 33, 14, 27, 187 } };
for(int x=0;x<para.length;x++){
System.out.print("{");
for(int y=0;y<para[x].length;y++){
if(y==(para[x].length-1)){
if(x==(para.length-1)){
System.out.print(para[x][y]+"}");
}else{
System.out.print(para[x][y]+"},");
}
}else{
System.out.print(para[x][y]+",");
}
}
}
}
}
复制代码
结果是你想要的
作者:
syusikoku
时间:
2014-3-12 10:19
恩。明白了。谢谢各位大神。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2