2010-04-01 5 views

답변

229

당신은, initializeerrorSoon 필요

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

코드 만 declared이 있습니다.

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

당신은 당신이 인덱스를 설정 시작 전에 String 요소 에 대한 올바른 메모리 스토리지를 할당 할 수 있도록 배열을 초기화 할 필요가있다.

errorSoonString 요소에 할당에는 메모리 만 참조 핸들이 없으며, 어떤 인덱스 변수를 초기화 할 때 오류가 발생합니다 (당신처럼) 배열을 선언 경우 .

측면 참고로

, 또한 n은 얼마나 많은 문자열되면서

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

그것은 당신이 사용할 수 없습니다 불쌍()의 모든 문자열을 인스턴스화가있다 : 경우

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

우리는 이미으로 우리는 문자열 배열로 수집 할 수 있습니다 문자열 (stringList을)의 목록을 가지고 기본 값을 가진 배열. 5 개의 빈 문자열의 배열은 = new Array [5] ("");이어야합니다. = { "", "", "", "", ""} 대신. –

+0

for 루프를 사용하십시오. –

6
String[] errorSoon = new String[n]; 

동등 괄호 안의 String 배열 { }되도록,

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

를 초기화 할 수있다 기다려야합니다.

선언문에서 그렇게 할 수 있습니다. 또는 나중에 String []을 사용하지 않고도 할 수 있습니다. 사용하기 전에 가능한 한 오래 사용하십시오.

23
String[] errorSoon = { "foo", "bar" }; 

- 또는 -

String[] errorSoon = new String[2]; 
errorSoon[0] = "foo"; 
errorSoon[1] = "bar"; 
104
String[] args = new String[]{"firstarg", "secondarg", "thirdarg"}; 
+2

OP 질문 제목에서 알 수 있듯이 문자열을 허용하는 매개 변수에 문자열을 전달하려했으나 해결책이 아닙니다. – kommradHomer

+0

새 문자열 btw를 사용할 수 없습니까? String [] output = { "", "", ""}; 내 코드에서 작동하는 것 같습니다. –

+2

배열을 초기화하고 다시 초기화하고자한다면'args = { "new", "array"};' 'args = new String [] { new ","array "}}; – Darpan

1

당신은 항상이

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

처럼 쓸 수있는 당신이해야 잘 자바, ++ 당신은 그냥 C에서 마이그레이션 생각 데이터 형을 초기화합니다 (원시 형 및 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?"}; 
    } 
} 
0

문자열 선언 :

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 

지금 내가 Charecter/기호에 ASCII 값을 변환 할를.

int n=(int)chr; 
System.out.println((char)n);//output:S 
0
String[] string=new String[60]; 
System.out.println(string.length); 

그 것이다 초기화 및 우리는 또한 예를 들어, 스트림을 활용할 수있는 Java 8 초보자

에서
2

매우 간단한 방법으로 문자열 길이 코드를 점점

String[] strings = stringList.stream().toArray(String[]::new); 
관련 문제