everyone。特定の食材を使ってサンドイッチを作る必要があり、特定のトッピングを挿入するにはリンクされたリストのいくつかの要素の間を反復する必要があります。チキンとトマトの間にベーコンを入れなければならないコードの最後の2つの部分について助けが必要です。何らかの理由で、ベーコンは塩とBread2の間の最後に現れています。どんな助けでも大歓迎です。あなたの時間をありがとう。リンクされたリストとイテレータ
/*
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);
}
}
http://stackoverflow.com/a/19614083/2055998 –
インデックスのみを使用することはできません。 –
リンク先の特定のソリューションを見ましたか? –