반응형
배열을 ArrayList로 변환
이 질문에 이미 답변이 있습니다.
- 배열 34 답변 에서 ArrayList 만들기
배열을 ArrayList
Java 로 바꾸는 데 많은 문제가 있습니다. 이것은 지금 내 배열입니다.
Card[] hand = new Card[2];
"hand"는 "카드"의 배열을 보유합니다. 어떻게 생겼을까 ArrayList
요?
그 ArrayList
라인은
import java.util.ArrayList;
...
ArrayList<Card> hand = new ArrayList<Card>();
ArrayList
당신이 가지고 있는 것을 사용하는 것을
hand.get(i); //gets the element at position i
hand.add(obj); //adds the obj to the end of the list
hand.remove(i); //removes the element at position i
hand.add(i, obj); //adds the obj at the specified index
hand.set(i, obj); //overwrites the object at i with the new obj
http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html 도 읽어 읽어.
이것은 당신에게 목록을 줄 것입니다.
List<Card> cardsList = Arrays.asList(hand);
arraylist를 사용할 수 있습니다.
ArrayList<Card> cardsList = new ArrayList<Card>(Arrays.asList(hand));
List<Card> list = new ArrayList<Card>(Arrays.asList(hand));
목록 선언 (그리고 빈 arraylist로 초기화)
List<Card> cardList = new ArrayList<Card>();
요소 추가 :
Card card;
cardList.add(card);
요소 반복 :
for(Card card : cardList){
System.out.println(card);
}
참고 URL : https://stackoverflow.com/questions/9811261/convert-an-array-into-an-arraylist
반응형
'ProgramingTip' 카테고리의 다른 글
다수의 실행중인 프로세스에 콘솔을 사용합니까? (0) | 2020.10.12 |
---|---|
캐스팅과 강제의 차이점은 무엇입니까? (0) | 2020.10.12 |
세미콜론으로 구분 된 분열을 Python에서 사전으로 분할 (0) | 2020.10.12 |
2 개의 클래스가 경우에만 CSS로 요소를 사용할 수 있습니까? (0) | 2020.10.12 |
정상 종료 프로세스에 신호를 어떤 순서로 보내야합니까? (0) | 2020.10.12 |