Java ArrayList字符串操作

Java ArrayList字符串操作,java,arraylist,Java,Arraylist,我正在编写一个简单的程序,使用新的行样式大括号将源代码重新格式化为行尾大括号样式。我在处理字符串并将其传递给arraylist时遇到问题 我的具体问题是,当我发现一行有一个带花括号的新行时,我就得到了前面这行的索引,将新行设置到上一个索引中的元素并附加一个“{,我在删除上一行时遇到了问题 我尝试过执行.remove(previousIndex),但不起作用 样本输入: public class Reformat { public static void main(String[] arg

我正在编写一个简单的程序,使用新的行样式大括号将源代码重新格式化为行尾大括号样式。我在处理字符串并将其传递给arraylist时遇到问题

我的具体问题是,当我发现一行有一个带花括号的新行时,我就得到了前面这行的索引,将新行设置到上一个索引中的元素并附加一个
“{
,我在删除上一行时遇到了问题

我尝试过执行
.remove(previousIndex)
,但不起作用

样本输入:

public class Reformat
{
    public static void main(String[] args)
    {
        System.out.println("Test 1");
        System.out.println("Test 2");
    }
}
这是我目前的代码:

public class Reform {
    public static void main(String[] arg) throws FileNotFoundException {
        // Pass source file to File object
        File sourceFile = new File(arg[0]);
        // Create AL of type String to hold tokens
        ArrayList<String> code = new ArrayList<String>();
        Scanner input = null;
        // Try-catch block to handle any errors while opening the file
        try {
            input = new Scanner(sourceFile);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            while (input.hasNextLine()) {
                String currentLine = input.nextLine();

                if (currentLine.isEmpty()) {
                    continue;
                } else if (currentLine.contains("{") && code.size() != 0) {
                    int previousIndex = code.size() - 1;
                    code.add(code.set(previousIndex, code.get(previousIndex) + "{"));
                } else {
                    code.add(currentLine);
                }
            }//end of while
            for (String line : code)
                System.out.println(line);
        }//end of finally
        input.close();
    }//end of main
}//end of class
公共课改革{
公共静态void main(字符串[]arg)引发FileNotFoundException{
//将源文件传递给文件对象
File sourceFile=新文件(arg[0]);
//创建字符串类型的AL以保存令牌
ArrayList代码=新的ArrayList();
扫描仪输入=空;
//在打开文件时,尝试使用catch块来处理任何错误
试一试{
输入=新扫描仪(源文件);
}catch(filenotfounde异常){
e、 printStackTrace();
}最后{
while(input.hasNextLine()){
字符串currentLine=input.nextLine();
if(currentLine.isEmpty()){
继续;
}else if(currentLine.contains(“{”)&&code.size()!=0){
int previousIndex=code.size()-1;
code.add(code.set(previousIndex,code.get(previousIndex)+“{”);
}否则{
代码。添加(当前行);
}
}//结束
for(字符串行:代码)
系统输出打印项次(行);
}//终于结束了
input.close();
}//干管末端
}//下课
这是错误的:

code.add(code.set(previousIndex, code.get(previousIndex) + "{"));
应该是:

code.set(previousIndex, code.get(previousIndex) + "{");

我仔细研究了有问题的代码,并通过以下方式解决了问题:

 else if (currentLine.contains("{") && code.size() != 0) {
                int previousIndex = code.size() - 1;
                String newLine = code.set(previousIndex, code.get(previousIndex) + "{");
                code.add(previousIndex, newLine);
                code.remove(previousIndex);

无法100%确定为什么它以这种方式工作,而不是其他尝试。我希望得到一个解释。最糟糕的解决方案类型是您不理解的解决方案。

您的
最终
部分将运行,即使引发异常。。我想您希望将所有处理移到
try
部分,并且只运行
input.close()
最后,为什么不使用IDE来格式化代码?原来是字符串newLine=code.set(previousIndex,code.get(previousIndex)+“{”);code.add(newLine);为什么要以未修改的形式获取前一行?我想重点是修改它来添加“{”?是的,我不明白你为什么不@n0tion发布一个没有文件的工作代码。:)是的,这就是重点。我找不到ArrayList的append方法。所以我的想法是抓住原始表单,追加{,删除原始行,然后用行尾的大括号追加新行。