简体   繁体   English

用Java方法打印数组

[英]Printing an array in Java method

So I need to print an array of integers into a String, and I have it almost right with one problem 所以我需要将一个整数数组打印成一个字符串,并且我几乎可以解决一个问题

public static String arrayToString(int[] anArray) {
    String x = "";
    String y = "";
    String result = "";
    for (int i = 0; i < anArray.length; i++) {
        y = Integer.toString(anArray[i]);
        x = x + ", " + y;
    }
    result = "[" + x + "]";
    return result;
}

public static void main(String[] args) {
    int arrayInt[] = new int[] { 80, 100, 80, 92, 95, 87, 82, 76, 45, 76, 80, 70};
    System.out.println("array : " + arrayToString(arrayInt));
}

When I execute the code, instead of printing: 当我执行代码时,而不是打印:

[80, 100, 80, 92, 95, 87, 82, 76, 45, 76, 80, 70] [80,100,80,92,95,87,82,76,45,76,80,70]

I get: 我得到:

[, 80, 100, 80, 92, 95, 87, 82, 76, 45, 76, 80, 70] [,80,100,80,92,95,87,82,76,45,76,80,70]

This should be really simple to me but I'm stuck, where do I need to put an exception to remove it? 这对我来说真的很简单,但是我被困住了,我需要在哪里放置异常以将其删除?

You are just adding an extra , in the beginning. 你只是增加一个额外的,在开始的时候。 One simple way to get rid of it is, 摆脱它的一种简单方法是
Replace 更换

result = "[" + x + "]";

with

result = "[" + x.substring(1) + "]";

But the best solution would be to use Arrays.toString . 但是最好的解决方案是使用Arrays.toString See this for more details. 请参阅了解更多详情。

The problem is that you are getting an extra comma , at the beginning. 问题是,你得到一个额外的逗号 ,开头。 This will solve your problem in the arrayToString function: 这将解决arrayToString函数中的问题:

public static String arrayToString(int[] anArray) {
    String x = "";
    String y = "";
    String result = "";
    for (int i = 0; i < anArray.length-1; i++) {
        y = Integer.toString(anArray[i]);
        x = x + y + ",";
    }
    y = Integer.toString(anArray[anArray.length-1]);
    x = x + y;
    result = "[" + x + "]";
    return result;
}
 public static String arrayToString(int[] anArray) {
    String x = "";
    String y = "";
    String result = "";
    for (int i = 0; i < anArray.length; i++) {
        y = Integer.toString(anArray[i]);
        if(x.isEmpty()){
            x = y;
        } else {
            x = x + ", " + y;
        }
    }
    result = "[" + x + "]";
    return result;
}

public static void main(String[] args) {
    int arrayInt[] = new int[] { 80, 100, 80, 92, 95, 87, 82, 76, 45, 76, 80, 70 };
    System.out.println("array : " + arrayToString(arrayInt));
}

您可以包括检查第一个元素。

if( i == 0 ) x = y; else x = x + ", " + y;

Please change the last line in your code, result string variable as below: 请更改代码的最后一行,结果字符串变量如下:

public static String arrayToString(int[] anArray) {
String x = "";
String y = "";
String result = "";
for (int i = 0; i < anArray.length; i++) {
    y = Integer.toString(anArray[i]);
    x = x + ", " + y;
}
 result = "[" + x + "]";
 return result.charAt(0)+result.substring(3);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM