새로 배우는 중입니다. 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 인터페이스에 지정된 메서드 만 사용해야한다고 가정 할 수 있습니다.
내 컴퓨터에 이것을 시도하고 그것은 하겠어
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()라는 메서드가 있습니다.
위의 경우 해당 메서드를 호출 할 수 있습니까?
그렇다면 일반적으로 파생 클래스의 메서드 목록은 항상 기본 클래스 메서드의 수퍼바이저가되므로 기본 클래스 포인터를 사용하여 파생 클래스 개체를 저장할 수 있다는 것을 의미합니까?
위의 결론이 맞습니까? 그것은 내 멍청한 질문이지만 나는 자바에 매우 익숙하다.
정확하게 작동하는지, 오류 메시지는 무엇입니까? –
우리가 당신을 도울 수있는 경우 귀하의 질문에 실제 코드를 제공해야합니다. 당신이 작성한 코드는 무엇입니까? 네가 본 오류가 뭐야? "작동하지 않는다"는 설명이 충분하지 않습니다. – Recurse
또한 java.util.List를 가져와야합니다. –