2016-01-21 66 views
0

嗨那裏我想從arrayList中輸出項目,如果用戶輸入匹配ArrayList中的itemNumber ..我正在使用和這些是重寫方法。ArrayList中的查找項目和輸出

實現的接口方法:

package Purchase; 

import java.util.*; 
public class Items implements recordItem { 

String description; 


ArrayList<Items> itemList = new ArrayList<Items>(); 
public static Items newItems = new Items(); 
+0

你能澄清你的問題嗎?你想替換**返回「不存在」; **然後將不存在的對象添加到列表中? –

+0

@Kiresays不,我的問題是,當我想讓它從ArrayList返回項目描述時,它會回覆「不存在」。 – BinaryBoy

回答

1

您的問題就在這裏:

this.itemNumber = itemNumber; 
this.description = description; 
this.unitPrice = unitPrice; 
this.sort = sort; 

Items theItems = new Items(); 
itemList.add(theItems); 

你要添加一個空的 「項目」 的清單。

這將工作:

Items theItems = new Items(); 

theItems.itemNumber = itemNumber; 
theItems.description = description; 
theItems.unitPrice = unitPrice; 
theItems.sort = sort; 

itemList.add(theItems); 

並請千萬不要用靜態實例像

public static Items newItems = new Items(); 
在你的實體類