[PDF] [PDF] Collections Collections Collections javautilArrayList

paquetage java util Peter Sander ESSI-Université de Nice Sophia Antipolis 4 java util ArrayList ❍ Solution il contient plusieurs objets (de la classe Object



Previous PDF Next PDF





[PDF] Collections Collections Collections javautilArrayList

paquetage java util Peter Sander ESSI-Université de Nice Sophia Antipolis 4 java util ArrayList ❍ Solution il contient plusieurs objets (de la classe Object



[PDF] ArrayList

An ArrayList is an object that can store a group of other objects for us and allow us to manipulate those objects one by one For example, we could use an ArrayList to store all the String names of the pizza toppings offered by a restaurant, or we could store all the URLs that make up a user's favorite sites



[PDF] Java : les collections

public class Main { public static void main(String[] args) { ArrayList liste = new ArrayList(); liste add(0); liste add(1); liste add(2); liste add(3); for(Object elt: liste) {



[PDF] ArrayList - GitHub Pages

List and ArrayList List is a basic data type (not a class) in Java, List is an interface, not a class you cannot create "List" objects ArrayList is a class that 



[PDF] ArrayLists, Generics A data structure is a software construct used to

The ArrayList class provided by Java is essentially a way to create an array that can class Integer, which is a wrapper class for objects of type int (or we could 



[PDF] Chapter 14 Introduction to Generics The ArrayList Class The

java util • An ArrayList is created and named in the same way as object of any class, except that objects of the base type String with an initial capacity of 20



[PDF] Why ArrayList is not a subclass of ArrayList

Since the beginnings of Java, Integer[] has been a subclass of Object[] Thus, the following code is syntactically correct and will compile Integer[] b= new Integer[2 ];



[PDF] Arrays and ArrayLists

the values/objects that they store • Arrays are each array object stores a public final int ArrayLists java util Class ArrayList java lang Object java util



Collections of Objects

individualized reference variables for these objects: Students s1, s2, s3, perhaps, ArrayList x; // ArrayList is one of Java's predefined collection types



[PDF] Lists -- How to Build Your Own

Atul Prakash Reference: Look at ArrayList java implementation in Java Object get(int index) • remove Object[] data; // list itself null values at the end

[PDF] arraylist programming questions in java

[PDF] arraylist starts with 0 or 1

[PDF] arrays and do loops

[PDF] arrays and objects are examples of the primitive data type php

[PDF] arrays are allocated with the keyword

[PDF] arrays are everywhere

[PDF] arrays are immutable

[PDF] arrays are objects in java

[PDF] arrays are passed by reference

[PDF] arrays are passed to a method by ____

[PDF] arrays are quizlet

[PDF] arrays can store references to the objects of a class.

[PDF] arrays in c

[PDF] arrays in data structure notes

[PDF] arrays in math

CollectionsCollections

Karima BoudaoudKarima Boudaoud

IUT- R&T

IUT- R&TPeter Sander ESSI-Université de Nice Sophia

Antipolis2CollectionsCollections

Définition

une collection est un objet qui contient d'autres objets exemple: un tableau est une collection

Classes & interfacesAbstractCollection

ArrayList

Arrays

Collections

HashSet

LinkedList

TreeSet

Vector

List Map Set

SortedMap

SortedSet

Package

Ces classes et interfaces se trouvent dans le

paquetage java.util

Peter Sander ESSI-Université de Nice Sophia

Antipolis3CollectionsCollections

Problème

les tableaux ne répondent pas toujours à tous les besoins quand un nombre inconnu d'objets sont à stocker... on pourrait créer un très grand tableau, maisce n'est pas très " propre » ce n'est jamais assez grand !

Peter Sander ESSI-Université de Nice Sophia

Solution

la classe java.util.ArrayListjava.util.ArrayList c'est la classe la plus utilisée un ArrayListArrayList se comporte comme un tableau il contient plusieurs objets (de la classe Object uniquement) ne peut contenir des types primitifs il accède à ses éléments à l'aide d'un index il grossit automatiquement quand plus de place pour contenir de nouveaux objets il existe des méthodes pour ajouter ou enlever un élément

Peter Sander ESSI-Université de Nice Sophia

Antipolis5

ArrayListArrayList

Création d'un ArrayList

il est possible d'indiquer la taille initiale dans le constructeur

Il y a 2 constructeurs :

ArrayList()

ArrayList(int initialCapacity)

Peter Sander ESSI-Université de Nice Sophia

Antipolis6

ArrayListArrayList

Modification d'éléments

Il y a deux manières d'ajouter un élémentà la fin d'un ArrayList avec la méthode boolean add(Object newElement)

à une position donnée

void add(int index, Object newElement)

throws IndexOutOfBoundsExceptionle paramètre index indique où insérer le nouvel élément

si position incorrecte, une exception est levée

Peter Sander ESSI-Université de Nice Sophia

Antipolis7

ArrayListArrayList

Modification d'éléments

pour remplacer un objet à une position donnée

Object set(int index, Object newElement)

throws IndexOutOfBoundsException cette méthode fonctionne comme void add(int index, Object newElement) sauf que l'élément à la position index est remplacé

Peter Sander ESSI-Université de Nice Sophia

Antipolis8

ArrayListArrayList

Accès aux Éléments

pour accéder à un élémentil n'y a pas d'indexation comme pour les tableaux il faut utiliser la méthode spécialisée Object get(int index) throws IndexOutOfBoundsException exemple :

ArrayList aList = new ArrayList();

aList.add(new PacMan()); aList[0].display(); // interdit ! aList.get(0).display(); // ok

Peter Sander ESSI-Université de Nice Sophia

Antipolis9

Accès aux Éléments

pour tester le contenu, il existe la méthode boolean isEmpty() pour connaître le nombre d'éléments dans la liste, il faut utiliser la méthode : int size() exemple : if (!aList.isEmpty()) { for (int i=0; iArrayListArrayList

Peter Sander ESSI-Université de Nice Sophia

Antipolis10

ArrayListArrayList

Recopie

pour recopier une liste dans un tableau, il faut utiliser la méthode

Object[ ] toArray()

exemple :

ArrayList aList = new ArrayList();

Object[] tab = new Object[aList.size()];

tab = aList.toArray();

Peter Sander ESSI-Université de Nice Sophia

Antipolis11

ArrayListArrayList

Recherche d'éléments

pour savoir si un objet est présent ou non dans une liste, il faut utiliser la méthode boolean contains(Object obj) pour connaître la position d'un élément dans une liste, on peut utiliser deux méthodes pour avoir la première occurrence, il faut utiliser int indexOf(Object obj) pour avoir la dernière occurrence, il faut utiliser int lastIndexOf(Object obj)

Peter Sander ESSI-Université de Nice Sophia

Antipolis12

ArrayListArrayList

Suppression d'éléments

Pour supprimer un élément à une position

donnée, il faut utiliser la méthode

Object remove(int index)

throws IndexOutOfBoundsException

Peter Sander ESSI-Université de Nice Sophia

Antipolis13

CollectionsCollections

Autre classe

il existe une autre classe qui est aussi très utile : java.util.Vector voir le package java.util.Vector pour connaître les différences avec ArrayList

Karima Boudaoud IUT GTR - Sophia Antipolis 14

RésuméRésumé

boolean add(Object obj) void add(int indice, Object obj) boolean contains(Object obj)

Object get(int indice)

int indexOf(Object obj) int lastIndexOf(Object obj) void remove(int indice) void set(indice, Object obj) int size()

Karima Boudaoud IUT GTR - Sophia Antipolis 15

ExempleExemple

public class Employe { private String leNom, lePrenom; private double leSalaire public Employe (String unNom, String unPrenom) { leNom = unNom; lePrenom = unPrenom; public Employe (String unNom, String unPrenom, double unSalaire) { leNom = unNom; lePrenom = unPrenom; leSalaire = unSalaire; public String getNom() return leNom;

Karima Boudaoud IUT GTR - Sophia Antipolis 16

ExempleExemple

public static void main(String [] args) {

ArrayList tableauEmployes = new ArrayList();

Employe emp1 = new Employe("Charles", "McCathieNevile");

Employe emp2 = new Employe("Peter", "Sander");

tableauEmployes.add(emp1); tableauEmployes.add(emp2);

If (!tableauEmployes.isEmpty()) {

for (int i = 0; i