나는 카드 ints 1-52의 갑판의 arraylist를 가지고 있습니다. 그리고 그들은 섞여서 기본적으로 절반의 인덱스 (0,25)와 (26,51)로 갑판을 나눠서 두 명의 플레이어를 다루고 싶습니다. 그러나 각 플레이어가 이미 작성한 다른 방법을 간섭하지 않도록 arraylist를 유지하고 싶지만 목록 <을 사용하는 응답 만 봅니다. 두 플레이어의 데크를 arraylists로 유지할 수있는 방법이 있습니까?ArrayList.subList에서 ArrayList를 얻는 방법은 무엇입니까?
import java.util.ArrayList;
import java.util.Collections;
public class Methods {
public static ArrayList<Integer> deck = new ArrayList<Integer>();
public static ArrayList<Integer> deckPlayerOne = new ArrayList<Integer>();
public static ArrayList<Integer> deckPLayerTwo = new ArrayList<Integer>();
public static ArrayList<Integer> generateDeck(){
for(int i=1; i<53; i++){
deck.add(i);
}
return deck;
}
public static ArrayList<Integer> shuffle(){
Collections.shuffle(deck);
return deck;
}
public static void splitDeck(){
// deckPlayerOne = deck.subList(0, 25); <---- location of error
}
public static ArrayList<Integer> playerOneDeck(){
}
}
public static ArrayList<Integer> platerTwoDeck(){
}
}
먼저 * 어떤 오류가 발생합니까? 둘째,'List'에 대한 계약과 관련하여 정말로 필요한 것은 정적 메소드 각각이'ArrayList' 대신'List '이되는 것입니다. (그들은 당신이 지금하고있는 방식으로 새롭게해야만합니다.) 궁극적으로,'List'에 대한 계약이 성취되면, 구체적인 구현이 무엇인지에 대해 왜 신경 쓰나요? 내가 너를 돌볼 이유가 될 수 있기 때문에 나는 이것을 말한다. 그러나 그것은 나에게 아직 명백하지 않다. –
Makoto
'List' 나'Collection' 대신'ArrayList'로 변수 나 필드를 선언하는 것은 나쁜 습관입니다. – BevynQ
이론적으로 ArrayList에서 ArrayList로 직접 이동하고 싶다면 복사하고 제거해야 할 것 같습니까? 25 ++ 또는 x ++에서? – Adamc23