[java]acceder au contenu d'une arraylist
Fermé
hamza
-
22 nov. 2008 à 23:50
choubaka Messages postés 39153 Date d'inscription jeudi 4 avril 2002 Statut Modérateur Dernière intervention 24 mars 2023 - 23 nov. 2008 à 11:04
choubaka Messages postés 39153 Date d'inscription jeudi 4 avril 2002 Statut Modérateur Dernière intervention 24 mars 2023 - 23 nov. 2008 à 11:04
A voir également:
- [java]acceder au contenu d'une arraylist
- Acceder au bios - Guide
- [<B>contenu masqué</b>] ✓ - Forum Android
- Jeux java itel ✓ - Forum Jeux vidéo
- Telecharger java - Télécharger - Langages
- Java apk - Télécharger - Langages
5 réponses
choubaka
Messages postés
39153
Date d'inscription
jeudi 4 avril 2002
Statut
Modérateur
Dernière intervention
24 mars 2023
2 099
23 nov. 2008 à 11:04
23 nov. 2008 à 11:04
salut
un autre exemple
un autre exemple
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class MainClass {
public static void main(String[] args) {
List cats = new ArrayList();
for (int i = 0; i < 7; i++)
cats.add(new Cat(i));
Iterator e = cats.iterator();
while (e.hasNext())
((Cat) e.next()).id();
}
}
class Cat {
private int catNumber;
public Cat(int i) {
catNumber = i;
}
public void id() {
System.out.println("Cat #" + catNumber);
}
}
choubaka
Messages postés
39153
Date d'inscription
jeudi 4 avril 2002
Statut
Modérateur
Dernière intervention
24 mars 2023
2 099
22 nov. 2008 à 23:56
22 nov. 2008 à 23:56
Salut
pour récupérer un objet contenu dans une arraylist, tu dois connaître le type de l'objet.
dans une boucle
retrouver l'index, faire un "cast" de l'objet sur l'index et le tour est joué
l'idéal est d'utiliser l'"iterator"
http://java.sun.com/j2se/1.3/docs/api/java/util/ArrayList.html
pour récupérer un objet contenu dans une arraylist, tu dois connaître le type de l'objet.
dans une boucle
retrouver l'index, faire un "cast" de l'objet sur l'index et le tour est joué
l'idéal est d'utiliser l'"iterator"
http://java.sun.com/j2se/1.3/docs/api/java/util/ArrayList.html
g pa compri exactement ce que tu m'as proposé.
j'ai une classe fils:
public class fils {
public String s;
public int i;
public String racine;
public int h;
public fils(){
this.s = " ";
this.i = 0;
this.racine=" ";
}
public fils(String s, int i,String r)
{
this.s = s;
this.i = i;
this.racine=r;
}
public String get_fils(){
return s;
}
public int get_h(){
return i;
}
public String get_racine(){
return racine;
}
public void setRacine(String racine)
{
this.racine = racine;
}
public void setH (int h)
{
this.h = h;
}
}
et j'ai une autre classe h
{.....//
public fils noeud(int i,int j,String s,String but ){
String v= new String();
int h=0;
v= permut(i,j,s);
h=distance_manhaten(chaine_v_matrive(v),chaine_v_matrive(but));
fils f = new fils(v,h,s);
return f;
}
public ArrayList generer_fils(int z, String w,String but){
vfils.clear();
switch (z)
{
case 0:
vfils.add(noeud(0, 1, w, but));
vfils.add(noeud(0, 3, w, but));
break;
case 1:
vfils.add(noeud(1, 0, w, but));
vfils.add(noeud(1, 2, w, but));
vfils.add(noeud(1, 4, w, but));
break;
case 2:
vfils.add(noeud(2, 5, w, but));
vfils.add(noeud(2, 1, w, but));
break;
default:System.out.println("erreur");
}
return vfils;
}
je veux afficher le contenu de vfils comment faire ca ?
j'ai une classe fils:
public class fils {
public String s;
public int i;
public String racine;
public int h;
public fils(){
this.s = " ";
this.i = 0;
this.racine=" ";
}
public fils(String s, int i,String r)
{
this.s = s;
this.i = i;
this.racine=r;
}
public String get_fils(){
return s;
}
public int get_h(){
return i;
}
public String get_racine(){
return racine;
}
public void setRacine(String racine)
{
this.racine = racine;
}
public void setH (int h)
{
this.h = h;
}
}
et j'ai une autre classe h
{.....//
public fils noeud(int i,int j,String s,String but ){
String v= new String();
int h=0;
v= permut(i,j,s);
h=distance_manhaten(chaine_v_matrive(v),chaine_v_matrive(but));
fils f = new fils(v,h,s);
return f;
}
public ArrayList generer_fils(int z, String w,String but){
vfils.clear();
switch (z)
{
case 0:
vfils.add(noeud(0, 1, w, but));
vfils.add(noeud(0, 3, w, but));
break;
case 1:
vfils.add(noeud(1, 0, w, but));
vfils.add(noeud(1, 2, w, but));
vfils.add(noeud(1, 4, w, but));
break;
case 2:
vfils.add(noeud(2, 5, w, but));
vfils.add(noeud(2, 1, w, but));
break;
default:System.out.println("erreur");
}
return vfils;
}
je veux afficher le contenu de vfils comment faire ca ?
choubaka
Messages postés
39153
Date d'inscription
jeudi 4 avril 2002
Statut
Modérateur
Dernière intervention
24 mars 2023
2 099
23 nov. 2008 à 00:20
23 nov. 2008 à 00:20
en gros.
ArrayList array = new ArrayList();
Iterator it = array.iterator();
while (it.hasNext());
Object o = (Object) it.next();
il faut aussi tester le fait que ton ArrayList ne soit pas vide avec la méthode isEmpty() qui te renvoie un booléen.
ArrayList array = new ArrayList();
Iterator it = array.iterator();
while (it.hasNext());
Object o = (Object) it.next();
il faut aussi tester le fait que ton ArrayList ne soit pas vide avec la méthode isEmpty() qui te renvoie un booléen.
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question