0
/* INPUT
Greenland
Denmark
Iceland
Finland
Sweden
Norway
*/

这部分返回所需的一切。除了最后一个,我都得到了。

public void insert(Country item){
      System.out.println("Item receieved by pQueue: " + item.getCountryName());
      int j;
      if (nItems==0)
          pQueArray[nItems++] = item;
      else{
         for (j=nItems-1; j>=0; --j)
            if (item.getCountryName().compareTo(pQueArray[j].getCountryName()) < 0 )
               pQueArray[j+1] = pQueArray[j];
            else
               break;
         // end for
         pQueArray[j+1] = item;                                          
         nItems++;  
      }  // end else
}  // end insert()

它没有正确返回最后一个项目,我不知道为什么?

/* OUTPUT
Denmark
Finland
Greenland
Iceland``
Sweden
Norway */
4

1 回答 1

0

您的问题在于:

pQueArray[j+1] = item;

这表明您正在将最新项目添加到队列的最后一个位置,只有在您完成处理以比较其余元素之后。

于 2013-05-30T00:29:40.833 回答