带迭代器的java链表

mzmfm0qo  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(298)

所有人。我必须做一个三明治与某些成分,并需要迭代之间的一些元素的链表插入某些配料。我需要一些帮助,在我的代码的最后两部分,我必须插入鸡肉和番茄之间的培根。不知为什么,咸肉出现在盐和面包之间的最末端。任何帮助都将不胜感激。谢谢你抽出时间。

/*
 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);
  }

}
68bkxrlz

68bkxrlz1#

您输入了一个拼写错误:

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

编辑:
分号提前结束if语句。这意味着 lit.add("bacon"); 始终运行,无论if语句如何。

相关问题