Java 但是,通过我自己的修改,将数组馈送到数组列表中

Java 但是,通过我自己的修改,将数组馈送到数组列表中,java,data-structures,Java,Data Structures,我有一个input.txt文件,它逐行包含以下位: 10100010 10010010 10110101 11100011 10010100 01010100 10000100 11111111 00010100 我的代码可以从.txt文件中打印出来,但是现在我需要将它们放入一个经过修改的ArrayList中。。。正如在数组中一样,列表将扩展到大小12,因为我将始终在arraylist的第一、第二、第四和第八位置添加新的内容 所以在上面的每一行8位之后,我想为每一行创建一个单独的数组列表。。。

我有一个input.txt文件,它逐行包含以下位:

10100010
10010010
10110101
11100011
10010100
01010100
10000100
11111111
00010100
我的代码可以从.txt文件中打印出来,但是现在我需要将它们放入一个经过修改的ArrayList中。。。正如在数组中一样,列表将扩展到大小12,因为我将始终在arraylist的第一、第二、第四和第八位置添加新的内容

所以在上面的每一行8位之后,我想为每一行创建一个单独的数组列表。。。然而,我的代码似乎将所有内容添加到一个名为al的大数组列表中

例如,x表示我自己添加的内容

10100010阵列大小8

xx1x010x0010---数组列表大小12

这些x将在以后处理,但我希望有一个数组列表打印到控制台或输出文件,用于上面input.txt文件中的每一行


这个循环多次重复下面的语句

for(int i = 0;i < n1.length; i++){
  ...
}
这就是为什么所有列表元素都在一个大列表中

只需省略for语句,但保留循环体

后来呢,


应该移动到读取文本文件行的循环中。

它们都被添加到al,因为您正在将它们全部添加到al。我想您需要的是一个ArrayList的ArrayList。比如说:

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

while((strLine = br.readLine()) != null) {
    //print the content to console
    System.out.println(strLine);
    int[] n1 = new int [8];

    for(int i =0;i < strLine.length();i++){
        //  System.out.println((strLine.charAt(i)));

         n1[i] = Integer.valueOf(strLine.substring(i, i+1)); 

    }

    List<Integer> al = new ArrayList<Integer>();
    for(int i = 0;i < n1.length; i++){
        al.add(0,1);  // dummy value for now 1st need to be changed
        al.add(1,0);  // dummy value for now 2nd need to be changed
        al.add(2, n1[0]);
        al.add(3,0);  // dummy value for now 4th need to be changed
        al.add(4,n1[1]);
        al.add(5,n1[2]);
        al.add(6,n1[3]);
        al.add(7,0);   // dummy value for now 8th need to be changed
        al.add(8,n1[4]);
        al.add(9,n1[5]);
        al.add(10,n1[6]);
        al.add(11,n1[7]);
    }
    bitLists.add(al);
}
ArrayList<Integer> al = new ArrayList<Integer>();
List<List<Integer>> bitLists = new ArrayList<ArrayList<Integer>>();

while((strLine = br.readLine()) != null) {
    //print the content to console
    System.out.println(strLine);
    int[] n1 = new int [8];

    for(int i =0;i < strLine.length();i++){
        //  System.out.println((strLine.charAt(i)));

         n1[i] = Integer.valueOf(strLine.substring(i, i+1)); 

    }

    List<Integer> al = new ArrayList<Integer>();
    for(int i = 0;i < n1.length; i++){
        al.add(0,1);  // dummy value for now 1st need to be changed
        al.add(1,0);  // dummy value for now 2nd need to be changed
        al.add(2, n1[0]);
        al.add(3,0);  // dummy value for now 4th need to be changed
        al.add(4,n1[1]);
        al.add(5,n1[2]);
        al.add(6,n1[3]);
        al.add(7,0);   // dummy value for now 8th need to be changed
        al.add(8,n1[4]);
        al.add(9,n1[5]);
        al.add(10,n1[6]);
        al.add(11,n1[7]);
    }
    bitLists.add(al);
}