索引错误,无法弄清楚如何解析ArrayList的最后3个元素

问题描述 投票:0回答:1

我目前正在尝试将Maven依赖关系树解析为JSON,但是在某个地方我搞砸了索引编制,因此我不知道在哪里。它只是不适用于ArrayList中的最后3个元素。有人知道我哪里出错了吗?我已经在这里待了几个小时了。

这是我的代码:

main.java

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;

public class Main {
public static void main(String[] args) {

    String baseText =
            "[INFO] +- org.antlr:antlr4:jar:4.7.1:compile\n" +
            "[INFO] |  +- org.antlr:antlr4-runtime:jar:4.7.1:compile\n" +
            "[INFO] |  +- org.antlr:antlr-runtime:jar:3.5.2:compile\n" +
            "[INFO] |  \\- com.ibm.icu:icu4j:jar:58.2:compile\n" +
            "[INFO] +- commons-io:commons-io:jar:1.3.2:compile\n" +
            "[INFO] +- brs:dxprog-lang:jar:3.3-SNAPSHOT:compile\n" +
            "[INFO] |  +- brs:libutil:jar:2.51:compile\n" +
            "[INFO] |  |  +- commons-collections:commons-collections:jar:3.2.2:compile\n" +
            "[INFO] |  |  +- org.apache.commons:commons-collections4:jar:4.1:compile\n" +
            "[INFO] |  |  |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.9.0:compile\n" +
            "[INFO] |  |  |  \\- com.fasterxml.jackson.core:jackson-core:jar:2.9.5:compile";

    String[] arrOfStr = baseText.split("\n");
    ArrayList<Dependency> depList = new ArrayList<Dependency>();

    for (int i = 0; i < arrOfStr.length; i++) {
        Dependency dep = new Dependency();
        arrOfStr[i] = arrOfStr[i].substring(7);
        arrOfStr[i] = arrOfStr[i].replace("+-", "|");
        arrOfStr[i] = arrOfStr[i].replace("\\-", "|");

        int level = StringUtils.countMatches(arrOfStr[i], "|");

        arrOfStr[i] = arrOfStr[i].replace(" | ", "");
        arrOfStr[i] = arrOfStr[i].replace("| ", "");

        String[] linjeArr = arrOfStr[i].split(":");

        dep.setArtifact(linjeArr[0]);
        dep.setGroup(linjeArr[1]);
        dep.setScope(linjeArr[4]);
        dep.setVersion(linjeArr[3]);
        dep.setLevel(level);
        depList.add(dep);
    }

    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    System.out.println(gson.toJson(childSorter(depList, 0)));
}


public static ArrayList<Dependency> childSorter(ArrayList<Dependency> depList, int startIndex) {
    try {
        for (int i = startIndex; i < depList.size()-1; i++) {
            if (depList.get(i).getLevel() < depList.get(i + 1).getLevel()) {
                depList.get(i).addSubDependency(depList.get(i + 1));
                int counter = i + 1;
                do {
                    if (!(i + 1 > depList.size() - 1)) {
                        if (depList.get(counter).getLevel() == depList.get(i + 1).getLevel()) {
                            depList.get(i).addSubDependency(depList.get(counter));
                            depList.remove(counter);
                        }
                    } else {
                        if (depList.get(counter).getLevel() == depList.get(i).getLevel()) {
                            depList.get(i).addSubDependency(depList.get(counter));
                            depList.remove(counter);
                        }
                    }
                    counter += 1;
                } while (depList.get(i + 1).getLevel() == depList.get(counter).getLevel());
                depList.remove(i + 1);
            }
        }

    } catch (IndexOutOfBoundsException e) {
        System.out.println(Arrays.toString(e.getStackTrace()));
    }
    return depList;
}

}

这是我的输出:

