2012-11-24 65 views
1

我需要一些關於java的幫助,並返回一個數組的結果。試圖打印一個數組

我的數組列表存儲了來自餐廳的訂單(由訂單類創建)。 Deliverylog類(創建數組列表)然後有一個方法將訂單添加到名爲waitingList的數組列表中。

每個訂單都有藏漢它的屁股細節交貨時間等參考號碼..

什麼我嘗試做,但還沒有得到一個線索是,是創建一個參數的方法(INT ref)將在數組列表中搜索具有與輸入的相同參考號的項目。當它發現它時會返回訂單,否則返回null。

任何幫助appriciated。

代碼

/** 
* Show a order. 
* @param refnum The reference number of the order to be shown 
*/ 
public void findOrderWaiting(int refnum) 
    { 
    if(refnum < 0) { 
     // This is not a valid reference number 
    } 
    else if(refnum <= numberOfOrders()) { 
     // This is a valid reference number 
     System.out.println(waitingList.get(refnum)); 
    } 
    else { 
     // This is not a valid reference number 
    } 
    } 
+1

好的。那麼,你到現在爲止有什麼?向我們展示代碼。 –

+0

編寫代碼,發佈它,我們將一起討論它。 – Maroun

+0

看到您的代碼會更有趣.... – pbhd

回答

0

嗯,你可以通過數組循環使用標準的for循環:

for(int i=0; i<array.length; i++){ 
    if(array[i].intPart == ref) return array[i]; 
} 
return null; 

希望幫助!

+0

我是否在代碼之前執行此操作,或者不是代碼 –

1

你知道ArrayList中必須找到內的項目的方法:

//a is an arraylist 

    //filling the list here is omitted 

    //o is the object to find its index 
    int ind=a.indexOf(o); 

int indexOf(Object o)是已經發明瞭,所以你不要有做的方法。如果你想要這樣做的目的是爲了學習,你可以從互聯網搜索散列技術,但你使用的是arrayList,最簡單的就是indexOf()方法。

哈希表是給你自由,更靈活的搜索項目或一個鍵(你給鑰匙,並獲得項目或你給項目,並獲得其關鍵)(密鑰可以是一個對象呢!)

0
for(String s: restaurentOrders){ 
    if(s.equalsIgnoreCase()){ 
       //You have it.. 
    } 
} 

如果你正在將你的數組/ Arraylist存儲在restaurentOrders中。希望它有幫助