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

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

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

LinkedList: add(E o)

 
/**
 *Output: 
Original contents of ll: [A, A2, B, C, D, E, F, Z]
Contents of ll after deletion: [A, A2, C, D, E, Z]
ll after deleting first and last: [A2, C, D, E]
ll after change: [A2, C, D Changed, E]
 */
import java.util.LinkedList;
public class MainClass {
  public static void main(String args[]) {
    LinkedList<String> ll = new LinkedList<String>();
    ll.add("B");
    ll.add("C");
    ll.add("D");
    ll.add("E");
    ll.add("F");
    ll.addLast("Z");
    ll.addFirst("A");
    ll.add(1, "A2");
    System.out.println("Original contents of ll: " + ll);
    ll.remove("F");
    ll.remove(2);
    System.out.println("Contents of ll after deletion: " + ll);
    ll.removeFirst();
    ll.removeLast();
    System.out.println("ll after deleting first and last: " + ll);
    String val = ll.get(2);
    ll.set(2, val + " Changed");
    System.out.println("ll after change: " + ll);
  }
}





LinkedList: addFirst(E o)

 
/**
 *Output: 
Original contents of ll: [A, A2, B, C, D, E, F, Z]
Contents of ll after deletion: [A, A2, C, D, E, Z]
ll after deleting first and last: [A2, C, D, E]
ll after change: [A2, C, D Changed, E]
 */
import java.util.LinkedList;
public class MainClass {
  public static void main(String args[]) {
    LinkedList<String> ll = new LinkedList<String>();
    ll.add("B");
    ll.add("C");
    ll.add("D");
    ll.add("E");
    ll.add("F");
    ll.addLast("Z");
    ll.addFirst("A");
    ll.add(1, "A2");
    System.out.println("Original contents of ll: " + ll);
    ll.remove("F");
    ll.remove(2);
    System.out.println("Contents of ll after deletion: " + ll);
    ll.removeFirst();
    ll.removeLast();
    System.out.println("ll after deleting first and last: " + ll);
    String val = ll.get(2);
    ll.set(2, val + " Changed");
    System.out.println("ll after change: " + ll);
  }
}





LinkedList: add(int index, E element)

 
/**
 *Output: 
Original contents of ll: [A, A2, B, C, D, E, F, Z]
Contents of ll after deletion: [A, A2, C, D, E, Z]
ll after deleting first and last: [A2, C, D, E]
ll after change: [A2, C, D Changed, E]
 */
import java.util.LinkedList;
public class MainClass {
  public static void main(String args[]) {
    LinkedList<String> ll = new LinkedList<String>();
    ll.add("B");
    ll.add("C");
    ll.add("D");
    ll.add("E");
    ll.add("F");
    ll.addLast("Z");
    ll.addFirst("A");
    ll.add(1, "A2");
    System.out.println("Original contents of ll: " + ll);
    ll.remove("F");
    ll.remove(2);
    System.out.println("Contents of ll after deletion: " + ll);
    ll.removeFirst();
    ll.removeLast();
    System.out.println("ll after deleting first and last: " + ll);
    String val = ll.get(2);
    ll.set(2, val + " Changed");
    System.out.println("ll after change: " + ll);
  }
}





LinkedList: addLast(E o)

 
/**
 *Output: 
Original contents of ll: [A, A2, B, C, D, E, F, Z]
Contents of ll after deletion: [A, A2, C, D, E, Z]
ll after deleting first and last: [A2, C, D, E]
ll after change: [A2, C, D Changed, E]
 */
import java.util.LinkedList;
public class MainClass {
  public static void main(String args[]) {
    LinkedList<String> ll = new LinkedList<String>();
    ll.add("B");
    ll.add("C");
    ll.add("D");
    ll.add("E");
    ll.add("F");
    ll.addLast("Z");
    ll.addFirst("A");
    ll.add(1, "A2");
    System.out.println("Original contents of ll: " + ll);
    ll.remove("F");
    ll.remove(2);
    System.out.println("Contents of ll after deletion: " + ll);
    ll.removeFirst();
    ll.removeLast();
    System.out.println("ll after deleting first and last: " + ll);
    String val = ll.get(2);
    ll.set(2, val + " Changed");
    System.out.println("ll after change: " + ll);
  }
}





LinkedList: clear()

 
import java.util.LinkedList;
public class Main {
  public static void main(String[] args) {
    LinkedList<String> lList = new LinkedList<String>();
    lList.add("1");
    lList.add("2");
    lList.add("3");
    lList.add("4");
    lList.add("5");
    System.out.println("LinkedList contains : " + lList);
    lList.clear();
    System.out.println("LinkedList now contains : " + lList);
  }
}





LinkedList: getFirst()

 
import java.util.LinkedList;
public class Main {
  public static void main(String[] args) {
    LinkedList<String> lList = new LinkedList<String>();
    lList.add("1");
    lList.add("2");
    lList.add("3");
    lList.add("4");
    lList.add("5");
    System.out.println("First element of LinkedList is : " + lList.getFirst());
    System.out.println("Last element of LinkedList is : " + lList.getLast());
  }
}





