Soit la classe fr.umlv.td.author.Author :
public class Author { public Author(String firstName,String lastName) { this.firstName=firstName; this.lastName=lastName; } public @Override String toString() { return firstName+' '+lastName; } private final String firstName; private final String lastName; }
Map<Author,String> tels=new HashMap<Author,String>(); Author danBrown=new Author("Dan","Brown"); tels.put(danBrown,"001-745-897"); System.out.println(tels.get(danBrown)); System.out.println(tels.get(new Author("Dan","Brown")));
On souhaite implanter un algorithme permettant de répartir une liste dans un ordre aléatoire.
/** Swaps the elements at the specified positions in the specified list. * (If the specified positions are equal, invoking this method leaves the list unchanged.) * * @param list the list in which to swap elements. * @param i the index of one element to be swapped. * @param the index of the other element to be swapped. * @throws IndexOutOfBoundsException if either i or j is out of range. */
void swap(List<?> list,int i,int j);
void shuffle(List<?> list1,List<?> list2);
On souhaite pouvoir écrire le code suivant :
Iterator<Integer> it=rangeIterator(1,5); for(;it.hasNext();) System.out.println(it.next()); // affiche 1 2 3 4 5
On souhaite maintenant pouvoir écrire le code suivant :
for(int i:range(1,5)) System.out.println(i); // affiche 1 2 3 4 5
for(int i:range(190,200)) System.out.println(i);