【问题标题】:How to Add newline after every 3rd element in arraylist in java?java - 如何在java中arraylist中的每个第3个元素之后添加换行符?
【发布时间】:2014-03-30 15:58:14
【问题描述】:

我的程序需要在数组列表中的每个第三个元素之后添加换行符。这是我的输入文件,其中包含以下数据:

934534519137441534534534366, 0796544345345345348965, 796345345345544894534565, 734534534596544534538965, 4058991374534534999999, 34534539624, 91953413789453450452, 9137534534482080, 9153453459137482080, 405899137999999, 9653453564564524, 91922734534779797, 0834534534980001528, 82342398534 6356343430001528, 405899137999999, 9191334534643534547423752, 3065345782642564522021, 826422205645345345645621, 40584564563499137999999, 953453345344624, 3063454564345347, 919242353463428434451, 09934634634604641264, 990434634634641264, 40346346345899137999999, 963445636534653452, 919234634643325857953, 91913453453437987385, 59049803463463453455421, 405899137534534999999, 9192273453453453453758434,

它会延伸到多行。

代码:

public class MyFile {

    private static final Pattern ISDN =Pattern.compile("\\s*ISDN=(.*)");

    public List<String> getISDNsFromFile(final String fileName) throws IOException {

        final Path path = Paths.get(fileName);
        final List<String> ret = new ArrayList<>();
        Matcher m;
        String line;
        int index = 0;
        try (
        final BufferedReader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8);) {
            while ((line = reader.readLine()) != null) {
                m = ISDN.matcher(line);
                if (m.matches()) {
                    ret.add(m.group(1));
                    index++;
                    if(index%3==0){
                        if(ret.size() == index){
                            ret.add(m.group(1).concat("\n"));
                        }
                    }
                }
            }
            return ret;
        } 
    }
}

【问题讨论】:

  • 你有什么问题?
  • 我会将它们全部添加到列表中,并且仅在打印时添加格式。在数据中添加格式通常会导致混淆。
  • 在这种情况下不要使用“\n”,而是使用System.getProperty("line.separator");
  • 你能正确告诉我怎么做吗??
  • try 块是什么?你在哪里发现你的异常?

标签: java arraylist


【解决方案1】:

我将您的代码更改为使用“新”Java 7 文件 I/O 读取,并更正了行分隔符的使用以及一些格式。在这里,我在完成后迭代列表。如果它真的很长,你可以在构造它时对其进行迭代。

public class MyFile {

    private static final Pattern ISDN = Pattern.compile("\\s*ISDN=(.*)");
    private static final String LS = System.getProperty("line.separator");

    public List<String> getISDNsFromFile(final String fileName) throws IOException {

        List<String> ret = new ArrayList<>();
        Matcher m;

        List<String> lines = Files.readAllLines(Paths.get(fileName), StandardCharsets.UTF_8);
        for (String line : lines) {
            m = ISDN.matcher(line);
            if (m.matches())
                ret.add(m.group(1));
        }
        for (int i = 3; i < ret.size(); i+=4)
            ret.add(i, LS);
        return ret;
    }
}

【讨论】:

    【解决方案2】:

    我认为您不需要将arraylist的大小与索引进行比较。只需删除该条件并尝试使用此

    if(index%3==0){                         
       ret.add(System.getProperty("line.separator"));
       }
    

    虽然我支持@Peter 的评论 I would add them all to a List and only add the formatting when you print. Adding formatting in your data usually leads to confusion.

    【讨论】:

      猜你喜欢
      • 2020-08-12
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 2018-02-11
      • 1970-01-01
      • 2020-12-09
      相关资源
      最近更新 更多