LinkedList: get(int index)

 
/**
 *Output: 
Original contents of ll: [A, A2, B, C, D, E, F, Z]
Contents of ll after deletion: [A, A2, C, D, E, Z]
ll after deleting first and last: [A2, C, D, E]
ll after change: [A2, C, D Changed, E]
 */
import java.util.LinkedList;
public class MainClass {
  public static void main(String args[]) {
    LinkedList<String> ll = new LinkedList<String>();
    ll.add("B");
    ll.add("C");
    ll.add("D");
    ll.add("E");
    ll.add("F");
    ll.addLast("Z");
    ll.addFirst("A");
    ll.add(1, "A2");
    System.out.println("Original contents of ll: " + ll);
    ll.remove("F");
    ll.remove(2);
    System.out.println("Contents of ll after deletion: " + ll);
    ll.removeFirst();
    ll.removeLast();
    System.out.println("ll after deleting first and last: " + ll);
    String val = ll.get(2);
    ll.set(2, val + " Changed");
    System.out.println("ll after change: " + ll);
  }
}





LinkedList: getLast()

 
import java.util.LinkedList;
public class Main {
  public static void main(String[] args) {
    LinkedList<String> lList = new LinkedList<String>();
    lList.add("1");
    lList.add("2");
    lList.add("3");
    lList.add("4");
    lList.add("5");
    System.out.println("First element of LinkedList is : " + lList.getFirst());
    System.out.println("Last element of LinkedList is : " + lList.getLast());
  }
}





LinkedList: indexOf(Object o)

 
import java.util.LinkedList;
public class Main {
  public static void main(String[] args) {
    LinkedList<String> lList = new LinkedList<String>();
    lList.add("1");
    lList.add("2");
    lList.add("3");
    lList.add("4");
    lList.add("5");
    lList.add("2");
    System.out.println(lList.indexOf("2"));
    System.out.println(lList.lastIndexOf("2"));
  }
}





LinkedList: iterator()

 
import java.util.Iterator;
import java.util.LinkedList;
public class Main {
  public static void main(String[] args) {
    LinkedList<String> lList = new LinkedList<String>();
    lList.add("1");
    lList.add("2");
    lList.add("3");
    lList.add("4");
    lList.add("5");
    Iterator itr = lList.iterator();
    while (itr.hasNext()) {
      System.out.println(itr.next());
    }
  }
}





LinkedList: lastIndexOf(Object o)

 
import java.util.LinkedList;
public class Main {
  public static void main(String[] args) {
    LinkedList<String> lList = new LinkedList<String>();
    lList.add("1");
    lList.add("2");
    lList.add("3");
    lList.add("4");
    lList.add("5");
    lList.add("2");
    System.out.println(lList.indexOf("2"));
    System.out.println(lList.lastIndexOf("2"));
  }
}





LinkedList: removeFirst()

 
/**
 *Output: 
Original contents of ll: [A, A2, B, C, D, E, F, Z]
Contents of ll after deletion: [A, A2, C, D, E, Z]
ll after deleting first and last: [A2, C, D, E]
ll after change: [A2, C, D Changed, E]
 */
import java.util.LinkedList;
public class MainClass {
  public static void main(String args[]) {
    LinkedList<String> ll = new LinkedList<String>();
    ll.add("B");
    ll.add("C");
    ll.add("D");
    ll.add("E");
    ll.add("F");
    ll.addLast("Z");
    ll.addFirst("A");
    ll.add(1, "A2");
    System.out.println("Original contents of ll: " + ll);
    ll.remove("F");
    ll.remove(2);
    System.out.println("Contents of ll after deletion: " + ll);
    ll.removeFirst();
    ll.removeLast();
    System.out.println("ll after deleting first and last: " + ll);
    String val = ll.get(2);
    ll.set(2, val + " Changed");
    System.out.println("ll after change: " + ll);
  }
}





LinkedList: remove(int index)

 
/**
 *Output: 
Original contents of ll: [A, A2, B, C, D, E, F, Z]
Contents of ll after deletion: [A, A2, C, D, E, Z]
ll after deleting first and last: [A2, C, D, E]
ll after change: [A2, C, D Changed, E]
 */
import java.util.LinkedList;
public class MainClass {
  public static void main(String args[]) {
    LinkedList<String> ll = new LinkedList<String>();
    ll.add("B");
    ll.add("C");
    ll.add("D");
    ll.add("E");
    ll.add("F");
    ll.addLast("Z");
    ll.addFirst("A");
    ll.add(1, "A2");
    System.out.println("Original contents of ll: " + ll);
    ll.remove("F");
    ll.remove(2);
    System.out.println("Contents of ll after deletion: " + ll);
    ll.removeFirst();
    ll.removeLast();
    System.out.println("ll after deleting first and last: " + ll);
    String val = ll.get(2);
    ll.set(2, val + " Changed");
    System.out.println("ll after change: " + ll);
  }
}





