簡體   English   中英

存儲ArrayList數據並轉換為字符串[] []

[英]Storing ArrayList Data and Converting to a String[][]

我想我的代碼是正確的,用於在ArrayList中存儲數據。 但是,我無法將列表轉換為String [] []。 這是我的代碼:

        int row = 0;
        int col = 0;

        String fileInput = JOptionPane.showInputDialog(null, 
                "Please enter the path of the CSV file to read:");

        File file = new File(fileInput);

        BufferedReader bufRdr;
        bufRdr = new BufferedReader(new FileReader(file));
        String line = null;

        // Construct a new empty ArrayList for type String
        ArrayList<String[]> al = new ArrayList<String[]>();

        //read each line of text file
        while((line = bufRdr.readLine()) != null) {
            String[] columnData = line.split(",");
            al.add(columnData);
        }

        // Convert ArrayList to String array
        String[][] numbers = (String[][]) al.toArray(new String[al.size()][16]);

我收到一條錯誤,指出我無法轉換為String [] []類型。 關於我能做什么的任何想法?

編輯:所以轉換問題解決了(編輯過的代碼粘貼在上面) - 現在它輸出什么都沒有。 我覺得我必須錯誤地存儲數據。 我應該添加/更改什么?

該數組將是String[]Object[] String[] ,因此強制轉換不起作用。 您可以(並且應該)使用提供數組的toArray()版本來填充:

String[][] numbers = (String[][]) al.toArray(new String[al.size()][]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM