2013-01-24 7 views
1

새로 배우는 중입니다. util 패키지의 다양한 데이터 구조를 이해하려고합니다. 내가List 변수에 저장된 ArrayList 객체

을 본의 ArrayList 클래스의 decleration에서
/*create an ArrayList object*/ 
List<String> arrayList = new ArrayList<String>(); 

public class ArrayList 
extends AbstractList 
implements List, RandomAccess, Cloneable, Serializable 

내가 ArrayList를 <>

의 생성자에 의해 반환 된 객체를 저장하는 형식 목록 <>의 객체를 사용하는 경우 내 질문은

이 작동합니다. add() 또는 size()와 같이 List 인터페이스에 지정된 메서드 만 사용해야한다고 가정 할 수 있습니다.

내 컴퓨터에 이것을 시도하고 그것은 하겠어

http://www.javamex.com/tutorials/collections/using_1.shtml

I는 다음과 같습니다 시도 전체 예제 코드

아래의 자바 튜토리얼이 발견되지 않습니다.
package arraylist; 
import java.util.ArrayList; 
import java.util.List; 

public class ArrayListExample { 

     public static void main(String[] args) 
     { 

     /*create an ArrayList object*/ 
     List<String> arrayList = new ArrayList<String>(); 

       /* 
       Add elements to Arraylist using 
       boolean add(Object o) method. It returns true as a general behavior 
       of Collection.add method. The specified object is appended at the end 
       of the ArrayList. 
       */ 
     arrayList.add("1"); 
     arrayList.add("2"); 
     arrayList.add("3"); 

     /* 
     Use get method of Java ArrayList class to display elements of ArrayList. 
     Object get(int index) returns and element at the specified index in 
     the ArrayList 
     */ 
     System.out.println("Getting elements of ArrayList"); 
     System.out.println(arrayList.get(0)); 
     System.out.println(arrayList.get(1)); 
     System.out.println(arrayList.get(2)); 
    } 
} 

나는 List.that는 우리가 형의 ArrayList의 객체에 저장하는 유형 목록의 변수를 사용한다 나의 다음 질문을 work.However하지 않은 이유 가져올 잊어 버렸습니다.

List 및 ArrayList의 메서드 비교 ArrayList에는 list에없는 trimToSize()라는 메서드가 있습니다.

위의 경우 해당 메서드를 호출 할 수 있습니까?

그렇다면 일반적으로 파생 클래스의 메서드 목록은 항상 기본 클래스 메서드의 수퍼바이저가되므로 기본 클래스 포인터를 사용하여 파생 클래스 개체를 저장할 수 있다는 것을 의미합니까?

위의 결론이 맞습니까? 그것은 내 멍청한 질문이지만 나는 자바에 매우 익숙하다.

+2

정확하게 작동하는지, 오류 메시지는 무엇입니까? –

+2

우리가 당신을 도울 수있는 경우 귀하의 질문에 실제 코드를 제공해야합니다. 당신이 작성한 코드는 무엇입니까? 네가 본 오류가 뭐야? "작동하지 않는다"는 설명이 충분하지 않습니다. – Recurse

+4

또한 java.util.List를 가져와야합니다. –

답변

3

은 당신이 ArrayList에 초기화되는 List로 선언 된 개체를 사용할 수 있어야 List 인터페이스를 구현합니다. 이것은 꽤 일반적인 관행입니다. 코드를 변경하지 않고도 목록의 구현을 전환 할 수 있습니다.

이 목록 인터페이스를 가져 확인하십시오

package arraylist; 
import java.util.ArrayList; 
import java.util.List; 

public class ArrayListExample { 

     public static void main(String[] args) 
     { 

     /*create an ArrayList object*/ 
     List<String> arrayList = new ArrayList<String>(); 

       /* 
       Add elements to Arraylist using 
       boolean add(Object o) method. It returns true as a general behavior 
       of Collection.add method. The specified object is appended at the end 
       of the ArrayList. 
       */ 
     arrayList.add("1"); 
     arrayList.add("2"); 
     arrayList.add("3"); 

     /* 
     Use get method of Java ArrayList class to display elements of ArrayList. 
     Object get(int index) returns and element at the specified index in 
     the ArrayList 
     */ 
     System.out.println("Getting elements of ArrayList"); 
     System.out.println(arrayList.get(0)); 
     System.out.println(arrayList.get(1)); 
     System.out.println(arrayList.get(2)); 
    } 
} 

는이 예제를 고려 List

+0

@ liv2hak'trimToSize'에 대한 질문에 대답했습니다 –

+0

코드 변경없이 목록의 구현을 전환 할 수 있습니다. 이 진술이 의미하는 바를 조금 더 자세히 설명해 주시겠습니까? – liv2hak

+1

'List' 인터페이스에서만 메서드를 사용한다는 의미입니다. 그런 다음 목록의 초기화를 'LinkedList'와 같은 목록 인터페이스를 구현하는 다른 클래스로 변경할 수 있습니다. '리스트 arrayList = 새로운 LinkedList ();' –

1

List<String>의 ArrayList는 '이 변수는 List<String> 인터페이스의 저장을 구현 할 수 있습니다'List<String> 그래서 거기 살 수

ArrayList<String>를 구현하는 것을 의미한다. ArrayList 이후

2

의 인터페이스에없는 때문에이 List 당신이 trimToSize 기능을 사용할 수 없습니다 참조,

class A implements B { } 

이 경우 클래스는 구현 된 인터페이스 중 하나의 유형.

C c = new A(); 

c의 인스턴스를 사용할 때이 캐스팅을 수행하면 클래스 C에서 사용할 수있는 메서드 만 액세스 할 수 있습니다.

마찬가지로 ArrayList은 사용자의 ClassA이고 List은 예에서 클래스 B입니다. 당신이 목록 인터페이스에서 사용 가능한 메소드를 호출 할 수 아래 사용 arraylist 예에서

,

/*create an ArrayList object*/ 
List<String> arrayList = new ArrayList<String>(); 

그리고 위의 할당을 사용하는 경우 ArrayList의 방법 단지를 사용하려는 경우 당신은 trimToSize() 등과 같은 ArrayList의 메소드를 호출 할 수 없습니다 아래와 같이 사용하십시오.

/*create an ArrayList object*/ 
ArrayList<String> arrayList = new ArrayList<String>(); 

개발자의 대부분을

은, 시간의 90 %가 목록에서 제공하는 방법을 사용하고, 전자의 방법은 다음 거기에서 이유 모든 예제를 이잖아. 파일의 상단은 다음과 같이 할 수

1

편집 :

패키지 ArrayList를; 가져 오기 java.util.ArrayList; import java.util.List; 당신이 목록을 가져 오지 않은 경우

....

, 당신은 변수의 유형으로 사용할 수 없습니다. 이것은 당신이 겪고있는 문제입니다.

관련 문제