数据未添加到arrayList

时间:2012-04-21 14:40:14

标签: java

我对我的java代码感到不安。出于某种原因,我试图添加到列表中的属性是有效的,但它只保留或显示列表中的对象。我不知道属性是否被删除或者什么,但它真的很烦人。

public static Program parseProgram(File fileName)
        throws FileNotFoundException {
    Scanner in1 = new Scanner(fileName);

    Program program = new Program();
    Short value = 0;
    Short address = 0;
    boolean indirect = false;
    String[] temp = null;

    while (in1.hasNextLine()) {
        String line = in1.nextLine();
        temp = line.split("\\s+");

        Scanner in2 = new Scanner(temp[0]);

        if (in2.hasNextInt(16)) {
            int temp1 = Integer.parseInt(temp[0]);
            Short temp2 = (short) temp1;
            int temped = Integer.parseInt(temp[1]);
            Short temp3 = (short) temped;
            DataLine data = new DataLine(temp2, temp3);
            try {
                program.addDataLine(data);
            } catch (DataAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else {
            String s = "*";
            String ss = new String();

            Instruction instruction = Instruction.getByName(temp[0]);

            if (instruction.requiresArgument()) {
                int temped = Integer.parseInt(temp[1], 16);
                Short arg = (short) temped;
                if (temp.length > 1) {
                    if (temp[1].endsWith("*")) {
                        indirect = true;
                        ss = temp[1].substring(0, temp[1].indexOf("*"));
                        arg = (short) Integer.parseInt(ss);
                        CodeLine code = new CodeLine(instruction, arg,
                                indirect);
                        try {
                            program.addCodeLine(code);
                        } catch (CodeAccessException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    } else if (line.contains(s)) {
                        indirect = true;
                        String sg = temp[1].substring(0,
                                temp[1].indexOf("*"));
                String st = temp[1]
                                .substring(temp[1].indexOf("*") + 1);
                        ss = sg + st;
                        arg = (short) Integer.parseInt(ss);
                CodeLine code = new CodeLine(instruction, arg);
                        try {
                            program.addCodeLine(code);
                        } catch (CodeAccessException e) {
                        // TODO Auto-generated  catch block
                            e.printStackTrace();
                        }
                    }
                } else {
                    System.out.println("error");
                }
            }

            else {
                CodeLine code = new CodeLine(instruction);
                try {
                    program.addCodeLine(code);
                } catch (CodeAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }

    }
    return program;

}

} }

公共类程序实现Serializable {

private List<CodeLine> codeSegment = new ArrayList<CodeLine>();

private List<DataLine> dataSegment = new ArrayList<DataLine>();

/**
 * Add a line of code to this program.
 * 
 * @param line must not be null.
 * @throws CodeAccessException if line is null
 */
public void addCodeLine(CodeLine line) throws CodeAccessException{
    if(line == null)
        throw new CodeAccessException("Cannot add null code lines to a program.");
    codeSegment.add(line);
}

/**
 * Add a data instructionn to this program.  A data instruction is
 * a address - value pair.
 * 
 * @param line must not be null.
 * @throws DataAccessException if line is null
 */
public void addDataLine(DataLine line) throws DataAccessException{
    if(line == null)
        throw new DataAccessException("Cannot add null data lines to a program.");
    dataSegment.add(line);
}

1 个答案:

答案 0 :(得分:0)

我的建议是从头开始重写这一切。真。删除此代码并重新编写。