2013-05-16 6 views
-1

카드 목록 배열에 배열 색인을 추가했습니다. 같은 값을 가진 인덱스를 추가하지 않으려합니다. 무엇을 바꾸어야합니까?배열에 저장된 동일한 값을 건너 뛰고 싶습니다.

playerHand.player_hand.add(cardList.get(lastCardIndex)); 
playerHand.player_hand.add(cardList.get(lastCardIndex - 1)); 
playerHand.player_hand.add(cardList.get(lastCardIndex - 2)); 
playerHand.player_hand.add(cardList.get(lastCardIndex - 3)); 
playerHand.player_hand.add(cardList.get(lastCardIndex - 4)); 

답변

1

당신은 사용할 수있는 Set :

중복 요소가없는 컬렉션입니다.

그럼 당신은 Set#toArray()를 사용할 수 있습니다

이 세트의 모든 요소를 ​​포함하고있는 배열을 돌려줍니다.

또한 Set에서 ArrayList을 가질 수

List<Integer> list = new ArrayList<Integer>(yourSet); 
1

이 경우 Set을 사용해야합니다.

Set<Integer> cardIndexes = new HashSet<Integer>(); 

은 그것이 첨가 값의 고유성을 보장한다. 당신이 마지막에 배열을해야하는 경우

은이를 달성하기 위해 SettoArray() 방법을 사용할 수 있습니다.

HereSet에 대한 세부 사항을 찾을 수 있습니다.

1

이 같은 HashSet에 사용할 수 있습니다.

HashSet <Integer> arr1=new HashSet<Integer>(); 
arr1.add(1); 
arr1.add(2); 
arr1.add(2); 
arr1.add(1); 
관련 문제