[
  {
    "Group": "antlr4",
    "Artifact": "org.antlr",
    "Version": "4.7.1",
    "Scope": "compile",
    "Level": 1,
    "SubDependency": [
      {
        "Group": "antlr4-runtime",
        "Artifact": "org.antlr",
        "Version": "4.7.1",
        "Scope": "compile",
        "Level": 2,
        "SubDependency": []
      },
      {
        "Group": "antlr4-runtime",
        "Artifact": "org.antlr",
        "Version": "4.7.1",
        "Scope": "compile",
        "Level": 2,
        "SubDependency": []
      },
      {
        "Group": "icu4j",
        "Artifact": "com.ibm.icu",
        "Version": "58.2",
        "Scope": "compile",
        "Level": 2,
        "SubDependency": []
      }
    ]
  },
  {
    "Group": "commons-io",
    "Artifact": "commons-io",
    "Version": "1.3.2",
    "Scope": "compile",
    "Level": 1,
    "SubDependency": []
  },
  {
    "Group": "dxprog-lang",
    "Artifact": "brs",
    "Version": "3.3-SNAPSHOT",
    "Scope": "compile",
    "Level": 1,
    "SubDependency": [
      {
        "Group": "libutil",
        "Artifact": "brs",
        "Version": "2.51",
        "Scope": "compile",
        "Level": 2,
        "SubDependency": []
      },
      {
        "Group": "libutil",
        "Artifact": "brs",
        "Version": "2.51",
        "Scope": "compile",
        "Level": 2,
        "SubDependency": []
      },
      {
        "Group": "commons-collections4",
        "Artifact": "org.apache.commons",
        "Version": "4.1",
        "Scope": "compile",
        "Level": 3,
        "SubDependency": []
      }
    ]
  },
  {
    "Group": "jackson-annotations",
    "Artifact": "com.fasterxml.jackson.core",
    "Version": "2.9.0",
    "Scope": "compile",
    "Level": 4,
    "SubDependency": []
  },
  {
    "Group": "jackson-core",
    "Artifact": "com.fasterxml.jackson.core",
    "Version": "2.9.5",
    "Scope": "compile",
    "Level": 4,
    "SubDependency": []
  }
]

这是预期的输出:

[
  {
    "Group": "antlr4",
    "Artifact": "org.antlr",
    "Version": "4.7.1",
    "Scope": "compile",
    "Level": 1,
    "SubDependency": [
      {
        "Group": "antlr4-runtime",
        "Artifact": "org.antlr",
        "Version": "4.7.1",
        "Scope": "compile",
        "Level": 2,
        "SubDependency": []
      },
      {
        "Group": "antlr4-runtime",
        "Artifact": "org.antlr",
        "Version": "4.7.1",
        "Scope": "compile",
        "Level": 2,
        "SubDependency": []
      },
      {
        "Group": "icu4j",
        "Artifact": "com.ibm.icu",
        "Version": "58.2",
        "Scope": "compile",
        "Level": 2,
        "SubDependency": []
      }
    ]
  },
  {
    "Group": "commons-io",
    "Artifact": "commons-io",
    "Version": "1.3.2",
    "Scope": "compile",
    "Level": 1,
    "SubDependency": []
  },
  {
    "Group": "dxprog-lang",
    "Artifact": "brs",
    "Version": "3.3-SNAPSHOT",
    "Scope": "compile",
    "Level": 1,
    "SubDependency": [
      {
        "Group": "libutil",
        "Artifact": "brs",
        "Version": "2.51",
        "Scope": "compile",
        "Level": 2,
        "SubDependency": []
      },
      {
        "Group": "libutil",
        "Artifact": "brs",
        "Version": "2.51",
        "Scope": "compile",
        "Level": 2,
        "SubDependency": [
          {
           "Group": "commons-collections4",
           "Artifact": "org.apache.commons",
           "Version": "4.1",
           "Scope": "compile",
           "Level": 3,
           "SubDependency": [  
             {
              "Group": "jackson-annotations",
              "Artifact": "com.fasterxml.jackson.core",
              "Version": "2.9.0",
              "Scope": "compile",
              "Level": 4,
              "SubDependency": []
             },
             {
              "Group": "jackson-core",
              "Artifact": "com.fasterxml.jackson.core",
              "Version": "2.9.5",
              "Scope": "compile",
              "Level": 4,
              "SubDependency": []
             }
           ]
         }
       ]
      },
    ]
  }
]

编辑:期望输出中的格式

java arraylist indexing
1个回答
0
投票
for (int i = startIndex; i < depList.size()-1; i++) {

似乎在这里,您将从depList的大小中减去1作为for循环中的条件。但是,这不是必需的,因为您使用的是小于运算符,而不是小于或等于运算符。

© www.soinside.com 2019 - 2024. All rights reserved.