Java by API/java.util/List — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 17:43, 31 мая 2010
Содержание
- 1 List: addAll(Collection<? extends T> c)
- 2 List: add(T e)
- 3 List: clear()
- 4 List: clone()
- 5 List: contains(Object o)
- 6 List: equals(Object o)
- 7 List: get(int index)
- 8 List: isEmpty()
- 9 List: lastIndexOf(Object o)
- 10 List: listIterator()
- 11 List: removeAll(Collection<?> c)
- 12 List: remove(int index)
- 13 List: remove(Object o)
- 14 List: retainAll(Collection<?> c)
- 15 List: set(int index, T element)
- 16 List: subList(int fromIndex, int toIndex)
- 17 List: toArray(T[] a)
List: addAll(Collection<? extends T> c)
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String args[]) throws Exception {
List<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");
List<String> list2 = new ArrayList<String>();
list2.add("X");
list2.add("Y");
list2.add("Z");
list.addAll(list2);
list.addAll(1, list2);
System.out.println(list);
}
}
List: add(T e)
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Main {
public static void main(String[] args) {
List<String> myList = new ArrayList<String>();
myList.add("Hello");
myList.add("World");
myList.add("World");
Set<String> set = new HashSet<String>(myList);
System.out.println(set);
}
}
List: clear()
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String args[]) throws Exception {
List<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");
list.clear();
System.out.println(list);
}
}
List: clone()
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] a) {
List<String> list = new ArrayList<String>();
list.add("A");
List list2 = ((List) ((ArrayList) list).clone());
System.out.println(list);
System.out.println(list2);
list.clear();
System.out.println(list);
System.out.println(list2);
}
}
List: contains(Object o)
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] a) {
List list = Arrays.asList(new String[] { "A", "B", "C", "D" });
System.out.println(list.contains("C"));
}
}
List: equals(Object o)
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] a) {
List list = Arrays.asList(new String[] { "A", "B", "C", "D" });
List list2 = Arrays.asList(new String[] { "A", "B", "C" });
System.out.println(list.equals(list2));
}
}
List: get(int index)
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String args[]) throws Exception {
List<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");
System.out.println(list.get(2));
}
}
List: isEmpty()
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] a) {
List list = Arrays.asList(new String[] { "A", "B", "C", "D" });
System.out.println(list.size());
System.out.println(list.isEmpty());
}
}
List: lastIndexOf(Object o)
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] a) {
List list = Arrays.asList(new String[] { "A", "B", "C", "D" });
System.out.println(list.lastIndexOf("A"));
}
}
List: listIterator()
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
public class Main {
public static void main(String[] a) {
List<String> list = new LinkedList<String>();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
ListIterator iter = list.listIterator();
while (iter.hasNext()) {
System.out.println(iter.next());
}
}
}
List: removeAll(Collection<?> c)
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String args[]) throws Exception {
List<String> list = new ArrayList<String>();
list.add("A");
list.add("A");
list.add("B");
list.add("B");
list.add("C");
list.add("C");
List<String> list2 = new ArrayList<String>();
list2.add("A");
list2.add("B");
list2.add("C");
list.removeAll(list2);
System.out.println(list);
}
}
List: remove(int index)
import java.util.List;
import java.util.Vector;
public class Main {
public static void main(String args[]) {
Vector<String> v1 = new Vector<String>();
v1.add("A");
v1.add("B");
v1.add("C");
List l = v1.subList(1, 2);
l.remove(0);
System.out.println(l);
System.out.println(v1);
}
}
List: remove(Object o)
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String args[]) throws Exception {
List<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");
System.out.println(list.remove(0));
System.out.println(list.remove("B"));
System.out.println(list);
}
}
List: retainAll(Collection<?> c)
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] argv) throws Exception {
List<String> list1 = new ArrayList();
List<String> list2 = new ArrayList();
list1.addAll(list2);
list1.removeAll(list2);
list1.retainAll(list2);
list1.clear();
int newSize = 2;
list1.subList(newSize, list1.size()).clear();
}
}
List: set(int index, T element)
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] a) {
List<String> list = Arrays.asList(new String[] { "A", "B", "C", "D" });
list.set(2, "X");
System.out.println(list);
}
}
List: subList(int fromIndex, int toIndex)
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] argv) throws Exception {
List<String> list1 = new ArrayList();
List<String> list2 = new ArrayList();
list1.addAll(list2);
list1.removeAll(list2);
list1.retainAll(list2);
list1.clear();
int newSize = 2;
list1.subList(newSize, list1.size()).clear();
}
}
List: toArray(T[] a)
import java.util.Arrays;
import java.util.List;
public class MainClass {
public static void main(String args[]) {
String[] a = new String[] { "a", "c", "b" };
List l = (Arrays.asList());
String stuff[] = (String[]) l.toArray(new String[0]);
}
}