2016-03-27 1 views
0

tout le monde. Je dois faire un sandwich avec certains ingrédients et j'ai besoin d'itérer entre certains éléments de la liste chaînée pour insérer certaines garnitures. J'ai besoin d'aide sur les deux dernières parties de mon code où je dois insérer du bacon entre le poulet et la tomate. Pour une raison quelconque, le bacon apparaît à la toute fin entre le sel et le pain2. Toute aide serait grandement appréciée. Merci pour votre temps.Liste liée avec Iterator

/* 
Programmer: Yamin Mousselli 
Date: 03/27/2016 
Purpose: Demonstrate Use and Knowledge of LinkedList and Iterator. You CAN'T use an index number for inserting elements into 
linked list. You must only use the list iterator. Submit one java file only. 
*/ 
import java.util.List; 
import java.util.LinkedList; 
import java.util.ListIterator; 

public class LinkedListDemo { 
    public static void main(String[] args) 
    { 

    List<String> myLinkedList = new LinkedList<String>(); 

    String strOutput=""; 

    //BUILD THE SANDWICH 

    myLinkedList.add("Bread1"); 
    myLinkedList.add("mustard"); 
    myLinkedList.add("lettuce"); 
    myLinkedList.add("chicken"); 
    myLinkedList.add("tomato"); 
    myLinkedList.add("Bread2"); 

    ListIterator<String> lit = myLinkedList.listIterator(); 

    while(lit.hasNext()) 
    { 
     strOutput += (lit.next().toString() + ",") ; 
    } 
    strOutput +=("You have reached the end of the sandwich.\n"); 


    //SHOW THE CURRENT SANDWICH IN REVERSE USING "PREVIOUS()" METHOD 
    while(lit.hasPrevious()) 
    { 
     strOutput += (lit.previous().toString() + ","); 
    } 
    strOutput +=("You have reached the end of the sandwich.\n"); 


    //ADD PICKLES BETWEEN LETTUCE AND CHICKEN 
    while(lit.hasNext()) 
    { 
     if(lit.next().toString().equals("lettuce")) 
     { 
      lit.add("pickles"); 
      break; 
     } 
    } 

    while(lit.hasPrevious()) 
    { 
     lit.previous(); 
    } 

    while(lit.hasNext()) 
    { 
     strOutput += (lit.next().toString() + ",") ; 
    } 
    strOutput +=("You have reached the end of the sandwich.\n"); 


    //ADD CHEESE BETWEEN TOMATO AND BREAD2 
    while(lit.hasPrevious()) 
    { 
     if(lit.previous().toString().equals("Bread2")) 
     { 
      lit.add("cheese"); 
      break; 
     } 
    } 

    while(lit.hasPrevious()) 
    { 
     lit.previous(); 
    } 

    while(lit.hasNext()) 
    { 
     strOutput += (lit.next().toString() + ","); 
    } 
    strOutput += ("You have the reached the end of the sandwich.\n"); 


    //ADD SALT BETWEEN CHEESE AND BREAD2 
    while(lit.hasPrevious()) 
    { 
     if(lit.previous().toString().equals("Bread2")) 
     { 
      lit.add("salt"); 
      break; 
     } 
    } 

    while(lit.hasPrevious()) 
    { 
     lit.previous(); 
    } 

    while(lit.hasNext()) 
    { 
     strOutput += (lit.next().toString() + ","); 
    } 
    strOutput += ("You have the reached the end of the sandwich.\n"); 


    //GO BACKWARDS AND INSERT BACON BETWEEN CHICKEN AND TOMATO 
    while(lit.hasPrevious())  
    { 
     if(lit.previous().toString().equals("chicken")); 
     { 
      lit.add("bacon"); 
      break; 
     } 
    } 

    while(lit.hasPrevious()) 
    { 
     lit.previous(); 
    } 

    while(lit.hasNext()) 
    { 
     strOutput += (lit.next().toString() + ","); 
    } 
    strOutput += ("You have the reached the end of the sandwich.\n"); 


    //SHOW FINAL SANDWICH IN FORWARD ORDER 

    javax.swing.JOptionPane.showMessageDialog(null, strOutput); 
    System.exit(0); 
    } 

} 
+0

http://stackoverflow.com/a/19614083/2055998 –

+0

Seul problème est que je ne peux pas utiliser un index. –

+0

Avez-vous regardé la solution particulière à laquelle je suis lié? –

Répondre

0

Vous avez une faute de frappe:

if(lit.previous().toString().equals("chicken")); // <-- semi-colon 
    { 
     lit.add("bacon"); 
     break; 
    } 

EDIT:

Le point-virgule ferme au début de l'instruction if. Cela signifie que lit.add("bacon"); est toujours exécuté, quelle que soit l'instruction if.

+0

Ce sont toujours les choses stupides qui me touchent. Je vous remercie. Pour la dernière partie, comment imprimer la déclaration dans la boîte de dialogue? Je ne sais pas pourquoi la boîte de dialogue du message ne s'affiche pas. –

+0

J'ai aussi pris le javax.swing. dans la même ligne de JOptionPane.showMessageDialog(), à l'étage avec les importations (import javax.swing. *; –

+0

? J'ai utilisé une boucle améliorée mais cela n'a pas fonctionné, –