Функциональность List - часть 2
// То, что вы можете сделать со списками.
import java.util.*; import com.bruceeckel.util.*;
public class List1 { public static List fill(List a) { Collections2.countries.reset(); Collections2.fill(a, Collections2.countries, 10); return a; } static boolean b; static Object o; static int i; static Iterator it; static ListIterator lit; public static void basicTest(List a) { a.add(1, "x"); // Вставка в позицию 1
a.add("x"); // Вставка в конец
// Добавление Сollection:
a.addAll(fill(new ArrayList())); // Добавление Collection, начиная с 3 позиции:
a.addAll(3, fill(new ArrayList())); b = a.contains("1"); // Есть здесь?
// Есть ли вся Collection здесь?
b = a.containsAll(fill(new ArrayList())); // Списки позволяют случайный доступ, который дешев
// для ArrayList, и дорог для LinkedList:
o = a.get(1); // Получить объект из позиции 1
i = a.indexOf("1"); // Узнать индекс объекта
b = a.isEmpty(); // Есть ли элементы внутри?
it = a.iterator(); // Обычный Iterator
lit = a.listIterator(); // ListIterator
lit = a.listIterator(3); // Начать с позиции 3
i = a.lastIndexOf("1"); // Последнее совпадение
a.remove(1); // Удалить из позиции 1
a.remove("3"); // Удалить этот объект
a.set(1, "y"); // Установить позицию 1 на "y"
// Оставить все, что есть в аргументе
// (пересечение двух множеств):
a.retainAll(fill(new ArrayList())); // Удаление всего, что есть в аргументе:
a.removeAll(fill(new ArrayList())); i = a.size(); // Каков размер?
a.clear(); // Удаление всех элементов
} public static void iterMotion(List a) { ListIterator it = a.listIterator(); b = it.hasNext(); b = it.hasPrevious(); o = it.next(); i = it.nextIndex(); o = it.previous(); i = it.previousIndex(); } public static void iterManipulation(List a) { ListIterator it = a.listIterator(); it.add("47"); // Должно произойти перемещение на элемент после добавления:
it.next(); // Удалить элемент, который был только что выбран: