Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:将一个列表添加到另一个列表,而不复制引用_Java_List_Arraylist_Deep Copy_Shallow Copy - Fatal编程技术网

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

Java:将一个列表添加到另一个列表,而不复制引用,java,list,arraylist,deep-copy,shallow-copy,Java,List,Arraylist,Deep Copy,Shallow Copy,我希望将csv中的每一行作为子列表读取,并将此类子列表添加到主列表中 所以它会是这样的: [[第1行][第2行]……[最后一行]] 如何确保主列表中添加的子列表不受原始子列表更改的影响。我知道这和浅拷贝和深拷贝有关。正确的方法是什么。这样做的原因是,我可能会将子列表用于其他地方的其他不同操作。一旦我需要这样做,我需要清除其中的内容作为一个空列表。因此,我希望对不同的任务保持使用相同的列表 import java.io.BufferedReader; import java.io.FileRea

我希望将csv中的每一行作为子列表读取,并将此类子列表添加到主列表中

所以它会是这样的:

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

如何确保主列表中添加的子列表不受原始子列表更改的影响。我知道这和浅拷贝和深拷贝有关。正确的方法是什么。这样做的原因是,我可能会将子列表用于其他地方的其他不同操作。一旦我需要这样做,我需要清除其中的内容作为一个空列表。因此,我希望对不同的任务保持使用相同的列表

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.io.BufferedReader;
导入java.io.FileReader;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.List;
公共类CSVReader{
公共静态void main(字符串[]args){
字符串csvFile=“E:\\country.csv”;
字符串行=”;
字符串cvsSplitBy=“,”;
List sub=new ArrayList();
List master=new ArrayList();
try(BufferedReader br=new BufferedReader(new FileReader(csvFile))){
而((line=br.readLine())!=null){
//使用逗号作为分隔符
字符串[]国家=行分割(cvsSplitBy);
sub.add(行);
主。添加(子);
//系统输出打印项次(sub);
sub.remove(0);
//System.out.println(“国家[code=“+Country[4]+”,name=“+Country[5]+”]”);
}
}捕获(IOE异常){
e、 printStackTrace();
}
系统输出打印项次(主);
}
}

这将输出空列表“[]”

尝试将
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);
String csvFile=“E:\\country.csv”;
字符串行=”;
字符串cvsSplitBy=“,”;
List master=new ArrayList();
try(BufferedReader br=new BufferedReader(new FileReader(csvFile))){
而((line=br.readLine())!=null){
//使用逗号作为分隔符
字符串[]国家=行分割(cvsSplitBy);
List sub=new ArrayList();
sub.add(行);
主。添加(子);
//系统输出打印项次(sub);
//sub.remove(0);
//System.out.println(“国家[code=“+Country[4]+”,name=“+Country[5]+”]”);
}
}捕获(IOE异常){
e、 printStackTrace();
}
系统输出打印项次(主);

尝试将
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);
String csvFile=“E:\\country.csv”;
字符串行=”;
字符串cvsSplitBy=“,”;
List master=new ArrayList();
try(BufferedReader br=new BufferedReader(new FileReader(csvFile))){
而((line=br.readLine())!=null){
//使用逗号作为分隔符
字符串[]国家=行分割(cvsSplitBy);
List sub=new ArrayList();
sub.add(行);
主。添加(子);
//系统输出打印项次(sub);
//sub.remove(0);
//System.out.println(“国家[code=“+Country[4]+”,name=“+Country[5]+”]”);
}
}捕获(IOE异常){
e、 printStackTrace();
}
系统输出打印项次(主);

两点:-

1-更改您的主列表,如下所示。不应创建泛型列表(原始类型)。若将列表创建为原始类型,则可以输入任何数据类型输入。它将创建问题,因为可能存在多数据类型列表

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

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

    }

}
导入java.io.BufferedReader;
导入java.io.FileReader;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.List;
公共类CSVReader{
公共静态void main(字符串[]args){
字符串csvFile=“E:\\country.csv”;
字符串行=”;
字符串cvsSplitBy=“,”;
List sub=new ArrayList();
List master=new ArrayList();
try(BufferedReader br=new BufferedReader(new FileReader(csvFile))){
而((line=br.readLine())!=null){
//使用逗号作为分隔符
字符串[]国家=行分割(cvsSplitBy);
sub=新的ArrayList();
sub.add(行);
主添加(新阵列列表(子));
//系统输出打印项次(sub);
sub.remove(0);
//System.out.println(“国家[code=“+Country[4]+”,name=“+Country[5]+”]”);
}
}捕获(IOE异常){
e、 printStackTrace();
}
系统输出打印项次(主);
}
}

两点:-

1-更改您的主列表,如下所示。不应创建泛型列表(原始类型)。若将列表创建为原始类型,则可以输入任何数据类型输入。它将创建问题,因为可能存在多数据类型列表

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

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

    }

}
导入java.io.BufferedReader;
导入java.io.FileReader;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.List;
公共类CSVReader{
公共静态void main(字符串[]args){
字符串csvFile=“E:\\country.csv”;
字符串行=”;
字符串cvsSplitBy=“,”;
List sub=new ArrayList();
List master=new ArrayList();
try(BufferedReader br=new BufferedReader(new FileReader(csvFile))){
而((line=br.readLine())!=null){
//使用逗号作为分隔符
字符串[]国家=行分割(cvsSplitBy);
sub=新的ArrayList();