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

我想将从 csv 中读取的每一行作为 sub_list 并将此类 sub_list 添加到 master_list 中。

https://img2.mukewang.com/64f8332b0001534f06210224.jpg

所以它会是这样的:

[[第 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);


    }


}

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


肥皂起泡泡
浏览 83回答 3
3回答

qq_遁去的一_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 {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; String csvFile = "E:\\country.csv";&nbsp; &nbsp; &nbsp; &nbsp; String line = "";&nbsp; &nbsp; &nbsp; &nbsp; String cvsSplitBy = ",";&nbsp; &nbsp; &nbsp; &nbsp; List<String> sub = new ArrayList<String>();&nbsp; &nbsp; &nbsp; &nbsp; List<List<String>> master = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while ((line = br.readLine()) != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // use comma as separator&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] country = line.split(cvsSplitBy);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sub = new ArrayList<String>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sub.add(line);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; master.add(new ArrayList<String>(sub));//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(sub);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sub.remove(0);//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Country [code= " + country[4] + " , name=" + country[5] + "]");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(master);&nbsp; &nbsp; }}

白板的微信

尝试将变量声明移到subwhile 块上。&nbsp; &nbsp; String csvFile = "E:\\country.csv";&nbsp; &nbsp; &nbsp; &nbsp; String line = "";&nbsp; &nbsp; &nbsp; &nbsp; String cvsSplitBy = ",";&nbsp; &nbsp; &nbsp; &nbsp; List<List> master = new ArrayList<List>();&nbsp; &nbsp; &nbsp; &nbsp; try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while ((line = br.readLine()) != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // use comma as separator&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] country = line.split(cvsSplitBy);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<String> sub = new ArrayList<String>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sub.add(line);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; master.add(sub);//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(sub);//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sub.remove(0);//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Country [code= " + country[4] + " , name=" + country[5] + "]");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(master);

幕布斯7119047

你可以这样做public static List<String[]> getTestData(String fileName) {&nbsp; &nbsp; List<String[]> records = new ArrayList<String[]>();&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; String record;&nbsp; &nbsp; &nbsp; &nbsp; BufferedReader file = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "UTF-8"));&nbsp; &nbsp; &nbsp; &nbsp; while ((record = file.readLine()) != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(record);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String fields[] = record.split(",");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; records.add(fields);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; file.close();&nbsp; &nbsp; &nbsp; &nbsp; return records;&nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }&nbsp; &nbsp; return null;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java