【问题标题】:Java: how to initialize String[]?Java:如何初始化 String[]?
【发布时间】:2011-02-03 14:12:08
【问题描述】:

错误

% javac  StringTest.java 
StringTest.java:4: variable errorSoon might not have been initialized
        errorSoon[0] = "Error, why?";

代码

public class StringTest {
        public static void main(String[] args) {
                String[] errorSoon;
                errorSoon[0] = "Error, why?";
        }
}

【问题讨论】:

标签: java string initialization


【解决方案1】:

你需要initializeerrorSoon,如错误信息所示,你只有declared它。

String[] errorSoon;                   // <--declared statement
String[] errorSoon = new String[100]; // <--initialized statement

您需要初始化数组,以便它可以为String 元素分配正确的内存存储在您可以开始设置索引之前

如果您声明数组(如您所做的那样),则没有为String 元素分配内存,而只有errorSoon 的引用句柄,并且当您声明时会抛出错误尝试在任何索引处初始化变量。

作为旁注,您还可以初始化大括号内的 String 数组,{ } 就这样,

String[] errorSoon = {"Hello", "World"};

相当于

String[] errorSoon = new String[2];
errorSoon[0] = "Hello";
errorSoon[1] = "World";

【讨论】:

  • 不能使用 () 来实例化数组中的每个 String 都具有默认值,这是一个遗憾。一个包含 5 个空字符串的数组应该是 = new Array[5]("");而不是 = {"","","","",""}。
  • 使用 for 循环。
【解决方案2】:
String[] args = new String[]{"firstarg", "secondarg", "thirdarg"};

【讨论】:

  • 也许不是 OPs 问题标题所暗示的,但我试图将我的字符串传递给一个接受 String[] 的参数,这就是解决方案
  • 你不能忽略新的字符串吗?字符串[] 输出 = {"","",""};似乎在我的代码中工作。
  • 如果你已经初始化了你的数组并且你想重新初始化它,你不能去args = {"new","array"};你必须args = new String[]{"new", "array"};
【解决方案3】:
String[] errorSoon = { "foo", "bar" };

-- 或--

String[] errorSoon = new String[2];
errorSoon[0] = "foo";
errorSoon[1] = "bar";

【讨论】:

    【解决方案4】:

    Java 8 中,我们还可以使用流,例如

    String[] strings = Stream.of("First", "Second", "Third").toArray(String[]::new);
    

    如果我们已经有一个字符串列表(stringList),那么我们可以收集到字符串数组中:

    String[] strings = stringList.stream().toArray(String[]::new);
    

    【讨论】:

      【解决方案5】:

      我相信你刚刚从 C++ 迁移过来,那么在 java 中你必须初始化一个数据类型(除了原始类型之外,String 在 java 中不被视为原始类型)才能根据它们的规范使用它们,如果你不这样做'那么它就像一个空的引用变量(很像 C++ 上下文中的指针)。

      public class StringTest {
          public static void main(String[] args) {
              String[] errorSoon = new String[100];
              errorSoon[0] = "Error, why?";
              //another approach would be direct initialization
              String[] errorsoon = {"Error , why?"};   
          }
      }
      

      【讨论】:

        【解决方案6】:
        String[] errorSoon = new String[n];
        

        n 是它需要保存的字符串数。

        您可以在声明中执行此操作,或者稍后不使用 String[] 执行此操作,只要在您尝试使用它们之前即可。

        【讨论】:

          【解决方案7】:
          String[] arr = {"foo", "bar"};
          

          如果将字符串数组传递给方法,请执行以下操作:

          myFunc(arr);
          

          或者做:

          myFunc(new String[] {"foo", "bar"});
          

          【讨论】:

            【解决方案8】:

            你总是可以这样写

            String[] errorSoon = {"Hello","World"};
            
            For (int x=0;x<errorSoon.length;x++) // in this way u create a for     loop that would like display the elements which are inside the array     errorSoon.oh errorSoon.length is the same as errorSoon<2 
            
            {
               System.out.println(" "+errorSoon[x]); // this will output those two     words, at the top hello and world at the bottom of hello.  
            }
            

            【讨论】:

              【解决方案9】:

              您可以使用下面的代码来初始化大小并将空值设置为字符串数组

              String[] row = new String[size];
              Arrays.fill(row, "");
              

              【讨论】:

                【解决方案10】:

                字符串声明:

                String str;
                

                字符串初始化

                String[] str=new String[3];//if we give string[2] will get Exception insted
                str[0]="Tej";
                str[1]="Good";
                str[2]="Girl";
                
                String str="SSN"; 
                

                我们可以获取字符串中的单个字符:

                char chr=str.charAt(0);`//output will be S`
                

                如果我想像这样获得单个字符的 Ascii 值:

                System.out.println((int)chr); //output:83
                

                现在我想将 Ascii 值转换为字符/符号。

                int n=(int)chr;
                System.out.println((char)n);//output:S
                

                【讨论】:

                  【解决方案11】:
                  String[] string=new String[60];
                  System.out.println(string.length);
                  

                  它是初始化并以非常简单的方式为初学者获取 STRING LENGTH 代码

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 2012-11-03
                    • 1970-01-01
                    • 2012-02-27
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    相关资源
                    最近更新 更多