Correction-TD1 - Généricité - Collection (1) - Copie
Correction-TD1 - Généricité - Collection (1) - Copie
Correction-TD1 - Généricité - Collection (1) - Copie
Exercice1
[3, 5, 3, 12, 3]
[3, 5, 3, 3]
[5, 3, 3]
[5]
Exercice2
Erreur à la ligne 16 : ArrayList<Base> l2 = l1;
Incompatibles types ArrayList<Derivée> cannot be converted to ArrayList<Base>
Exercice3
L2DSI
44
Exercice4
package TD2;
import java.util.ArrayList;
class Noeud<E> {
E valeur;
Noeud<E> suivant;
1
class ListChainee <E> {
Noeud<E> premier;
public ListChainee() {
premier = null;
}
public void add(E val) {
Noeud<E> nouveau = new Noeud < E > (val);
if (premier == null) {
premier = nouveau;
} else {
Noeud<E> dernier = premier;
while (dernier.suivant != null) {
dernier = dernier.suivant;
}
dernier.suivant = nouveau;
}
}
public Noeud<E> get(int index) {
int i = 0;
Noeud<E> courant = premier;
while (courant.suivant != null && i < index) {
i++;
courant = courant.suivant; }
if (index == i)
return courant;
else return null; }
public String toString() {
String ch="";
Noeud<E> courant = premier;
while (courant.suivant != null ) {
2
ch+=courant.valeur+" ";
courant = courant.suivant;
}return ch+courant.valeur;
}
}
class TestListeChainée {
public static void main(String[] args) {
ListChainee <Integer> L = new ListChainee <Integer>();
L.add(5);L.add(7);
System.out.println(L.toString());
}
}
Exercice5
package td2_2021;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.TreeSet;
enum typeProduit {
normale,extrat
}
class Produit {
private int codeProd;
private typeProduit typeProduit;
private float prix;
3
this.codeProd = codeProd;
this.typeProduit = typeProduit;
this.prix = prix;
}
4
private int codeCom;
private HashSet<Produit> listeProd;
private Date datecomd;
5
return -1;
}
return 0;
} }
public class Stockage {
private int codeStockage;
private String ville;
private TreeSet<Commande> listeCommande;
6
l.remove(p);
} } return l;}
public int getCodeStockage() {
return codeStockage;
}
}