t
t
t
t
t t 9) Stockage des objets
tttt
9) Stockage des objets
Texte original t Traducteur : Jérome QUELIN
t
t
///
Ce chapitre contient 14 pages
1 2 3 4 5 6 7 8 9 10 11 12 13 14
\\\
t t t
t t t
t
t t t


Of course, you can also decide to make
x a LinkedList (instead of a generic List) and carry
the precise type information around with x. The beauty (and the intent)
of using the interface is that if you decide you want to change your
implementation, all you need to do is change it at the point of creation, like
this:

t Bien sûr, on peut aussi décider de faire de x une LinkedList (au lieu d'une List générique) et véhiculer le type précis d'informations avec x. La beauté (et l'intérêt) de l'utilisation d'une interface est que si on décide de changer l'implémentation, la seule chose qu'on ait besoin de changer est l'endroit où la liste est créée, comme ceci :
t t t
List x = new ArrayList();
t
List x = new ArrayList();
t t t
The rest of your code can remain
untouched (some of this genericity can also be achieved with
iterators).
t Et on ne touche pas au reste du code (une telle généricité peut aussi être réalisée via des itérateurs).
t t t
In the class hierarchy, you can see a
number of classes whose names begin with “Abstract,” and
these can seem a bit confusing at first. They are simply tools that partially
implement a particular interface. If you were making your own Set, for
example, you wouldn’t start with the Set interface and implement
all the methods, instead you’d inherit from
AbstractSet and do the minimal necessary work to
make your new class. However, the containers library contains enough
functionality to satisfy your needs virtually all the time. So for our purposes,
you can ignore any class that begins with
Abstract.”
t Dans la hiérarchie de classes, on peut voir un certain nombre de classes dont le nom débute par « Abstract », ce qui peut paraître un peu déroutant au premier abord. Ce sont simplement des outils qui implémentent partiellement une interface particulière. Si on voulait réaliser notre propre Set, par exemple, il serait plus simple de dériver AbstractSet et de réaliser le travail minimum pour créer la nouvelle classe, plutôt que d'implémenter l'interface Set et toutes les méthodes qui vont avec. Cependant, la bibliothèque de conteneurs possède assez de fonctionnalités pour satisfaire quasiment tous nos besoins. De notre point de vue, nous pouvons donc ignorer les classes débutant par « Abstract ».
t t t
Therefore, when you look at the diagram,
you’re really concerned with only those interfaces at the top of
the diagram and the concrete classes (those with solid boxes around them).
You’ll typically make an object of a concrete class, upcast it to the
corresponding interface, and then use the interface throughout the
rest of your code. In addition, you do not need to consider the legacy elements
when writing new code. Therefore, the diagram can be greatly simplified to look
like this:
t Ainsi, lorsqu'on regarde le diagramme, on n'est réellement concerné que par les interfaces du haut du diagramme et les classes concrètes (celles qui sont entourées par des boîtes solides). Typiquement, on se contentera de créer un objet d'une classe concrète, de la transtyper dans son interface correspondante, et ensuite utiliser cette interface tout au long du code. De plus, on n'a pas besoin de se préoccuper des éléments pré-existants lorsqu'on produit du nouveau code. Le diagramme peut donc être grandement simplifié pour ressembler à ceci :    
t t t

t Image
t t t
Now it only includes the interfaces and
classes that you will encounter on a regular basis, and also the elements that
we will focus on in this chapter.
t Il n'inclut plus maintenant que les classes et les interfaces que vous serez amenés à rencontrer régulièrement, ainsi que les éléments sur lesquels nous allons nous pencher dans ce chapitre.
t t t
Here’s a simple example, which
fills a Collection (represented here with an ArrayList) with
String objects, and then prints each element in the
Collection:

t Voici un exemple simple, qui remplit une Collection (représenté ici par une ArrayList) avec des objets String, et affiche ensuite chaque élément de la Collection :
t t t
//: c09:SimpleCollection.java
// A simple example using Java 2 Collections.
import java.util.*;

public class SimpleCollection {
  public static void main(String[] args) {
    // Upcast because we just want to
    // work with Collection features
    Collection c = new ArrayList();
    for(int i = 0; i < 10; i++)
      c.add(Integer.toString(i));
    Iterator it = c.iterator();
    while(it.hasNext())
      System.out.println(it.next());
  }
} ///:~
t
//: c09:SimpleCollection.java
// Un exemple simple d'utilisation des Collections Java 2.
import java.util.*;

public class SimpleCollection {
  public static void main(String[] args) {
    // Transtypage ascendant parce qu'on veut juste
    // travailler avec les fonctionnalités d'une Collection
    Collection c = new ArrayList();
    for(int i = 0; i < 10; i++)
      c.add(Integer.toString(i));
    Iterator it = c.iterator();
    while(it.hasNext())
      System.out.println(it.next());
  }
} ///:~
t t t
The first line in main( ) creates an ArrayList object and then upcasts it to a Collection. Since this example uses only the Collection methods, any object of a class inherited from Collection would work, but ArrayList is the typical workhorse Collection. t La première ligne de main() crée un objet ArrayList et le transtype ensuite en une Collection. Puisque cet exemple n'utilise que les méthodes de Collection, tout objet d'une classe dérivée de Collection fonctionnerait, mais l'ArrayList est la Collection à tout faire typique.
t t t
The add( ) method, as its name suggests, puts a new element in the Collection. However, the documentation carefully states that add( ) “ensures that this Container contains the specified element.” This is to allow for the meaning of Set, which adds the element only if it isn’t already there. With an ArrayList, or any sort of List, add( ) always means “put it in,” because Lists don’t care if there are duplicates. t La méthode add(), comme son nom le suggère, ajoute un nouvel élément dans la Collection. En fait, la documentation précise bien que add() « assure que le conteneur contiendra l'élément spécifié ». Cette précision concerne les Sets, qui n'ajoutent un élément que s'il n'est pas déjà présent. Avec une ArrayList, ou n'importe quel type de List, add() veut toujours dire « stocker dans », parce qu'une List se moque de contenir des doublons.
t t t
All Collections can produce an Iterator via their iterator( ) method. Here, an Iterator is created and used to traverse the Collection, printing each element. t Toutes les Collections peuvent produire un Iterator grâce à leur méthode iterator(). Ici, un Iterator est créé et utilisé pour traverser la Collection, en affichant chaque élément.
t t t

Collection functionality

t

Fonctionnalités des Collections

t t t
The following table shows everything you can do with a Collection (not including the methods that automatically come through with Object), and thus, everything you can do with a Set or a List. (List also has additional functionality.) Maps are not inherited from Collection, and will be treated separately. t La table suivante contient toutes les opérations définies pour une Collection (sans inclure les méthodes directement héritées de la classe Object), et donc pour un Set ou une List (les Lists possèdent aussi d'autres fonctionnalités). Les Maps n'héritant pas de Collection, elles seront traitées séparément.  
t t t
boolean add(Object) Ensures that the container holds the argument. Returns false if it doesn’t add the argument. (This is an “optional” method, described later in this chapter.)
boolean
addAll(Collection)
Adds all the elements in the argument. Returns true if any elements were added. (“Optional.”)
void clear( ) Removes all the elements in the container. (“Optional.”)
boolean
contains(Object)
true if the container holds the argument.
boolean containsAll(Collection) true if the container holds all the elements in the argument.
boolean isEmpty( ) true if the container has no elements.
Iterator iterator( ) Returns an Iterator that you can use to move through the elements in the container.
boolean
remove(Object)
If the argument is in the container, one instance of that element is removed. Returns true if a removal occurred. (“Optional.”)
boolean removeAll(Collection) Removes all the elements that are contained in the argument. Returns true if any removals occurred. (“Optional.”)
boolean retainAll(Collection) Retains only elements that are contained in the argument (an “intersection” from set theory). Returns true if any changes occurred. (“Optional.”)
int size( ) Returns the number of elements in the container.
Object[] toArray( ) Returns an array containing all the elements in the container.
Object[]
toArray(Object[] a)
Returns an array containing all the elements in the container, whose type is that of the array a rather than plain Object (you must cast the array to the right type).
t
boolean add(Object) Assure que le conteneur stocke l'argument. Renvoie false si elle n'ajoute pas l'argument (c'est une méthode « optionnelle », décrite plus tard dans ce chapitre).
boolean addAll(Collection) Ajoute tous les éléments de l'argument. Renvoie true si un élément a été ajouté (« optionnelle »).
void clear() Supprime tous les éléments du conteneur (« optionnelle »).
boolean contains(Object) true si le conteneur contient l'argument.
boolean containsAll(Collection) true si le conteneur contient tous les éléments de l'argument. 
boolean isEmpty() true si le conteneur ne contient pas d'éléments. 
Iterator iterator() Renvoie un Iterator qu'on peut utiliser pour parcourir les éléments du conteneur.
boolean remove(Object) Si l'argument est dans le conteneur, une instance de cet élément est enlevée. Renvoie true si c'est le cas (« optionnelle »).
boolean removeAll(Collection) Supprime tous les éléments contenus dans l'argument. Renvoie true si au moins une suppression a été effectuée (« optionnelle »).
boolean retainAll(Collection) Ne garde que les éléments contenus dans l'argument (une « intersection » selon la théorie des ensembles). Renvoie true s'il y a eu un changement (« optionnelle »).
int size() Renvoie le nombre d'éléments dans le conteneur. 
Object[] toArray() Renvoie un tableau contenant tous les éléments du conteneur.
Object[] toArray(Object[] a) Renvoie un tableau contenant tous les éléments du conteneur, dont le type est celui du tableau a au lieu d'Objects génériques (il faudra toutefois transtyper le tableau dans son type correct).
t t t
Notice that there’s no get( ) function for random-access element selection. That’s because Collection also includes Set, which maintains its own internal ordering (and thus makes random-access lookup meaningless). Thus, if you want to examine all the elements of a Collection you must use an iterator; that’s the only way to fetch things back. t Notez qu'il n'existe pas de fonction get() permettant un accès aléatoire. Ceci parce que les Collections contiennent aussi les Sets, qui maintiennent leur propre ordre interne, faisant de toute tentative d'accès aléatoire un non-sens. Il faut donc utiliser un Iterator pour parcourir tous les éléments d'une Collection ; c'est la seule façon de récupérer les objets stockés.
t t t
The following example demonstrates all of these methods. Again, these work with anything that inherits from Collection, but an ArrayList is used as a kind of “least-common denominator”: t L'exemple suivant illustre toutes ces méthodes. Encore une fois, cet exemple marcherait avec tout objet héritant de Collection, mais nous utilisons ici une ArrayList comme « plus petit dénominateur commun » :
t t t
//: c09:Collection1.java
// Things you can do with all Collections.
import java.util.*;
import com.bruceeckel.util.*;

public class Collection1 {
  public static void main(String[] args) {
    Collection c = new ArrayList();
    Collections2.fill(c,
      Collections2.countries, 10);    
    c.add("ten");
    c.add("eleven");
    System.out.println(c);
    // Make an array from the List:
    Object[] array = c.toArray();
    // Make a String array from the List:
    String[] str =
      (String[])c.toArray(new String[1]);
    // Find max and min elements; this means
    // different things depending on the way
    // the Comparable interface is implemented:
    System.out.println("Collections.max(c) = " +
      Collections.max(c));
    System.out.println("Collections.min(c) = " +
      Collections.min(c));
    // Add a Collection to another Collection
    Collection c2 = new ArrayList();
    Collections2.fill(c2,
      Collections2.countries, 10);    
    c.addAll(c2);
    System.out.println(c);
    c.remove(CountryCapitals.pairs[0][0]);
    System.out.println(c);
    c.remove(CountryCapitals.pairs[1][0]);
    System.out.println(c);
    // Remove all components that are in the
    // argument collection:
    c.removeAll(c2);
    System.out.println(c);
    c.addAll(c2);
    System.out.println(c);
    // Is an element in this Collection?
    String val = CountryCapitals.pairs[3][0];
    System.out.println(
      "c.contains(" + val  + ") = "
      + c.contains(val));
    // Is a Collection in this Collection?
    System.out.println(
      "c.containsAll(c2) = "+ c.containsAll(c2));
    Collection c3 = ((List)c).subList(3, 5);
    // Keep all the elements that are in both
    // c2 and c3 (an intersection of sets):
    c2.retainAll(c3);
    System.out.println(c);
    // Throw away all the elements
    // in c2 that also appear in c3:
    c2.removeAll(c3);
    System.out.println("c.isEmpty() = " +
      c.isEmpty());
    c = new ArrayList();
    Collections2.fill(c,
      Collections2.countries, 10);    
    System.out.println(c);
    c.clear(); // Remove all elements
    System.out.println("after c.clear():");
    System.out.println(c);
  }
} ///:~
t
//: c09:Collection1.java
// Opérations disponibles sur les Collections.
import java.util.*;
import com.bruceeckel.util.*;

public class Collection1 {
  public static void main(String[] args) {
    Collection c = new ArrayList();
    Collections2.fill(c,
      Collections2.countries, 10);    
    c.add("ten");
    c.add("eleven");
    System.out.println(c);
    // Crée un tableau à partir de la List :
    Object[] array = c.toArray();
    // Crée un tableau de Strings à partir de la List :
    String[] str =
      (String[])c.toArray(new String[1]);
    // Trouve les éléments mini et maxi ; ceci peut
    // signifier différentes choses suivant la manière
    // dont l'interface Comparable est implémentée :
    System.out.println("Collections.max(c) = " +
      Collections.max(c));
    System.out.println("Collections.min(c) = " +
      Collections.min(c));
    // Ajoute une Collection à une autre Collection
    Collection c2 = new ArrayList();
    Collections2.fill(c2,
      Collections2.countries, 10);    
    c.addAll(c2);
    System.out.println(c);
    c.remove(CountryCapitals.pairs[0][0]);
    System.out.println(c);
    c.remove(CountryCapitals.pairs[1][0]);
    System.out.println(c);
    // Supprime tous les éléments
    // de la Collection argument :
    c.removeAll(c2);
    System.out.println(c);
    c.addAll(c2);
    System.out.println(c);
    // Est-ce qu'un élément est dans la Collection ?
    String val = CountryCapitals.pairs[3][0];
    System.out.println(
      "c.contains(" + val  + ") = "
      + c.contains(val));
    // Est-ce qu'une Collection est contenue dans la Collection ?
    System.out.println(
      "c.containsAll(c2) = "+ c.containsAll(c2));
    Collection c3 = ((List)c).subList(3, 5);
    // Garde les éléments présents à la fois dans
    // c2 et c3 (intersection d'ensembles) :
    c2.retainAll(c3);
    System.out.println(c);
    // Supprime tous les éléments
    // de c2 contenus dans c3 :
    c2.removeAll(c3);
    System.out.println("c.isEmpty() = " +
      c.isEmpty());
    c = new ArrayList();
    Collections2.fill(c,
      Collections2.countries, 10);    
    System.out.println(c);
    c.clear(); // Supprime tous les éléments
    System.out.println("after c.clear():");
    System.out.println(c);
  }
} ///:~
t t t
ArrayLists are created containing different sets of data and upcast to Collection objects, so it’s clear that nothing other than the Collection interface is being used. main( ) uses simple exercises to show all of the methods in Collection. t Les ArrayLists sont créées et initialisées avec différents ensembles de données, puis transtypées en objets Collection ; il est donc clair que seules les fonctions de l'interface Collection sont utilisées. main() réalise de simples opérations pour illustrer toutes les méthodes de Collection.
t t t
The following sections describe the various implementations of List, Set, and Map and indicate in each case (with an asterisk) which one should be your default choice. You’ll notice that the legacy classes Vector, Stack, and Hashtable are not included because in all cases there are preferred classes within the Java 2 Containers. t Les sections suivantes décrivent les diverses implémentations des Lists, Sets et Maps et indiquent dans chaque cas (à l'aide d'une astérisque) laquelle devrait être votre choix par défaut. Vous noterez que les classes pré-existantes Vector, Stack et Hashtable ne sont pas incluses car certains conteneurs Java 2 fournissent les mêmes fonctionnalités.
t t t

List functionality

t

Fonctionnalités des Lists

t t t
The basic List is quite simple to use, as you’ve seen so far with ArrayList. Although most of the time you’ll just use add( ) to insert objects, get( ) to get them out one at a time, and iterator( ) to get an Iterator to the sequence, there’s also a set of other methods that can be useful. t La List de base est relativement simple à utiliser, comme vous avez pu le constater jusqu'à présent avec les ArrayLists. Mis à part les méthodes courantes add() pour insérer des objets, get() pour les retrouver un par un, et iterator() pour obtenir un Iterator sur la séquence, les listes possèdent par ailleurs tout un ensemble de méthodes qui peuvent se révéler très pratiques.
t t t
In addition, there are actually two types of List: the basic ArrayList, which excels at randomly accessing elements, and the much more powerful LinkedList (which is not designed for fast random access, but has a much more general set of methods). t Les Lists sont déclinées en deux versions : l'ArrayList de base, qui excelle dans les accès aléatoires aux éléments, et la LinkedList, bien plus puissante (qui n'a pas été conçue pour un accès aléatoire optimisé, mais dispose d'un ensemble de méthodes bien plus conséquent).  
t t t
List (interface) Order is the most important feature of a List; it promises to maintain elements in a particular sequence. List adds a number of methods to Collection that allow insertion and removal of elements in the middle of a List. (This is recommended only for a LinkedList.) A List will produce a ListIterator, and using this you can traverse the List in both directions, as well as insert and remove elements in the middle of the List.
ArrayList* A List implemented with an array. Allows rapid random access to elements, but is slow when inserting and removing elements from the middle of a list. ListIterator should be used only for back-and-forth traversal of an ArrayList, but not for inserting and removing elements, which is expensive compared to LinkedList.
LinkedList Provides optimal sequential access, with inexpensive insertions and deletions from the middle of the List. Relatively slow for random access. (Use ArrayList instead.) Also has addFirst( ), addLast( ), getFirst( ), getLast( ), removeFirst( ), and removeLast( ) (which are not defined in any interfaces or base classes) to allow it to be used as a stack, a queue, and a deque.
t
List (interface) L'ordre est la caractéristique la plus importante d'une List ; elle garantit de maintenir les éléments dans un ordre particulier. Les Lists disposent de méthodes supplémentaires permettant l'insertion et la suppression d'éléments au sein d'une List (ceci n'est toutefois recommandé que pour une LinkedList). Une List produit des ListIterators, qui permettent de parcourir la List dans les deux directions, d'insérer et de supprimer des éléments au sein de la List.
ArrayList* Une List implémentée avec un tableau. Permet un accès aléatoire instantané aux éléments, mais se révèle inefficace lorsqu'on insère ou supprime un élément au milieu de la liste. Le ListIterator ne devrait être utilisé que pour parcourir l'ArrayList dans les deux sens, et non pour l'insertion et la suppression d'éléments, opérations coûteuses comparées aux LinkedLists.
LinkedList Fournit un accès séquentiel optimal, avec des coûts d'insertion et de suppression d'éléments au sein de la List négligeables. Relativement lente pour l'accès aléatoire (préférer une ArrayList pour cela). Fournit aussi les méthodes addFirst(), addLast(), getFirst(), getLast(), removeFirst() et removeLast() (qui ne sont définies dans aucune interface ou classe de base) afin de pouvoir l'utiliser comme une pile, une file (une queue) ou une file double (queue à double entrée).
t t t
The methods in the following example each cover a different group of activities: things that every list can do (basicTest( )), moving around with an Iterator (iterMotion( )) versus changing things with an Iterator (iterManipulation( )), seeing the effects of List manipulation (testVisual( )), and operations available only to LinkedLists. t Les méthodes dans l'exemple suivant couvrent chacune un groupe de fonctionnalités : les opérations disponibles pour toutes les listes (basicTest()), le déplacement dans une liste avec un Iterator (iterMotion()) ainsi que la modification dans une liste avec un Iterator (iterManipulation()), la visualisation des manipulations sur la List (testVisual()) et les opérations disponibles uniquement pour les LinkedLists.
t t t
//: c09:List1.java
// Things you can do with Lists.
import java.util.*;
import com.bruceeckel.util.*;

public class List1 {
  public static List fill(List a) {
    Collections2.countries.reset();
    Collections2.fill(a,
      Collections2.countries, 10);
    return a;
  }
  static boolean b;
  static Object o;
  static int i;
  static Iterator it;
  static ListIterator lit;
  public static void basicTest(List a) {
    a.add(1, "x"); // Add at location 1
    a.add("x"); // Add at end
    // Add a collection:
    a.addAll(fill(new ArrayList()));
    // Add a collection starting at location 3:
    a.addAll(3, fill(new ArrayList()));
    b = a.contains("1"); // Is it in there?
    // Is the entire collection in there?
    b = a.containsAll(fill(new ArrayList()));
    // Lists allow random access, which is cheap
    // for ArrayList, expensive for LinkedList:
    o = a.get(1); // Get object at location 1
    i = a.indexOf("1"); // Tell index of object
    b = a.isEmpty(); // Any elements inside?
    it = a.iterator(); // Ordinary Iterator
    lit = a.listIterator(); // ListIterator
    lit = a.listIterator(3); // Start at loc 3
    i = a.lastIndexOf("1"); // Last match
    a.remove(1); // Remove location 1
    a.remove("3"); // Remove this object
    a.set(1, "y"); // Set location 1 to "y"
    // Keep everything that's in the argument
    // (the intersection of the two sets):
    a.retainAll(fill(new ArrayList()));
    // Remove everything that's in the argument:
    a.removeAll(fill(new ArrayList()));
    i = a.size(); // How big is it?
    a.clear(); // Remove all elements
  }
  public static void iterMotion(List a) {
    ListIterator it = a.listIterator();
    b = it.hasNext();
    b = it.hasPrevious();
    o = it.next();
    i = it.nextIndex();
    o = it.previous();
    i = it.previousIndex();
  }
  public static void iterManipulation(List a) {
    ListIterator it = a.listIterator();
    it.add("47");
    // Must move to an element after add():
    it.next();
    // Remove the element that was just produced:
    it.remove();
    // Must move to an element after remove():
    it.next();
    // Change the element that was just produced:
    it.set("47");
  }
  public static void testVisual(List a) {
    System.out.println(a);
    List b = new ArrayList();
    fill(b);
    System.out.print("b = ");
    System.out.println(b);
    a.addAll(b);
    a.addAll(fill(new ArrayList()));
    System.out.println(a);
    // Insert, remove, and replace elements
    // using a ListIterator:
    ListIterator x = a.listIterator(a.size()/2);
    x.add("one");
    System.out.println(a);
    System.out.println(x.next());
    x.remove();
    System.out.println(x.next());
    x.set("47");
    System.out.println(a);
    // Traverse the list backwards:
    x = a.listIterator(a.size());
    while(x.hasPrevious())
      System.out.print(x.previous() + " ");
    System.out.println();
    System.out.println("testVisual finished");
  }
  // There are some things that only
  // LinkedLists can do:
  public static void testLinkedList() {
    LinkedList ll = new LinkedList();
    fill(ll);
    System.out.println(ll);
    // Treat it like a stack, pushing:
    ll.addFirst("one");
    ll.addFirst("two");
    System.out.println(ll);
    // Like "peeking" at the top of a stack:
    System.out.println(ll.getFirst());
    // Like popping a stack:
    System.out.println(ll.removeFirst());
    System.out.println(ll.removeFirst());
    // Treat it like a queue, pulling elements
    // off the tail end:
    System.out.println(ll.removeLast());
    // With the above operations, it's a dequeue!
    System.out.println(ll);
  }
  public static void main(String[] args) {
    // Make and fill a new list each time:
    basicTest(fill(new LinkedList()));
    basicTest(fill(new ArrayList()));
    iterMotion(fill(new LinkedList()));
    iterMotion(fill(new ArrayList()));
    iterManipulation(fill(new LinkedList()));
    iterManipulation(fill(new ArrayList()));
    testVisual(fill(new LinkedList()));
    testLinkedList();
  }
} ///:~
t
//: c09:List1.java
// Opérations disponibles sur les Lists.
import java.util.*;
import com.bruceeckel.util.*;

public class List1 {
  public static List fill(List a) {
    Collections2.countries.reset();
    Collections2.fill(a,
      Collections2.countries, 10);
    return a;
  }
  static boolean b;
  static Object o;
  static int i;
  static Iterator it;
  static ListIterator lit;
  public static void basicTest(List a) {
    a.add(1, "x"); // Ajout à l'emplacement 1
    a.add("x"); // Ajout à la fin
    // Ajout d'une Collection :
    a.addAll(fill(new ArrayList()));
    // Ajout d'une Collection à partir du 3ème élément :
    a.addAll(3, fill(new ArrayList()));
    b = a.contains("1"); // L'élément est-il présent ?
    // La Collection entière est-elle présente ?
    b = a.containsAll(fill(new ArrayList()));
    // Les Lists permettent un accès aléatoire aux éléments,
    // bon marché pour les ArrayLists, coûteux pour les LinkedLists :
    o = a.get(1); // Récupère l'objet du premier emplacement
    i = a.indexOf("1"); // Donne l'index de l'objet
    b = a.isEmpty(); // La List contient-elle des éléments ?
    it = a.iterator(); // Iterator de base
    lit = a.listIterator(); // ListIterator
    lit = a.listIterator(3); // Démarre au 3ème élément
    i = a.lastIndexOf("1"); // Dernière concordance
    a.remove(1); // Supprime le premier élément
    a.remove("3"); // Supprime cet objet
    a.set(1, "y"); // Positionne le premier élément à "y"
    // Garde tous les éléments présents dans l'argument
    // (intersection de deux ensembles) :
    a.retainAll(fill(new ArrayList()));
    // Supprime tous les éléments présents dans l'argument :
    a.removeAll(fill(new ArrayList()));
    i = a.size(); // Taille de la List ?
    a.clear(); // Supprime tous les éléments
  }
  public static void iterMotion(List a) {
    ListIterator it = a.listIterator();
    b = it.hasNext();
    b = it.hasPrevious();
    o = it.next();
    i = it.nextIndex();
    o = it.previous();
    i = it.previousIndex();
  }
  public static void iterManipulation(List a) {
    ListIterator it = a.listIterator();
    it.add("47");
    // Doit aller sur un élément après add() :
    it.next();
    // Supprime l'élément qui vient d'être produit :
    it.remove();
    // Doit aller sur un élément après remove() :
    it.next();
    // Change l'élément qui vient d'être produit :
    it.set("47");
  }
  public static void testVisual(List a) {
    System.out.println(a);
    List b = new ArrayList();
    fill(b);
    System.out.print("b = ");
    System.out.println(b);
    a.addAll(b);
    a.addAll(fill(new ArrayList()));
    System.out.println(a);
    // Insère, supprime et remplace des éléments
    // en utilisant un ListIterator :
    ListIterator x = a.listIterator(a.size()/2);
    x.add("one");
    System.out.println(a);
    System.out.println(x.next());
    x.remove();
    System.out.println(x.next());
    x.set("47");
    System.out.println(a);
    // Traverse la liste à l'envers :
    x = a.listIterator(a.size());
    while(x.hasPrevious())
      System.out.print(x.previous() + " ");
    System.out.println();
    System.out.println("testVisual finished");
  }
  // Certaines opérations ne sont disponibles
  // que pour des LinkedLists :
  public static void testLinkedList() {
    LinkedList ll = new LinkedList();
    fill(ll);
    System.out.println(ll);
    // Utilisation comme une pile, insertion (push) :
    ll.addFirst("one");
    ll.addFirst("two");
    System.out.println(ll);
    // Utilisation comme une pile, récupération de la valeur du premier élément (peek) :
    System.out.println(ll.getFirst());
    // Utilisation comme une pile, suppression (pop) :
    System.out.println(ll.removeFirst());
    System.out.println(ll.removeFirst());
    // Utilisation comme une file, en retirant les
    // éléments à la fin de la liste :
    System.out.println(ll.removeLast());
    // Avec les opérations ci-dessus, c'est une file double !
    System.out.println(ll);
  }
  public static void main(String[] args) {
    // Crée et remplit une nouvelle List à chaque fois :
    basicTest(fill(new LinkedList()));
    basicTest(fill(new ArrayList()));
    iterMotion(fill(new LinkedList()));
    iterMotion(fill(new ArrayList()));
    iterManipulation(fill(new LinkedList()));
    iterManipulation(fill(new ArrayList()));
    testVisual(fill(new LinkedList()));
    testLinkedList();
  }
} ///:~
t t t
t t t
t t
\\\
///
t t t
t
     
Sommaire Le site de Bruce Eckel