Size does matter vol 2
Czy rozmiar ma znaczenie?
- Co pojawi się na ekranie po uruchomieniu poniższego kodu?
public class SizeDoesMatterVol2 { public static void main(String[] args) { final List<Integer> elements = Arrays.asList(1, 2, 3); final List<Integer> listOfInts = new ArrayList<Integer>(elements); final Collection<Integer> collectionOfInts = new ArrayList<Integer>(elements); listOfInts.remove(0); collectionOfInts.remove(0); System.out.println(listOfInts.size() == collectionOfInts.size()); } }
Odpowiedzi:
(a) false
(b) true
(c) błąd kompilacji
(d) błąd uruchomienia (UnsupportedOperationException)
- Odpowiedź uzasadnij i wyjaśnij dlaczego tak to działa
co jest konkluzją?
To, że:
final List listOfInts = new ArrayList(elements);
będzie numerowany od 0…, a:
final Collection collectionOfInts = new ArrayList(elements);
będzie numerowany od 1?
Dla Collection wywoływana jest metoda remove(Object o), a dla Listy remove(int index).