Les Tableaux | ArrayLists |
---|---|
accès aléatoire, structure de donnée linéaire | accès aléatoire, structure de donnée linéaire |
taille fixée | taille dynamique |
peut contenir des objets et des primitives | ne peut contenir que des objets |
il faut déclarer le type de l'élément | l'élément est de type Object jdk1.5: Avec une template on peut indiquer un type |
sécuritaire: verification des limites ("bounds checking") à temps d'execution | sécuritaire: verification des limites ("bounds checking") à temps d'execution |
Fish[] myArray = new Fish[15]; | ArrayList myList = new ArrayList(); jdk1.5: ArrayList myList15<Fish> = new ArrayList<Fish>(); |
myArray[index] = new Fish(loc); | myList.add(new Fish(loc)); |
myList.set(index, new Fish(loc)); | |
Fish f = myArray[index]; | Fish f = (Fish) myList.get(index); jdk1.5: Fish f = myList15.get(index); |
myArray[index].move(); | ((Fish) myList.get(index)).move(); jdk1.5: myList15.get(index).move(); |
for ( int k = 0; k < myArray.length; k++ ) System.out.println(myArray[index]); |
for ( int k = 0; k < myList.size(); k++ ) System.out.println(myList.get(k)); Iterator: for (Iterator i=myList.iterator();i.hasNext()) System.out.println(i.next()); |
Méthodes: | |
boolean add(Object obj) | |
void add(int index, Object obj) | |
Object get(int index) | |
Object set(int index, Object obj) | |
Object remove(int index) | |
int size() | |
Iterator iterator() |
import java.util.ArrayList; class Cloning { public int val=0; public ArrayList<Integer> al; Cloning(int val) { this.val=val; this.al=new ArrayList<Integer>(val); for (int i=0;i<val;i++) { al.add(new Integer(i)); } } public String toString() { return val + ":" + al.toString(); } public void increment() { for (int i=0;i<val;i++) { al.set(i,new Integer(al.get(i) + 1)); } val++; } public static void main(String [] args) { Cloning c = new Cloning(3); Cloning d=c; System.out.println("Valeur de c avant "+c); System.out.println("Valeur de d avant "+d); c.increment(); System.out.println("Valeur de c apres "+c); System.out.println("Valeur de d apres "+d); System.out.println(""); System.out.println(""); } }
import java.util.ArrayList; class Cloning2 implements Cloneable{ public int val=0; public ArrayList<Integer> al; Cloning2(int val) { this.val=val; this.al=new ArrayList<Integer>(val); for (int i=0;i<val;i++) { al.add(new Integer(i)); } } public String toString() { return val + ":" + al.toString(); } public void increment() { for (int i=0;i<val;i++) { al.set(i,new Integer(al.get(i) + 1)); } val++; } public Object clone() { try { return super.clone(); } catch (CloneNotSupportedException e) { throw new Error("This should not happen since we implemented Cloneable"); } } public static void main(String [] args) { Cloning2 c = new Cloning2(3); Cloning2 d=(Cloning2)c.clone(); System.out.println("Valeur de c avant "+c); System.out.println("Valeur de d avant "+d); c.increment(); System.out.println("Valeur de c apres "+c); System.out.println("Valeur de d apres "+d); System.out.println(""); } }
import java.util.ArrayList; class Cloning3 implements Cloneable{ private int val=0; private ArrayList<Integer> al; Cloning3(int val) { this.val=val; this.al=new ArrayList<Integer>(val); for (int i=0;i<val;i++) { al.add(new Integer(i)); } } public String toString() { return val + ":" + al.toString(); } public void increment() { for (int i=0;i<val;i++) { al.set(i,new Integer(al.get(i) + 1)); } val++; } protected Object clone() { try { Cloning3 nc = (Cloning3)super.clone(); nc.al=(ArrayList<Integer>)(al.clone()); return nc; } catch (CloneNotSupportedException e) { throw new Error("This should not happen since we implemented Cloneable"); } } public static void main(String [] args) { Cloning3 c = new Cloning3(3); Cloning3 d=(Cloning3)c.clone(); System.out.println("Valeur de c avant "+c); System.out.println("Valeur de d avant "+d); c.increment(); System.out.println("Valeur de c apres "+c); System.out.println("Valeur de d apres "+d); System.out.println(""); } }
import java.util.ArrayList; class Equals implements Cloneable{ private String s; int x; Equals(String s, int x) { this.s=s; this.x=x; } protected Object clone() { try { Equals e = (Equals)super.clone(); e.s=new String(s); return e; } catch (CloneNotSupportedException e) { throw new Error("This should not happen since we implemented Cloneable"); } } public static void main(String [] args) { Equals e = new Equals("Bonjour",12); Equals f = (Equals)e.clone(); System.out.println("f: " + e); System.out.println("f: " + f); System.out.println("e==f? " + e.equals(f)); System.out.println(""); } public String toString() { return x + " " + s; } }
import java.util.ArrayList; class Equals2 implements Cloneable{ private String s; int x; Equals2(String s, int x) { this.s=s; this.x=x; } protected Object clone() { try { Equals2 e = (Equals2)super.clone(); e.s=new String(s); return e; } catch (CloneNotSupportedException e) { throw new Error("This should not happen since we implemented Cloneable"); } } public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Equals2) ) return false; Equals2 e2 = (Equals2) o; return (x==e2.x && s.equals(e2.s)); } public static void main(String [] args) { Equals2 e = new Equals2("Bonjour",12); Equals2 f = (Equals2)e.clone(); System.out.println("f: " + e); System.out.println("f: " + f); System.out.println("e==f? " + e.equals(f)); System.out.println(""); } public String toString() { return x + " " + s; } }
import java.util.ArrayList; class Hashing implements Cloneable{ private String s; int x; Hashing(String s, int x) { this.s=s; this.x=x; } protected Object clone() { try { Hashing e = (Hashing)super.clone(); e.s=new String(s); return e; } catch (CloneNotSupportedException e) { throw new Error("This should not happen since we implemented Cloneable"); } } public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Hashing) ) return false; Hashing e2 = (Hashing) o; return (x==e2.x && s.equals(e2.s)); } public static void main(String [] args) { Hashing e = new Hashing("Bonjour",12); Hashing f = (Hashing)e.clone(); System.out.println("f: " + e.hashCode()); System.out.println("f: " + f.hashCode()); System.out.println("e==f? " + e.equals(f)); System.out.println(""); } public String toString() { return x + " " + s; } }
import java.util.ArrayList; class Hashing2 implements Cloneable{ private String s; int x; Hashing2(String s, int x) { this.s=s; this.x=x; } protected Object clone() { try { Hashing2 e = (Hashing2)super.clone(); e.s=new String(s); return e; } catch (CloneNotSupportedException e) { throw new Error("This should not happen since we implemented Cloneable"); } } public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Hashing2) ) return false; Hashing2 e2 = (Hashing2) o; return (x==e2.x && s.equals(e2.s)); } public int hashCode() { return x*5 + s.hashCode()*7; } public static void main(String [] args) { Hashing2 e = new Hashing2("Bonjour",12); Hashing2 f = (Hashing2)e.clone(); System.out.println("f: " + e.hashCode()); System.out.println("f: " + f.hashCode()); System.out.println("e==f? " + e.equals(f)); System.out.println(""); } public String toString() { return x + " " + s; } }
import java.util.ArrayList; import java.util.Iterator; class Boucles { public static void main(String [] args) { //ints int [] x = {0,1,2,3,4,5,6,7,8,9}; for (int i=0;i<x.length;i++) { System.out.print(i+" "); } System.out.println(""); for (int i : x) { System.out.print(i+" "); } System.out.println(""); //Integers ArrayList<Integer> alInt = new ArrayList<Integer>(); for (int i=0;i<10;i++) { alInt.add(new Integer(i)); } for (Integer i : alInt) { System.out.print(i + " "); } System.out.println(""); for (Iterator i = alInt.iterator();i.hasNext();) { System.out.print(i.next() + " "); } System.out.println(""); } }