LinkedList: removeLast()

 
/**
 *Output: 
Original contents of ll: [A, A2, B, C, D, E, F, Z]
Contents of ll after deletion: [A, A2, C, D, E, Z]
ll after deleting first and last: [A2, C, D, E]
ll after change: [A2, C, D Changed, E]
 */
import java.util.LinkedList;
public class MainClass {
  public static void main(String args[]) {
    LinkedList<String> ll = new LinkedList<String>();
    ll.add("B");
    ll.add("C");
    ll.add("D");
    ll.add("E");
    ll.add("F");
    ll.addLast("Z");
    ll.addFirst("A");
    ll.add(1, "A2");
    System.out.println("Original contents of ll: " + ll);
    ll.remove("F");
    ll.remove(2);
    System.out.println("Contents of ll after deletion: " + ll);
    ll.removeFirst();
    ll.removeLast();
    System.out.println("ll after deleting first and last: " + ll);
    String val = ll.get(2);
    ll.set(2, val + " Changed");
    System.out.println("ll after change: " + ll);
  }
}





LinkedList: remove(Object o)

 
/**
 *Output: 
Original contents of ll: [A, A2, B, C, D, E, F, Z]
Contents of ll after deletion: [A, A2, C, D, E, Z]
ll after deleting first and last: [A2, C, D, E]
ll after change: [A2, C, D Changed, E]
 */
import java.util.LinkedList;
public class MainClass {
  public static void main(String args[]) {
    LinkedList<String> ll = new LinkedList<String>();
    ll.add("B");
    ll.add("C");
    ll.add("D");
    ll.add("E");
    ll.add("F");
    ll.addLast("Z");
    ll.addFirst("A");
    ll.add(1, "A2");
    System.out.println("Original contents of ll: " + ll);
    ll.remove("F");
    ll.remove(2);
    System.out.println("Contents of ll after deletion: " + ll);
    ll.removeFirst();
    ll.removeLast();
    System.out.println("ll after deleting first and last: " + ll);
    String val = ll.get(2);
    ll.set(2, val + " Changed");
    System.out.println("ll after change: " + ll);
  }
}





LinkedList: set(int index, E element)

 
/**
 *Output: 
Original contents of ll: [A, A2, B, C, D, E, F, Z]
Contents of ll after deletion: [A, A2, C, D, E, Z]
ll after deleting first and last: [A2, C, D, E]
ll after change: [A2, C, D Changed, E]
 */
import java.util.LinkedList;
public class MainClass {
  public static void main(String args[]) {
    LinkedList<String> ll = new LinkedList<String>();
    ll.add("B");
    ll.add("C");
    ll.add("D");
    ll.add("E");
    ll.add("F");
    ll.addLast("Z");
    ll.addFirst("A");
    ll.add(1, "A2");
    System.out.println("Original contents of ll: " + ll);
    ll.remove("F");
    ll.remove(2);
    System.out.println("Contents of ll after deletion: " + ll);
    ll.removeFirst();
    ll.removeLast();
    System.out.println("ll after deleting first and last: " + ll);
    String val = ll.get(2);
    ll.set(2, val + " Changed");
    System.out.println("ll after change: " + ll);
  }
}





LinkedList: subList(int fromIndex, int toIndex)

 

import java.util.LinkedList;
import java.util.List;
public class Main {
  public static void main(String[] args) {
    LinkedList<String> lList = new LinkedList<String>();
    lList.add("1");
    lList.add("2");
    lList.add("3");
    lList.add("4");
    lList.add("5");
    System.out.println(lList);
    List lst = lList.subList(1, 4);
    System.out.println(lst);
    lst.remove(2);
    System.out.println(lst);
    System.out.println(lList);
  }
}





LinkedList: toArray(T[] a)

 
import java.util.LinkedList;
import java.util.List;
public class Main {
  public static void main(String[] args) {
    List<String> theList = new LinkedList<String>();
    theList.add("A");
    theList.add("B");
    theList.add("C");
    theList.add("D");
    String[] my = theList.toArray(new String[0]);
    for (int i = 0; i < my.length; i++) {
      System.out.println(my[i]);
    }
  }
}





new LinkedList < E > ()

 
/**
 *Output: 
Original contents of ll: [A, A2, B, C, D, E, F, Z]
Contents of ll after deletion: [A, A2, C, D, E, Z]
ll after deleting first and last: [A2, C, D, E]
ll after change: [A2, C, D Changed, E]
 */
import java.util.LinkedList;
public class MainClass {
  public static void main(String args[]) {
    LinkedList<String> ll = new LinkedList<String>();
    ll.add("B");
    ll.add("C");
    ll.add("D");
    ll.add("E");
    ll.add("F");
    ll.addLast("Z");
    ll.addFirst("A");
    ll.add(1, "A2");
    System.out.println("Original contents of ll: " + ll);
    ll.remove("F");
    ll.remove(2);
    System.out.println("Contents of ll after deletion: " + ll);
    ll.removeFirst();
    ll.removeLast();
    System.out.println("ll after deleting first and last: " + ll);
    String val = ll.get(2);
    ll.set(2, val + " Changed");
    System.out.println("ll after change: " + ll);
  }
}