Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.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 将CSV加载到多维数组中_Java_Arrays_Csv - Fatal编程技术网

Java 将CSV加载到多维数组中

Java 将CSV加载到多维数组中,java,arrays,csv,Java,Arrays,Csv,因此,我需要做的是将csv文件加载到带有动态数组(2到6个值之间的任意位置)的JTable中,首先我提出了以下解决方案: private String[][] Load() { try { final BufferedReader br = new BufferedReader(new FileReader("FilePath"); try{ String line; final ArrayList<S

因此,我需要做的是将csv文件加载到带有动态数组(2到6个值之间的任意位置)的JTable中,首先我提出了以下解决方案:

private String[][] Load()
{
    try {
        final BufferedReader br = new BufferedReader(new FileReader("FilePath");
        try{
            String line;
            final ArrayList<String> List1 = new ArrayList<String>();
            final ArrayList<String> List2 = new ArrayList<String>();
            final ArrayList<String> List3 = new ArrayList<String>();
            while((line = br.readLine()) != null)
            {
                final String[] parse = line.split(",");
                List1.add(parse[0]);
                List2.add(parse[1]);
                List3.add(parse[2]);
            }

            final ArrayList[] lists = new ArrayList[]{List1, List2, List3};
            final String[][] temp = new String[List1.size()][List1.get(0).split(",").length];

            for(int x = 0 ; x < temp.length ; x++)
            {
                for(int y = 0 ; y < temp[x].length ; y++)
                {
                    temp[x][y] = (String) lists[y].get(x);
                }
            }
            return temp;
        }finally
        {
            br.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
private String[][] Load()
{

    try
    {
        final BufferedReader br = new BufferedReader(new FileReader("FilePath"));
        try
        {
            final List<String> raw = new ArrayList<String>();
            String line;
            while((line = br.readLine()) != null)
                raw.add(line);

            final String[][] temp = new String[raw.size()][raw.get(0).split(",").length];
            for(int x = 0 ; x < raw.size() ; x++)
            {
                final String[] parse = raw.get(x).split(",");
                for(int y = 0 ; y < temp[x].length ; y++)
                {
                    temp[x][y] = parse[y];
                }
            }
            return temp;
        }finally
        {
            br.close();
        }
    }catch(IOException e){
        e.printStackTrace();
    }
    return null;
}
私有字符串[][]加载()
{
试一试{
final BufferedReader br=新BufferedReader(新文件读取器(“文件路径”);
试一试{
弦线;
最终ArrayList列表1=新ArrayList();
最终ArrayList列表2=新ArrayList();
最终ArrayList List3=新ArrayList();
而((line=br.readLine())!=null)
{
最后一个字符串[]parse=line.split(“,”);
列表1.add(解析[0]);
列表2.add(解析[1]);
清单3.add(解析[2]);
}
最终ArrayList[]列表=新ArrayList[]{List1,List2,List3};
最终字符串[][]临时=新字符串[List1.size()][List1.get(0.split(“,”).length];
对于(int x=0;x
我觉得很可怕,所以我想出了这个解决方案:

private String[][] Load()
{
    try {
        final BufferedReader br = new BufferedReader(new FileReader("FilePath");
        try{
            String line;
            final ArrayList<String> List1 = new ArrayList<String>();
            final ArrayList<String> List2 = new ArrayList<String>();
            final ArrayList<String> List3 = new ArrayList<String>();
            while((line = br.readLine()) != null)
            {
                final String[] parse = line.split(",");
                List1.add(parse[0]);
                List2.add(parse[1]);
                List3.add(parse[2]);
            }

            final ArrayList[] lists = new ArrayList[]{List1, List2, List3};
            final String[][] temp = new String[List1.size()][List1.get(0).split(",").length];

            for(int x = 0 ; x < temp.length ; x++)
            {
                for(int y = 0 ; y < temp[x].length ; y++)
                {
                    temp[x][y] = (String) lists[y].get(x);
                }
            }
            return temp;
        }finally
        {
            br.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
private String[][] Load()
{

    try
    {
        final BufferedReader br = new BufferedReader(new FileReader("FilePath"));
        try
        {
            final List<String> raw = new ArrayList<String>();
            String line;
            while((line = br.readLine()) != null)
                raw.add(line);

            final String[][] temp = new String[raw.size()][raw.get(0).split(",").length];
            for(int x = 0 ; x < raw.size() ; x++)
            {
                final String[] parse = raw.get(x).split(",");
                for(int y = 0 ; y < temp[x].length ; y++)
                {
                    temp[x][y] = parse[y];
                }
            }
            return temp;
        }finally
        {
            br.close();
        }
    }catch(IOException e){
        e.printStackTrace();
    }
    return null;
}
私有字符串[][]加载()
{
尝试
{
final BufferedReader br=新的BufferedReader(新文件读取器(“文件路径”));
尝试
{
最终列表原始=新的ArrayList();
弦线;
而((line=br.readLine())!=null)
原始。添加(行);
最终字符串[][]临时=新字符串[raw.size()][raw.get(0.split)(,“”.length];
对于(int x=0;x
但我也不太确定,它们都很好,所以我真正想问的是,你会怎么做


编辑:无法在此项目中真正使用外部libs。

CSV可能会变得更加复杂。我会使用第三方API(我认为Apache有一个)。@SJuan76对于我目前正在进行的项目,我不能真正使用外部libs,否则我肯定会使用。