Java by API/java.util/LinkedList
Содержание
- 1 LinkedList: add(E o)
- 2 LinkedList: addFirst(E o)
- 3 LinkedList: add(int index, E element)
- 4 LinkedList: addLast(E o)
- 5 LinkedList: clear()
- 6 LinkedList: getFirst()
- 7 LinkedList: get(int index)
- 8 LinkedList: getLast()
- 9 LinkedList: indexOf(Object o)
- 10 LinkedList: iterator()
- 11 LinkedList: lastIndexOf(Object o)
- 12 LinkedList: removeFirst()
- 13 LinkedList: remove(int index)
- 14 LinkedList: removeLast()
- 15 LinkedList: remove(Object o)
- 16 LinkedList: set(int index, E element)
- 17 LinkedList: subList(int fromIndex, int toIndex)
- 18 LinkedList: toArray(T[] a)
- 19 new LinkedList < E > ()
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);
}
}