You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将ArrayList生成的字符串矩阵格式化为x行y列?

Customize toString() for 3D ArrayList to Get Row-Column Formatting

Got it, let's sort this out. The default ArrayList.toString() spits out all those nested brackets, and just replacing them with empty strings only gives you a flat list—definitely not the clean row-column layout you're after. Instead of hacking the default output, we can build a custom formatting logic that respects your matrix structure.

The Core Idea

Your ArrayList<ArrayList<ArrayList<T>>> structure implies that:

  • The outermost list holds the rows of your matrix
  • Each middle list represents a single row's elements (wrapped in inner lists, from your example output)
  • The innermost lists are just containers for individual elements

So we need to iterate through each row, pull out the elements from their inner containers, separate them with tabs, and add a newline after each row.

Implementation Code

You can create a utility method (or override toString() if this is part of a custom class) like this:

public static <T> String format3DMatrix(ArrayList<ArrayList<ArrayList<T>>> matrix) {
    StringBuilder outputBuilder = new StringBuilder();
    
    // Loop through each row in the outer list
    for (ArrayList<ArrayList<T>> row : matrix) {
        // Loop through each element container in the row
        for (int colIndex = 0; colIndex < row.size(); colIndex++) {
            ArrayList<T> elementHolder = row.get(colIndex);
            // Grab the element (assuming each inner list has one element)
            if (!elementHolder.isEmpty()) {
                outputBuilder.append(elementHolder.get(0));
            }
            // Add tab separator unless it's the last element in the row
            if (colIndex != row.size() - 1) {
                outputBuilder.append("\t");
            }
        }
        // Add newline after each row
        outputBuilder.append(System.lineSeparator());
    }
    
    // Trim the final extra newline (optional but cleaner)
    if (outputBuilder.length() > 0) {
        outputBuilder.setLength(outputBuilder.length() - System.lineSeparator().length());
    }
    
    return outputBuilder.toString();
}

How to Use It

Let's say your matrixOne is structured like this (matching your example):

ArrayList<ArrayList<ArrayList<String>>> matrixOne = new ArrayList<>();

// First row: [a, b, c]
ArrayList<ArrayList<String>> row1 = new ArrayList<>();
row1.add(new ArrayList<>(List.of("a")));
row1.add(new ArrayList<>(List.of("b")));
row1.add(new ArrayList<>(List.of("c")));

// Second row: [d, e, f]
ArrayList<ArrayList<String>> row2 = new ArrayList<>();
row2.add(new ArrayList<>(List.of("d")));
row2.add(new ArrayList<>(List.of("e")));
row2.add(new ArrayList<>(List.of("f")));

// Third row: [i, h, g]
ArrayList<ArrayList<String>> row3 = new ArrayList<>();
row3.add(new ArrayList<>(List.of("i")));
row3.add(new ArrayList<>(List.of("h")));
row3.add(new ArrayList<>(List.of("g")));

matrixOne.add(row1);
matrixOne.add(row2);
matrixOne.add(row3);

Calling format3DMatrix(matrixOne) will give you exactly the output you want:

a	b	c
d	e	f
i	h	g

Dynamic Row/Column Sizing

This method automatically adapts to your matrix dimensions:

  • The number of rows is determined by the size of the outermost ArrayList
  • The number of columns per row is determined by the size of each middle ArrayList

If your innermost lists ever hold multiple elements, you can tweak the logic—for example, joining those elements with commas before adding them to the row.

Quick Notes

  • If your 3D structure has a different hierarchy (e.g., outer list is columns instead of rows), just swap the order of iteration
  • Add extra null/empty checks if your inner lists might be empty to avoid IndexOutOfBoundsException

内容的提问来源于stack exchange,提问作者billy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:58:24