繁体   English   中英

将ArrayList中的特定项添加到LinkedList Java中

[英]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数据。 您可以执行以下其中一项操作-

  1. 使用repo1.add(itemList.get(indx).toString()); 要么
  2. 更改repo1泛型以包括Item数据而不是String

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM