From Tutorial:
List<?> list = ... list.subList(fromIndex, toIndex).clear(); int i = list.subList(fromIndex, toIndex).indexOf(o); int j = list.subList(fromIndex, toIndex).lastIndexOf(o);
public static <E> List<E> dealHand(List<E> deck, int n) { int deckSize = deck.size(); List<E> handView = deck.subList(deckSize - n, deckSize); List<E> hand = new ArrayList<E>(handView); handView.clear(); return hand; }
Also covered in Tutorial:
More stuff in java.util.concurrent too, but generally not necessary since we're not using threads
Others:
List<String> names = new ArrayList<String>();
//btw: List<String> names = new ArrayList<>(); //Java 7's diamond <> operator
public java.util.ArrayList<Direction> getPathToExit() //bad
public java.util.List<Direction> getPathToExit() //good
Generally don't use these. synchronized
, so slight overhead to them (not as efficient)
String[] cards = {"AS", "2S", "3S", "4S", "5S", ...}; java.util.List<String> deck = java.util.Arrays.asList(cards); java.util.Collections.shuffle(deck); System.out.println(deck); //last two lines print same thing System.out.println(java.util.Arrays.toString(cards));
java.util.List<String> ducks = java.util.Arrays.asList("Huey", "Dewey", "Louie");
public static void main(String... args) {
String str = ""; for (int i = 0; i < 5; i++) { str += i + " "; } System.out.println(str); //prints: 0 1 2 3 4
StringBuilder str = new StringBuilder(); for (int i = 0; i < 5; i++) { str.append(i); str.append(" "); } System.out.println(str.toString()); //prints: 0 1 2 3 4