[英]Add specific item from ArrayList into LinkedList Java
我有一個包含很多對象的ArrayList
。 我希望能夠將任何對象添加到3個不同LinkedLists
的選擇中。 用戶輸入將通過鍵入他們希望添加的索引來選擇要添加到哪個LinkedList的項目。 這是我所追求的,但是我無法使其正常工作:
public void addToRepository(int indx, int r) {
if (r == 1){ //checks to see if chosen repository 1
for (int i=0; i < itemList.size(); i++) {
i = indx;
} // ignore this it was just me playing around to see if i'd be able to get the index this way..
repo1.add(itemList.get(indx)); //adds item from "itemList" with the index that
the user input has given
//other if statements
}
}
我不確定這是正確的主意,但會出現錯誤“無法將項目轉換為字符串”。 如果沒有,我該怎么做?
所以你有了
ArrayList<Item> itemList = new ArrayList<Item>();
而您正在嘗試-
repo1.add(itemList.get(indx));
根據異常,您得到的好像repo1
具有String數據。 您可以執行以下其中一項操作-
repo1.add(itemList.get(indx).toString());
要么 repo1
泛型以包括Item
數據而不是String
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.