Java:将一个列表添加到另一个列表中,但不复制引用

问题描述 投票:0回答:3

我想使每一行都从csv中读取为sub_list,并将此sub_list添加到master_list。

enter image description here

所以会是这样的:

[[[第1行] [第2行] .... [最后一行]]

如何确保添加到master_list中的sub_list不受原始sub_list中更改的影响。我了解这与浅拷贝与深拷贝有关。什么是正确的方法。这样做的原因是因为我可能会将子列表用于其他地方的其他不同操作。一旦需要,就需要清除其中的内容作为空白列表。因此,我想将相同的列表用于不同的任务。

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class CSVReader {

    public static void main(String[] args) {

        String csvFile = "E:\\country.csv";
        String line = "";
        String cvsSplitBy = ",";
        List<String> sub = new ArrayList<String>();
        List<List> master = new ArrayList<List>();

        try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {

            while ((line = br.readLine()) != null) {

                // use comma as separator
                String[] country = line.split(cvsSplitBy);
                sub.add(line);
                master.add(sub);
//              System.out.println(sub);
                sub.remove(0);

//                System.out.println("Country [code= " + country[4] + " , name=" + country[5] + "]");

            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println(master);

    }

}

这将打印出空列表“ []”。

java list arraylist deep-copy shallow-copy
3个回答
1
投票

两点:-

1-如下更改您的主列表。您不应该创建通用列表(原始类型)。如果将列表创建为原始类型,则可以输入任何数据类型输入。由于可能存在多种数据类型列表,因此会产生问题。

2-当您进入while循环时,创建/分配新引用到子列表,然后将其添加到主列表。

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class CSVReader {

    public static void main(String[] args) {

        String csvFile = "E:\\country.csv";
        String line = "";
        String cvsSplitBy = ",";
        List<String> sub = new ArrayList<String>();
        List<List<String>> master = new ArrayList<>();

        try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {

            while ((line = br.readLine()) != null) {

                // use comma as separator
                String[] country = line.split(cvsSplitBy);
                sub = new ArrayList<String>();
                sub.add(line);
                master.add(new ArrayList<String>(sub));
//              System.out.println(sub);
                sub.remove(0);

//                System.out.println("Country [code= " + country[4] + " , name=" + country[5] + "]");

            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println(master);

    }

}

1
投票

尝试将sub变量的声明移动到while块上。

    String csvFile = "E:\\country.csv";
        String line = "";
        String cvsSplitBy = ",";
        List<List> master = new ArrayList<List>();

        try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {

            while ((line = br.readLine()) != null) {

                // use comma as separator
                String[] country = line.split(cvsSplitBy);
                List<String> sub = new ArrayList<String>();
                sub.add(line);
                master.add(sub);
//              System.out.println(sub);
//              sub.remove(0);

//                System.out.println("Country [code= " + country[4] + " , name=" + country[5] + "]");

            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println(master);

0
投票

您可以这样做

public static List<String[]> getTestData(String fileName) {
    List<String[]> records = new ArrayList<String[]>();
    try {
        String record;
        BufferedReader file = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "UTF-8"));
        while ((record = file.readLine()) != null) {
            System.out.println(record);
            String fields[] = record.split(",");
            records.add(fields);
        }
        file.close();
        return records;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
© www.soinside.com 2019 - 2024. All rights reserved.