繁体   English   中英

Java:使用List of Integer的ArrayList读取CSV

[英]Java : CSV reading with ArrayList of List of Integer

所以,大家好,这是我的第一个问题。 确实在找了一段时间后我没有找到任何有用的东西。 我是java和数据挖掘的新手。 我有一个问题,所以我试图从CSV文件中获取一些数据以将其转换为ARFF(这将允许我在Weka中正确打开它)所以重点是我所拥有的CSV有点破碎,我有两个属性,一个是情感,一个是像素,但事实是像素一个是由17个不同的像素值组成。 我计划将我的第一个属性放在List的ArrayList中,其中每个列表由一个元素组成并代表一种情绪。 另一个列表的ArrayList,其中每个列表是我17个像素的一堆,我不知道我是否清楚,但这里是我的代码,后面是我的输出和我想要的输出。

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class CSVformat {

    private static ArrayList<List<Integer>> Emotions = new ArrayList<List<Integer>>();
    private static ArrayList<List<Integer>> Pixels = new ArrayList<List<Integer>>();

    public CSVformat(ArrayList<List<Integer>> emotions, ArrayList<List<Integer>> pixels){
    this.setEmotions(emotions);
    this.setPixels(pixels);
    }

    /**
     * @return the emotions
     */
    public ArrayList<List<Integer>> getEmotions() {
        return Emotions;
    }

    /**
     * @param emotions the emotions to set
     */
    public void setEmotions(ArrayList<List<Integer>> emotions) {
        CSVformat.Emotions = emotions;
    }

    /**
     * @return the pixels
     */
    public ArrayList<List<Integer>> getPixels() {
        return Pixels;
    }

    /**
     * @param pixels the pixels to set
     */
    public void setPixels(ArrayList<List<Integer>> pixels) {
        CSVformat.Pixels = pixels;
    }

    /*public void readData(String fileName) throws IOException { 
        int count = 0;
        String file = fileName;
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));
            String line = "";
            while ((line = br.readLine()) != null) {

                Emotions.add(line.split(","));

                String[][] v = (String[][]) bank.toArray(new String[bank.size()][12]);

            }
        } catch (FileNotFoundException e) {

        }
    }*/

    public static void fillArrayList(String fileName) throws FileNotFoundException {
        List<String> list = new ArrayList<String>(); 
        List<Integer> emotions = new ArrayList<Integer>();
        List<Integer> pixels = new ArrayList<Integer>();

        File file = new File(fileName);
        //Scanner scan = new Scanner(new File(fileName));
        if(file.exists()){
            try { 
                list = Files.readAllLines(file.toPath(),Charset.defaultCharset());
            } catch (IOException ex) {
                ex.printStackTrace();
            }
          if(list.isEmpty())
              //scan.close();
              return;
        }
       for(String line : list.subList( 1, list.size() )){
        String[] res = line.split(",");
        String[] arr= res[1].split(" ");
        emotions.add(Integer.parseInt(res[0]));
        for(int i=0;i<arr.length;i++){
            pixels.add(Integer.parseInt(arr[i]));   

            }

        }
     Emotions.add(emotions);
     Pixels.add(pixels);
    System.out.println(Emotions);
    System.out.println(Pixels);
    }}

所以这是我正在使用的缩短CSV:

emotion,pixels
0,70 80 82 72 58 58 60 63 54 58 60 48 89 115 121 119 115
1,70 80 82 72 58 58 60 63 54 58 60 48 89 115 121 119 115
3,70 80 82 72 58 58 60 63 54 58 60 48 89 115 121 119 115 

(请注意,我告诉你的CSV有点坏了,这是给我的课程作业,这就是为什么我要先修复它)

最后,通过为此CSV运行此代码,我将其作为输出:

[[0, 1, 3]]
[[70, 80, 82, 72, 58, 58, 60, 63, 54, 58, 60, 48, 89, 115, 121, 119, 115, 70, 80, 82, 72, 58, 58, 60, 63, 54, 58, 60, 48, 89, 115, 121, 119, 115, 70, 80, 82, 72, 58, 58, 60, 63, 54, 58, 60, 48, 89, 115, 121, 119, 115]]

虽然我想要这个:

[[0], [1], [3]]
[[70, 80, 82, 72, 58, 58, 60, 63, 54, 58, 60, 48, 89, 115, 121, 119, 115], [70, 80, 82, 72, 58, 58, 60, 63, 54, 58, 60, 48, 89, 115, 121, 119, 115], [70, 80, 82, 72, 58, 58, 60, 63, 54, 58, 60, 48, 89, 115, 121, 119, 115]]

我希望我很清楚。 我知道这只是因为我正在使用add方法列表,但是我甚至无法通过逐行使用扫描仪读取或其他方法来完成此任务。 我还尝试在将它再次放入ArrayList之前清除List,但是juste给出了一个完全EMPTY ArrayList。

非常感谢您的帮助。

你有一份清单清单

private static ArrayList<List<Integer>> Emotions = new ArrayList<List<Integer>>();

并将您的文件读入列表

List<Integer> emotions = new ArrayList<Integer>();

并将其存储在列表列表中

Emotions.add(emotions);

如果你打印出来,你得到:

[[0, 1, 3]]

如果你想让它与众不同,你必须为你处理的每个循环/行创建List<Integer> emotions ,所以最终你的fillArrayList方法可能如下所示:

public static void fillArrayList(String fileName) throws FileNotFoundException {
    List<String> list = new ArrayList<String>();
    File file = new File(fileName);
    if (file.exists()) {
        try {
            list = Files.readAllLines(file.toPath(), Charset.defaultCharset());
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        if (list.isEmpty())
            return;
    }

    for (String line : list.subList(1, list.size())) {
        System.out.println(line);

        String[] res = line.split(",");
        String[] arr = res[1].split(" ");

        List<Integer> emotions = new ArrayList<Integer>();
        emotions.add(Integer.parseInt(res[0]));
        Emotions.add(emotions);

        List<Integer> pixels = new ArrayList<Integer>();
        for (int i = 0; i < arr.length; i++) {
            pixels.add(Integer.parseInt(arr[i]));
        }
        Pixels.add(pixels);
    }
}

暂无
暂无

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

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