web-dev-qa-db-ja.com

Javaの:どのようにString []を初期化する?

エラー

% 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?";
        }
}
177
hhh

あなたは initializeerrorSoon、エラーメッセージに示されているように、あなたに必要なのは declared it)だけです。

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

配列を初期化して、String要素に正しいメモリ記憶領域を割り当てることができるようにする必要があります。 の前に インデックスの設定を開始できます。

only を(あなたが行ったように)宣言すると、String要素に割り当てられたメモリはなく、errorSoonへの参照ハンドルしかなく、任意のインデックスで変数を初期化しようとするとエラーが発生します。 。

ちなみに、String配列を中括弧で、{ }を初期化することもできます。

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

これはと同等です

String[] errorSoon = new String[2];
errorSoon[0] = "Hello";
errorSoon[1] = "World";
295
String[] args = new String[]{"firstarg", "secondarg", "thirdarg"};
117
Yauhen
String[] errorSoon = { "foo", "bar" };

- または -

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

C++から移行したばかりだと思います。Javaでは、データ型を初期化する必要があります(それ以外の場合はプリミティブ型、Stringはプリミティブ型とは見なされません)。これは空の参照変数のようなものです(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?"};   
    }
}
9

Java 8 ではストリームを利用することもできます。

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

すでに文字列のリスト(stringList)がある場合は、次のように文字列配列にまとめることができます。

String[] strings = stringList.stream().toArray(String[]::new);
7
i_am_zero
String[] errorSoon = new String[n];

Nは保持する必要がある文字列の数です。

宣言の中でそれを実行することも、後でString []を使用せずに実行することもできます。

7
AaronM

あなたはいつでもこのように書くことができます

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.  
}
1
Gopolang

文字列宣言:

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"; 

Stringで個々の文字を取得できます。

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
String[] string=new String[60];
System.out.println(string.length);

それは初心者のための非常に簡単な方法で初期化とSTRING LENGTHコードを取得することです

以下のコードを使用してサイズを初期化し、空の値をStringの配列に設定できます。

String[] row = new String[size];
Arrays.fill(row, "");
0
Ali Sadeghi
String[] arr = {"foo", "bar"};

文字列配列をメソッドに渡す場合は、次のようにします。

myFunc(arr);

またはする:

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