将 s1 的值复制到 s2 的元素中。

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

What am i doing wrong. copy the the values of s1 into the element von s2

问题

我试图将变量s1的值复制到s2的元素中,但是我得到了null。我做错了什么?以下是我的代码:

  1. public class Main {
  2. public static void main(String[] args) {
  3. String[] s1 = new String[10];
  4. String[] s2 = new String[10];
  5. String[] s3 = new String[10];
  6. for (int i = 0; i < s1.length; i++){
  7. s2[i] = s1[i];
  8. System.out.println("s2[" + i + "] : " + s2[i]);
  9. }
  10. }
  11. }

-------- 输出 -------

  1. s2[0] : null
  2. s2[1] : null
  3. s2[2] : null
  4. s2[3] : null
  5. s2[4] : null
  6. s2[5] : null
  7. s2[6] : null
  8. s2[7] : null
  9. s2[8] : null
  10. s2[9] : null
英文:

I am trying to copy the values of the variable s1 into the element of s2 and I am getting null. What am I doing wrong. Here are my Code:

  1. public class Main {
  2. public static void main(String[] args) {
  3. String[] s1 = new String[10];
  4. String[] s2 = new String[10];
  5. String[] s3 = new String[10];
  6. for (int i = 0; i &lt; s1.length; i++){
  7. s2[i] = s1[i];
  8. System.out.println(&quot;s2[&quot; + i + &quot;] : &quot; + s2[i]);
  9. }
  10. }
  11. }

-------- output -------

  1. s2[0] : null
  2. s2[1] : null
  3. s2[2] : null
  4. s2[3] : null
  5. s2[4] : null
  6. s2[5] : null
  7. s2[6] : null
  8. s2[7] : null
  9. s2[8] : null
  10. s2[9] : null

答案1

得分: 2

你已经创建了一个空的字符串数组,但是尚未为各个字符串分配任何内容。字符串默认为 null,因此您的输出是完全正常的。

您还可以阅读这篇有趣的帖子:https://stackoverflow.com/questions/5389200/what-is-a-java-strings-default-initial-value

如果您希望看到与 null 不同的任何结果,您必须通过以下方式初始化第一个数组:

  1. String[] s1 = {"第一个字符串", "第二个字符串", ...}

或者(在 for 循环之前,显然):

  1. s1[1] = "第一个字符串";
  2. s1[2] = "第二个字符串";
  3. ...
英文:

You have created an empty array of Strings, but you have not assigned any content to the individual strings. A string defaults to null, so your output is completely normal.

You can also read this interesting post: https://stackoverflow.com/questions/5389200/what-is-a-java-strings-default-initial-value

If you want to see any result different from null, you have to initialize the first array, by using:

  1. String[] s1 = {&quot;First string&quot;, &quot;Second string&quot;, ... }

or (before the for loop, obviously)

  1. s1[1] = &quot;First string&quot;
  2. s1[2] = &quot;Second string&quot;
  3. ...

答案2

得分: 1

以下是您要求的翻译内容:

你错过了初始化s1数组的步骤。默认情况下,它将包含空值,而您正在将这些空值复制到s2中,因此得到了空值。

  1. public class Main {
  2. public static void main(String[] args) {
  3. String[] s1 = {"A", "B", "C", "D"};
  4. String[] s2 = new String[10];
  5. String[] s3 = new String[10];
  6. for (int i = 0; i < s1.length; i++){
  7. s2[i] = s1[i];
  8. System.out.println("s2[" + i + "] : " + s2[i]);
  9. }
  10. }
  11. }
英文:

You missed out to initialise s1 array. By default, it will contains null values which you are copying into s2, hence null values.

  1. public class Main {
  2. public static void main(String[] args) {
  3. String[] s1 = {&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;};
  4. String[] s2 = new String[10];
  5. String[] s3 = new String[10];
  6. for (int i = 0; i &lt; s1.length; i++){
  7. s2[i] = s1[i];
  8. System.out.println(&quot;s2[&quot; + i + &quot;] : &quot; + s2[i]);
  9. }
  10. }
  11. }

huangapple
  • 本文由 发表于 2020年9月22日 15:15:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/64004768.html
匿名

发表评论

匿名网友

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

确定