0

現在の find メソッドはうまく機能しますが、1 つのノードに対してのみです。たとえば、find("London") と条件を満たすノードをさらに試してみると、最初のノードのみが返されます。条件を満たすすべてのノードを返すには、何を変更する必要がありますか?

public class LinkList {

public Link first; // ref to first link on list

public LinkList() // constructor
{
    first = null; // no links on list yet
}

 public void insert(Object Airplanefnumber,Object Airplanedest, Object Airplaneline, Object Airplaneplane, Object Airplanetime, Object Terminal, Object Parkingslot)
{ // make new link
    Link newLink = new Link(Airplanefnumber, Airplanedest, Airplaneline, Airplaneplane, Airplanetime, Terminal, Parkingslot);
    newLink.next = first; // it points to old first link
    first = newLink; // now first points to this

}
public void insertqueue(Object Airplanefnumber,Object Airplanedest, Object Airplaneline, Object Airplaneplane, Object Airplanetime, Object Terminal, Object Parkingslot, Object Runway)
{ // make new link
    LinkQueue newLink = new LinkQueue(Airplanefnumber, Airplanedest, Airplaneline, Airplaneplane, Airplanetime, Terminal, Parkingslot, Runway);
   // newLink.next = first; // it points to old first link
   // first = newLink; // now first points to this
}


public Link find(String key) // find link with given key
{ // (assumes non-empty list)
    Link current = first; // start at FIRST

    while( !current.Airplanedest.equals(key)) // while no match,
    {

     if(current.next == null) // if end of list,
            return null; // did not find it
        else // not end of list,
            current = current.next; // go to next link

}
return current;
}





public Link findnumber(int key) // find link with given key
{ // (assumes non-empty list)
    Link current = first; // start at FIRST
    while(!(current.Airplanefnumber.equals(key))) // while no match,
    {
        if(current.next == null) // if end of list,
            return null; // did not find it
        else // not end of list,
            current = current.next; // go to next link
    }
    return current; // found it



}




public Link delete(int key) // delete link with given key
{ // (assumes non-empty list)
    Link current = first; // search for link
    Link previous = first;

    while(!(current.Airplanefnumber).equals(key))
    {
        if(current.next == null)
            return null; // did not find it
        else
        {
            previous = current; // go to next link

        }
        current = current.next;
    } // found it

    if(current == first) // if first link,
        first = first.next; // change first
    else // otherwise,
        previous.next = current.next; // bypass it
    return current;
}


public void displayList() // display the list
{

    System.out.println();
    Link current = first; // start at beginning of list
    while(current != null) // until end of list,
    {
        current.displayLink(); // print data

        current = current.next; // move to next link
    }
   // System.out.println("Flight Number is: "+ flightnumber +"\n"+"Flight destination is:"+ destination +"\n"+ "Airline: "+ airline +"\n"+"Airplane: "+ airplane +"\n"+"Schedule time: "+ time);

    System.out.println("");
}

 public void peekFirst()  
 {  
   System.out.println(first.Airplanefnumber + "\n" +first.Airplanedest + "\n" + first.Airplaneline + "\n" + first.Airplaneplane + "\n" +  first.Airplanetime + "\n" + first.Terminal + "\n" + first.Parkingslot);
} 


public boolean isEmpty()  
{  
  return(first==null);  
}  
} //end of LinkList class


  //code from main

  Link f = null;
  System.out.println("Enter flight destination");
  String dest = input.nextLine();
  System.out.println("You entered destination "+ dest);
  f = Planes.find(dest);
  if( f != null){
    System.out.println("Flight number: "+f.Airplanefnumber +"\n"+"Flight destination is:"+f.Airplanedest+"\n"+"Airline: "+f.Airplaneline+"\n"+"Airplane type: "+f.Airplaneplane+"\n"+"Scheduled time: "+f.Airplanetime + "\n" + "Terminal nr. " + f.Terminal +"\n" + "Parking slot: " + f.Parkingslot);}
        else{
       System.out.println("Cannot find flight");
  }
4

1 に答える 1

2

find メソッドの while ループは、一致が見つかると停止します。あなたが求めていることを行う適切な方法は次のとおりです。

  1. 結果を保存するオブジェクトを作成します
  2. 結果を見つけるたびに、今のようにすぐに返すのではなく、一時ストレージに置きます
  3. リストの最後に到達したら、ストレージ オブジェクトを返します。空の場合、結果はありませんでした
于 2013-11-03T11:08:55.100 に答える