开发者社区> 问答> 正文

Java二维数组存储一位数组的问题

从一个txt文件中读出字符串,每一行输出一个record,但我想把所有record中的字符串存放到一个字符数组中,便于我下一步单个字符分析,该怎么改啊

class test2 {
    public static void main(String[] args) {
        test2 t = new test2();
        t.readMyFile();
    }

    void readMyFile() {
        String record;
        int recCount = 0;
        char[] a;
        char[][] b=new char[10][];
        int i=0;
        int j=0;
        try {
            FileReader fr = new FileReader("D:\\data.txt");
            BufferedReader br = new BufferedReader(fr);
            while ((record = br.readLine()) != null) {
                recCount++;
                a=record.toCharArray();
                System.out.println(recCount + ": " + record);
                System.out.println(a[j]);
                for(;j<a.length;j++)
                {   
                    b[i][j]=a[j];
                }
                i++;
                //System.out.println(b[i][j]);
            }
            br.close();
            fr.close();
        } catch (IOException e) {
            System.out.println("Uh oh, got an IOException error!");
            e.printStackTrace();
        }
    }

}

screenshot

展开
收起
蛮大人123 2016-03-12 18:42:14 2350 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪
    public class test2 {
        public static void main(String[] args) {
            test2 t = new test2();
            t.readMyFile();
        }
    
        void readMyFile() {
            String record;
            ArrayList<char[]> charLists = new ArrayList();//变成这样
            char[] a;
            try {
                FileReader fr = new FileReader("D:\\data.txt");
                BufferedReader br = new BufferedReader(fr);
                while ((record = br.readLine()) != null) {
                    a=record.toCharArray();
                    charLists.add(a);
                }
                br.close();
                fr.close();
            } catch (IOException e) {
                System.out.println("Uh oh, got an IOException error!");
                e.printStackTrace();
            }
        }
    }
    2019-07-17 19:01:26
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载