2011-11-17 11 views
6

In pratica, l'utente invia una stringa su cui Iterator cerca una lista array. Quando viene trovato, Iterator cancellerà l'oggetto contenente la stringa.Java, Utilizzo di Iterator per cercare una lista array ed eliminare gli oggetti corrispondenti

Poiché ciascuno di questi oggetti contiene due stringhe, sto riscontrando problemi nel scrivere queste righe come una sola.

Friend current = it.next(); 
String currently = current.getFriendCaption(); 

Grazie per qualsiasi aiuto!

+3

Ho paura che la domanda non abbia molto senso. Perché hai bisogno di scrivere quelle righe come una? –

+1

è aiutarmi, grazie .. ^^ –

risposta

34

Tu non li serve su una sola riga, basta usare remove per rimuovere un elemento quando corrisponde:

Iterator<Friend> it = list.iterator(); 
while (it.hasNext()) { 
    if (it.next().getFriendCaption().equals(targetCaption)) { 
     it.remove(); 
     // If you know it's unique, you could `break;` here 
    } 
} 

demo completa:

import java.util.*; 

public class ListExample { 
    public static final void main(String[] args) { 
     List<Friend> list = new ArrayList<Friend>(5); 
     String   targetCaption = "match"; 

     list.add(new Friend("match")); 
     list.add(new Friend("non-match")); 
     list.add(new Friend("match")); 
     list.add(new Friend("non-match")); 
     list.add(new Friend("match")); 

     System.out.println("Before:"); 
     for (Friend f : list) { 
      System.out.println(f.getFriendCaption()); 
     } 

     Iterator<Friend> it = list.iterator(); 
     while (it.hasNext()) { 
      if (it.next().getFriendCaption().equals(targetCaption)) { 
       it.remove(); 
       // If you know it's unique, you could `break;` here 
      } 
     } 

     System.out.println(); 
     System.out.println("After:"); 
     for (Friend f : list) { 
      System.out.println(f.getFriendCaption()); 
     } 

     System.exit(0); 
    } 

    private static class Friend { 
     private String friendCaption; 

     public Friend(String fc) { 
      this.friendCaption = fc; 
     } 

     public String getFriendCaption() { 
      return this.friendCaption; 
     } 

    } 
} 

uscita:

$ java ListExample 
Before: 
match 
non-match 
match 
non-match 
match 

After: 
non-match 
non-match
+0

ho capito la tua risposta e grazie molto, il problema è che quando si digita 'if (it.next(). Contiene (testo)) {' E doesn' lavoro? Ho bisogno di cercare solo una certa parte (sottotitoli String) di ogni oggetto in ArrayList. – Nayrdesign

+0

@Nayrdesign: assicurati di dichiarare correttamente 'Iterator' e che stai trattando ciò che ritorna correttamente. Per esempio, il tuo esempio ci 'if (it.next(). Contiene (testo)) {' si comporta come 'Iterator' sta iterando su stringhe, ma la tua domanda fa sembrare che' ArrayList' contenga oggetti 'Amico' non stringhe. La demo completa mostra come farlo correttamente. Il bit chiave sta dichiarando 'Iterator ' quindi 'Iterator' sta iterando su istanze di' Friend', in modo che 'it.next()' sia un 'Friend', quindi si può fare' if (it.next(). getFriendCaption(). contiene (testo)) {'. –

+0

@TJCrowder ho modellato il mio programma esattamente dopo la tua, ma io sono sempre: Exception in thread java.util.NoSuchElementException "principale" \t a java.util.AbstractList $ Itr.next (AbstractList.java:350) \t a randomInt .messAroundWithListAgain (RandomInt.java:74) \t a RandomInt.main (RandomInt.java:85) – hologram

Problemi correlati