簡體   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