Java by API/java.util/List — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 14:11, 31 мая 2010

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]);
  }
}