2013-11-22 3 views
0

나는 다음과 같은 방식으로 LinkedList들을의 ArrayList를 만들어 :의 ArrayList는

나는 다음의 ArrayList로 일 나는 것 같지 않았던이를했다 그래서 ArrayList에에 LinkedList들을 추가하기 위해 필요한
ArrayList<LinkedList<Card>> list = new ArrayList<>(5); 

 LinkedList<Card> temp_list0 = new LinkedList<Card>(); 
     LinkedList<Card> temp_list1 = new LinkedList<Card>(); 
     LinkedList<Card> temp_list2 = new LinkedList<Card>(); 
     LinkedList<Card> temp_list3 = new LinkedList<Card>(); 
     LinkedList<Card> temp_list4 = new LinkedList<Card>(); 
     list.add(temp_list0); 
     list.add(temp_list1); 
     list.add(temp_list2); 
     list.add(temp_list3); 
     list.add(temp_list4); 
,691 다음 LinkedList들을 참조를 제공함으로써 ArrayList의에 빈

for (position = 0; position < list.size(); position++) { 
list.add(new LinkedList<Card>()); 
} 

그럼 내가 수동으로 추가 LinkedList들을 남아

마지막으로, 반복 할 때마다, 뭔가를 추가하기 위해 LinkedLists 중 하나를 꺼내어 arraylist의 위치에 되돌려 놓아야했지만 LinkedList에 대한 참조를 잃어 버렸습니다. 따라서 정보는 다음과 같습니다. 내 방식이 작동하지 않기 때문에,

for (position = 0; position < deck.length; position++) { 
      scan = (deck[position]).getValue(); 
      temp_list = list.get(scan); 
      temp_list.offer(deck[position]); 
      list.add(scan, temp_list); 
     } 

정보를 잃지 않고 ArrayList에있는 LinkedList들을에 액세스 할 수있는 더 나은 방법이 있나요

을 잃었다.

답변

5

문제는 처음에 for 루프에 있습니다. size()은 할당 된 용량이 아닌 목록의 요소 수를 반환합니다 (목록 구현에도 포함 된 경우). 5을 상수로 바꾸고 0에서 상수로 반복하십시오. 그런 다음 ArrayList에서 get()/set()을 정상적으로 사용하십시오.

포함 된 LinkedList 개체를 "꺼내" "뒤로"놓을 필요가 없습니다. arrayList.get(linkedListNumber).offer(card);으로 전화하면됩니다.

+0

이렇게하면 arrayList.get (linkedListNumber) .offer (card); 그것을 arraylist에 다시 넣는 것을 알고 있습니까? – kolonel

+0

@kimos Java 참조가 작동하는 방식을 읽어야합니다. 참여하는 "뒤로 물러서는"일은 없습니다. 'LinkedList'를 검색하고 객체를 삽입하는 등의 작업을 수행합니다. 'LinkedList'는 여전히'ArrayList' 오른쪽에 있습니다. 당신이'ArrayList'에게 그것을 대체하거나 제거 할 때까지 항상 그것이있었습니다. – chrylis

+0

나는 고맙습니다. 많이 봅니다. – kolonel

관련 문제