开发者

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

开发者 https://www.devze.com 2023-03-26 23:58 出处:网络
I think I have the code correct for storing data in an ArrayList.However, I can\'t convert the list to a String[][]. Here\'s my code:

I think I have the code correct for storing data in an ArrayList. However, I can't convert the list to a String[][]. Here's my code:

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

I'm getting an error stating I can't convert to a String[][] type. Any ideas on what I can do?

EDIT: So the conversion problem is solved (the edited code is pasted above) - now its outputting nothing though. I feel like I must have stored the data incorrectly. Wh开发者_C百科at should I add/change?


The array will be an Object[] of String[], so the cast doesn't work. You can (and should) use the toArray() version that supplies the array to populate:

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

精彩评论

暂无评论...
验证码 换一张
取 消