意外结果:字符串旁边是空值

huangapple go评论120阅读模式
英文:

unexpected results: null next to the string

问题

import java.util.*;

public class Main {

    public static void main(String[] args) {

        Scanner scan1 = new Scanner(System.in);
        int test = scan1.nextInt();
        String[] sArr = new String[test];
        sArr[0] = "";

        int y = 0;

        while (test > 0) {
            char c;
            test--;

            Scanner scan2 = new Scanner(System.in);
            Scanner scan3 = new Scanner(System.in);

            int n = scan2.nextInt();
            String str = scan3.nextLine();
            String[] nSplit = str.split("(?<=\\G.)");

            int[] x = new int[n];

            for (int i = 0; i < n * 4 - 3; i += 4) {
                nSplit[i] += nSplit[i + 2];
                nSplit[i + 1] += nSplit[i + 3];
            }

            for (int i = 0, j = 0; i < x.length; i++, j += 4) {
                x[i] = Integer.parseInt(nSplit[j]) + Integer.parseInt(nSplit[j + 1]);
                c = (char) x[i];
                sArr[test] += c;
            }
        }

        for (int i = sArr.length-1; i > -1; i--) {
            System.out.println(sArr[i]);
        }
    }
}

输入:

5

1

1234

1

2345

1

3456

1

4567

1

5678

输出:

%null

;null

Qnull

gnull

}

//end

这些 null 是从哪里来的?如何去除它们?为什么最后一个元素旁边没有 null?

如果 null 表示所选数组索引处的空白,在这种情况下它是什么意思?

英文:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan1 = new Scanner(System.in);
int test = scan1.nextInt();
String[] sArr = new String[test];
sArr[0] = &quot;&quot;;
// String s = &quot;&quot;;
//  StringBuilder sB = new StringBuilder(s);
int y = 0;
while (test &gt; 0) {
char c;
test--;
Scanner scan2 = new Scanner(System.in);
Scanner scan3 = new Scanner(System.in);
int n = scan2.nextInt();
String str = scan3.nextLine();
String[] nSplit = str.split(&quot;(?&lt;=\\G.)&quot;);
int[] x = new int[n];
for (int i = 0; i &lt; n * 4 - 3; i += 4) {
nSplit[i] += nSplit[i + 2];
nSplit[i + 1] += nSplit[i + 3];
}
for (int i = 0, j = 0; i &lt; x.length; i++, j += 4) {
x[i] = Integer.parseInt(nSplit[j]) + Integer.parseInt(nSplit[j + 1]);
c = (char) x[i];
sArr[test] += c;
}
}
for (int i = sArr.length-1; i &gt;-1; i--) {
System.out.println(sArr[i]);
}
}
}

input:

5

1

1234

1

2345

1

3456

1

4567

1

5678

output:

null%

null;

nullQ

nullg

}
//end

where did these null come from? how to get rid of it?
why is there not null next to the last element?

if null means a blank at the selected array index then what it means in this case?

答案1

得分: 0

基本上正在发生的是您正在创建一个字符串数组。
您只初始化数组的第一个元素为""。
sArr[0] = "";

这就是为什么除了第一个元素之外的每个元素都有一个空值在其前面。

所以这个语句sArr[test] += c;会为数组中未初始化的所有字符串添加一个空值。

我建议您不要使用sArr[0] = "";,而是可以这样做
Arrays.fill(sArr, "");
它会将数组中所有字符串的值都初始化为""。

Arrays是一个在您导入的utils类下找到的类。

英文:

Basically what is happening is you are creating an array of Strings.
And you are only initialising the first element of the array to ""
sArr[0] = "";

That is why every other element but the first element has a null preceding it.

So this statement sArr[test] += c; is going to add a null value for all non initialised strings in your array of Strings.

I would suggest instead of sArr[0] = ""; you can do this
Arrays.fill(sArr , "")
It initialises all values of the strings in the array to ""

Arrays is a class found under utils which you are importing.

huangapple
  • 本文由 发表于 2020年8月16日 02:31:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63429554.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定