Codigos java

Solo disponible en BuenasTareas
  • Páginas : 7 (1526 palabras )
  • Descarga(s) : 0
  • Publicado : 4 de diciembre de 2010
Leer documento completo
Vista previa del texto
This program illustrates some of the methods specified by the List interface:
1 public class TestStringList {
2 public static void main(String[] args) {
3 List list = new ArrayList();
4 Collections.addAll(list, "GB", "DE", "FR", "ES");
5 System.out.println(list);
6 list.add(3, "DE");
7 System.out.println(list);
8 System.out.println("list.get(3): " + list.get(3));
9System.out.println("list.indexOf("DE"): " + list.indexOf("DE"));
10 System.out.println("list.indexOf("IE"): " + list.indexOf("IE"));
11 System.out.println("list.subList(1, 5): " + list.subList(1, 5));
12 list.remove("DE");
13 System.out.println(list);
14 }
15 }

EXAMPLE 7.2 Using the sublist() Method as a Range-View Operation

1 public class TestSubList {
2 public static voidmain(String[] args) {
3 List list = new ArrayList();
4 Collections.addAll(list, "A","B","C","D","E","F","G","H","I","J");
5 System.out.println(list);
6 System.out.println("list.subList(3,8): " + list.subList(3,8));
7 System.out.println("list.subList(3,8).get(2): "
8 + list.subList(3,8).get(2));
9 System.out.println("list.subList(3,8).set(2,"B"):");
10 list.subList(3,8).set(2,"B");
11 System.out.println(list);
12 System.out.println("list.indexOf("B"): " + list.indexOf("B"));
13 System.out.println("list.subList(3,8).indexOf("B"): "
14 + list.subList(3,8).indexOf("B"));
15 System.out.println(list);
16 System.out.println("Collections.reverse(list.subList(3,8)):");
17 Collections.reverse(list.subList(3,8));
18 System.out.println(list);
19System.out.println("Collections.rotate(list.subList(3,8), 2):");
20 Collections.rotate(list.subList(3,8), 2);
21 System.out.println(list);
22 System.out.println("Collections.fill(list.subList(3,8), "X"):");
23 Collections.fill(list.subList(3,8), "X");
24 System.out.println(list);
25 list.subList(3,8).clear();
26 System.out.println(list);
27 }
28 }

EXAMPLE 7.3 Using thesublist() Method as a Range-View Operation

1 public class TestSubList {
2 public static void main(String[] args) {
3 List list = new ArrayList();
4 Collections.addAll(list, "A","B","C","D","E","F","G","H");
5 System.out.println(list);
6 ListIterator it = list.listIterator();
7 System.out.println("it.nextIndex(): " + it.nextIndex());
8 System.out.println("it.next(): " +it.next());
9 System.out.println("it.previousIndex(): " + it.previousIndex());
10 System.out.println("it.nextIndex(): " + it.nextIndex());
11 System.out.println("it.next(): " + it.next());
12 System.out.println("it.next(): " + it.next());
13 System.out.println("it.previous(): " + it.previous());
14 System.out.println("it.previousIndex(): " + it.previousIndex());
15System.out.println("it.nextIndex(): " + it.nextIndex());
16 System.out.println("it.previous(): " + it.previous());
17 System.out.println("it.next(): " + it.next());
18 System.out.println("it.next(): " + it.next());
19 System.out.println("it.next(): " + it.next());
20 System.out.println("it.add("X"):");
21 it.add("X");
22 System.out.println(list);
23 System.out.println("it.next(): " +it.next());
24 System.out.println("it.set("Y"):");
25 it.set("Y");
26 System.out.println(list);
27 System.out.println("it.next(): " + it.next());
28 System.out.println("it.remove():");
29 it.remove();
30 System.out.println("it.next(): " + it.next());
31 System.out.println(list);
32 }
33 }

EXAMPLE 7.4 Using Several ListIterator Objects Iterating on the Same List Object
Thisprogram illustrates some of the methods that are specific to the ArrayList class:

1 public class TestingSeveralIterators {
2 public static void main(String[] args) {
3 List list = new ArrayList();
4 Collections.addAll(list, "A", "B", "C", "D");
5 System.out.println(list);
6 ListIterator it1 = list.listIterator();
7 System.out.println("it1.next(): " + it1.next());
8...
tracking img