name: portada class: portada-slide, center, middle # Arrays dinàmics ## List .footnote[Joan Puigcerver Ibáñez] --- layout: true class: regular-slide .right[.logo[]] --- # Introducció Intenta fer l'exercici següent - [AnotherInverseOrder](exercicis/08_arrays_dinamics.html)
--- # Arrays vs List - Els arrays tenen una mida fixe i això els fa molt limitats. - Arrays Dinàmics - Java: List - [List Javadoc](https://docs.oracle.com/javase/8/docs/api/java/util/List.html) --- # List Podem declarar llistes amb: ``` // llista buida List
list = new ArrayList
(); // llista amb valors List
stringList = new ArrayList
(Arrays.asList("Mar", "Iu", "Ot")); ``` Accedir a una posició de l'array amb: ``` int value = list.get(4); ``` --- # List Modificar a una posició de l'array amb: ``` // set(int index, E element) list.set(4, 6); ``` Consultat la llargada ``` list.size(); ``` For each ``` for(int value: list){ // do something } ``` --- # List Print ``` System.out.println(list); ``` --- # List - operacions dinàmiques Afegir un element ``` // al final list.add(15); // o a una posició // add(int index, E element) list.add(2, 15); ``` Eliminar elements ``` // l'element en una posició list.remove(5); // eliminar-los tots list.clear(); ``` ??? # Afegir els add all? boolean addAll(Collection extends E> c) Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator (optional operation). boolean addAll(int index, Collection extends E> c) # Per eliminar/afegir mentre s'itera: for(Iterator
it = stringList.iterator(); it.hasNext(); ){ String value = it.next(); //i.remove(); }