简体   繁体   English

将值从一个Arraylist复制到另一个时,所有值均设置为在第一个Arraylist中输入的最后一个值

[英]When copying the values from one Arraylist to another, all values get set as the last value entered in the first Arraylist

I am having some issue writing a small program for myself. 我在为自己编写一个小程序时遇到问题。 I have a class that will read from a CSV and put the data into arraylist of the class Farts (this is short for fighting arts before you ask). 我有一个可以从CSV读取并将数据放入Farts类的arraylist的类(在您问之前,这是搏击艺术的简称)。 This works, when I test my output I can see the values read in from the csv. 这有效,当我测试输出时,我可以看到从csv读取的值。

 AssetManager assetManager =context.getAssets();
    ArrayList<Farts> arrayfarts = new ArrayList<Farts>();
    InputStream csvStream = null;
    System.out.println("right before try loop");

    try {
        csvStream = assetManager.open("farts.txt");
        InputStreamReader csvStreamReader = new InputStreamReader(csvStream);
        CSVReader csvreader = new CSVReader(csvStreamReader);
        Farts fdata = new Farts();
        String[] line;
        int temp=0;
        while ((line=csvreader.readNext())!=null){
            System.out.println("inside of while");
            fdata.fname=line[0];
            fdata.description=line[1];
            arrayfarts.add(temp,fdata);
            System.out.println("Array iteration " + temp + " of " + arrayfarts.size());
            System.out.println(arrayfarts.get(temp).fname + " " + arrayfarts.get(temp).description + "\n");
            temp++;
        }

    } catch (IOException e) {
        e.printStackTrace();
        System.out.println("ioexception"+e.toString());
    }
    return arrayfarts;
}

Back in my Main class I then use .addall to add what is returned from above. 回到我的Main类中,然后使用.addall添加上面返回的内容。 However the data in the Arraylist is populated by only the last value entered into it. 但是,Arraylist中的数据仅由最后输入的值填充。

arrayFarts.addAll(readcsv.retrieveFarts(this));
    System.out.println(arrayFarts.get(0).fname + " " + arrayFarts.get(0).description + "\n");
    int temp=0;
        while (temp<arrayFarts.size()){
            cupboard().withDatabase(db).put(arrayFarts.get(temp));
            System.out.println("Array iteration " + temp + " of " + arrayFarts.size());
            System.out.println(arrayFarts.get(temp).fname+" "+arrayFarts.get(temp).description+"\n");
            temp++;
         }

Am I missing something? 我想念什么吗?

The problem lies in these lines: 问题出在以下几行:

 fdata.fname=line[0]; fdata.description=line[1]; arrayfarts.add(temp,fdata); 

You need to create a new Farts instance each loop, not change a single instance. 您需要在每个循环中创建一个新的Farts实例,而不是更改单个实例。

As it stands, you simply add the same instance multiple times to the list, while changing its values. 就目前而言,您只需将同一实例多次添加到列表中,同时更改其值。 This means that you will see only the last parsed values after the loop completes. 这意味着您将在循环完成后仅看到最后解析的值。 You will also see it multiple times, as ArrayList allows duplicates. 您还将多次看到它,因为ArrayList允许重复。

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

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