Rappel : Pour utiliser le type ArrayList, il faut importer les classes ie définissant ce type, au moyen de la directive :
import java.util.ArrayList
En vous aidant si nécessaire d'un programme, répondez aux questions suivantes :
A : Quelles valeurs contient le tableau tab après l'exécution du programme suivant? Expliquez.
1 2 3 4 5 6 7 8 9 10 11 12 |
import java.util.ArrayList;
class DynamicArray1 {
public static void main(String[] args) {
final int TAILLE = 10;
ArrayList<Integer> tab = new ArrayList<Integer>();
for (int i = 0; i < TAILLE; ++i) {
tab.add(tab.size());
}
}
} |
B : Quelles valeurs contient le tableau tab2 après l'exécution du programme suivant? Expliquez.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.ArrayList;
class DynamicArray2 {
public static void main(String[] args) {
ArrayList<Integer> tab1 = new ArrayList<Integer>();
tab1.add(99);
tab1.add(1);
tab1.add(0);
ArrayList<Integer> tab2 = new ArrayList<Integer>();
for(int i=0; i < tab1.size(); ++i) {
tab2.add(tab1.get(0));
}
}
} |