数组输入到数组列表但是我自己修改

时间:2014-04-19 05:35:23

标签: java data-structures

我有一个input.txt文件,其中包含以下位:

10100010
10010010
10110101
11100011
10010100
01010100
10000100
11111111
00010100

我的代码正好打印出.txt文件,但是现在我需要将它们放入带有修改的ArrayList中...因为在数组列表中将扩展为12,因为我将总是添加一些东西arraylist的第1,第2,第4和第8位置。

所以在上面的每个8位线之后,我想为每一行都有一个单独的数组列表...但是我的代码似乎将所有添加到一个名为al

的大数组列表中

例如x表示我自己的添加

10100010 ----数组大小为8

xx1x010x0010 -----数组列表大小12

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

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Encoder {
    public static void main(String[] args) {
        System.out.println("Please enter file Name: ");

        Scanner getInput = new Scanner(System.in);
        String fileName = getInput.nextLine();

        File file = new File(fileName + ".txt");
        FileInputStream fstream = null;

        try {
            fstream = new FileInputStream(file);

            DataInputStream in= new DataInputStream(fstream);

            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;
            ArrayList<Integer> al = new 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));
                }

                /*
                for(int n: n1)
                {
                System.out.println(n+" ");
                }
                */
                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]);
                }
            }

            int size = al.size();
            System.out.println(size);
            for(int j = 0; j < 12 ; j++) {
                System.out.println(al.get(j));
            }

            in.close();
        } catch (Exception e) { //Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }

        /*
         * try { System.out.println("Enter the name of Input file");
         *
         * Scanner getInput = new Scanner(System.in); String fileName =
         * getInput.nextLine();
         *
         * File f = new File(fileName+".txt"); InputStream f = new
         * FileInputStream(f);
         *
         * } catch (FileNotFoundException e) { // TODO Auto-generated catch
         * block e.printStackTrace(); }
        */
        System.out.println("Even or Odd");
    }
}

2 个答案:

答案 0 :(得分:0)

此循环只重复以下语句

for(int i = 0;i < n1.length; i++){
  ...
}

这就是为什么所有列表元素都在一个大列表中的原因。

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

稍后此外,

ArrayList<Integer> al = new ArrayList<Integer>();
应该将

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

答案 1 :(得分:0)

它们都被添加到al,因为您要将它们全部添加到al。我想你想要的是ArrayLists的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);
}