2010-06-15 6 views
2

수행 방법 :
1. 배열을 초기화 (생성)합니다.
2. 문자열 값을 입력하십시오.
3. 다른 String 값을 입력하십시오.
4. 덤프하여 내용을 가져옵니다.Java에서 배열 작업의 기초

+1

이것은 숙제처럼 들립니다. 장래에는, 다음과 같이 태그를 붙일 필요가 있습니다. 「ArrayList의 사용」, 「스택의 사용」등의 대답은, 완전히 무의미합니다. – BalusC

+0

숙제가 아닙니다. 질문이 부적절한 경우 사과드립니다. –

+0

숙제가 없다면 Collections 클래스, List, Set, Map 등을 사용해야합니다. Array의 직접 사용은 99.99999 %의 코드 냄새입니다. –

답변

7

Java의 배열은 고정 크기로 작성시 결정됩니다. 따라서 푸시 방법이 없습니다.

대신 List 인 것처럼 들릴 수 있습니다. 대체로 ArrayList<String>입니다. 목록에는 새 요소를 추가하기위한 add 함수가 있습니다.

Java Collections trail에는 다양한 유형의 컬렉션 (목록, 설정 및지도)에 대한 추가 정보가 있습니다.

목록과 설정은 각 운영자 자바의 작업 :

List<String> myList = new ArrayList<String>(); 
//List<String> myList = new LinkedList<String>(); 

myList.add("One"); 
myList.add("Two"); 

// Because we're using a Generic collection, the compiler 
// inserts a cast on the next line for you 
for (String current : myList) { 
    // This section happens once for each elements in myList 
    System.out.println(current); 
} 
// should print "One" and "Two" (without quotes) on separate lines 
+0

마침내 예제를 추가하려고했습니다. 잘하면 도움이됩니다. – Powerlord

+0

Brilliant! 내가 물어 봐야 할 질문에 대답 해 주셔서 감사합니다! –

1
anArray = new string[10]; 
anArray[0] = "MYSTRING"; 
string MyString = anArray[0]; 

for (int i =0; i <10; i++) 
{ 
    System.out.println(anArray[i]); 
} 

매우 간단까지 배열이가는대로, 원료 사용의 부담을 완화 할 수 있습니다 자바에서 다른 도서관의 몇 가지 있습니다 배열. http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html 이 기본적으로 고정 된 크기의 배열 :

2
int[] a; 
a = new int[5]; 

a[0]=1; 
a[1]=2; 
a[2]=3; 
a[3]=4; 
a[4]=5; 

for(int i =0; i<5; i++) 
    System.out.println(a[i]); 

Java.sun 배열의 도움에 대한 좋은 링크가 있습니다. 크기를 모르는 요소를 넣으려는 경우 ArrayList를보고 싶을 것입니다.

1

Stack을 사용하지 않으시겠습니까?

final Stack<String> strings = new Stack<String>(); 
strings.push("First"); 
strings.push("Second"); 
System.out.println(strings.toString()); 

당신은 또한 당신의 필요에 따라 List 또는 Queue를 사용할 수 있습니다.

+0

컬렉션에있는 toString이 작동한다는 것을 알지 못했지만 의미가 있다고 생각합니다. – Powerlord

+0

그래, 각 요소에 차례로'.toString()'을 호출하는 멋진 작은 목록을 출력한다. :디 –

1

당신이 목록을 원하는 것처럼 소리 않지만, 경우에 당신이 정말로 배열을 의미 했 ...

//1. 
String [] arr = new String[2]; 
//2. 
arr[0] = "first"; 
//3. 
arr[1] = "second"; 
//4. 
for (String s: arr) 
{ 
    System.out.println(s); 
} 
2

는 기본적으로 배열 한 번에 두 개 이상의 값을 유지하는 방법입니다. 그것은 배열에 값을 할당

int arrayName = new int[10]; 

, items.In 자바의 목록처럼 배열은 예를 들어 새로운 키워드를 사용하여 수행 할 수 있습니다 초기화 : 또한

arrayName[0] = 10; 

,

int[]ArrList = {1, 2, 3, 4,5}; 

int 배열 java의 간단한 예제는 다음과 같습니다.

public class array_ex { 

    public static void main(String []args) { 

     int arrex[] = {10,20,30}; //Declaring and initializing an array of three elements 

     for (int i=0;i&lt;arrex.length;i++){ 

      System.out.println(arrex[i]); 

     } 

    }  
} 

이것은 또한 어레이를 선언하기위한 다른 예이다

public class array_ex { 

    public static void main(String []args) { 

     int arrex[] = new int[3]; //declaring array of three items 

     arrex[0] =10; 

     arrex[1] =20; 

     arrex[2] =30; 

     //Printing array 

     for (int i=0;i&lt;arrex.length;i++){ 

      System.out.println(arrex[i]); 

     } 

    }  

} 

예들

ArrayList<String> ar = new ArrayList<String>(); 
String s1 ="sub1"; 
String s2 ="sub2"; 
String s3 ="sub3"; 
ar.add(s1); 
ar.add(s2); 
ar.add(s3); 
String s4 ="sub4"; 
ar.add(s4); 

희망 문자열 값을 미는 것이 유용!