[Solved] How do i format a ArrayList for printing?


this is almost what you need 🙂 using tabs

List<String> l = new ArrayList<String>(Arrays.asList(ss)){;
    @Override
    public String toString(){
        Iterator<String> it = iterator();
        if (! it.hasNext())
            return "";

        StringBuilder sb = new StringBuilder();
        int i = 0;
        for (;;) {
            i++;
            String e = it.next();
            sb.append(e);
            if (! it.hasNext())
                return sb.toString();
            sb.append('\t');
            if (i%4==0){
                sb.append("\n");
            }
        }
    }
};
System.out.println(l);

To have that perfect spacing between columns you want, you have to iterate over all elements twice to find the max width of each column.

But if tabs are OK for you (maybe you just want to copy and paste into a spreadsheet), you may try this way.

0

solved How do i format a ArrayList for printing?