Home > Back-end >  Removing the "[]" from an ArrayList<ArrayList<String>> println
Removing the "[]" from an ArrayList<ArrayList<String>> println

Time:10-21

Hoping to find a way to remove the "[]" that encloses the output of my code.

Controller:

view.linesDisplay(model.getFpData().getLines(100,103));

ArrayList<ArrayList> code.

@Override
public ArrayList<ArrayList<String>> getLines(int firstLine, int lastLine) {
    ArrayList<ArrayList<String>> listLines = new ArrayList<ArrayList<String>>();
    
    for (int i = firstLine; i < lastLine; i  ){
        listLines.add(getLine(i));
        
    }
    return listLines;

And the print side:

public void linesDisplay(ArrayList<ArrayList<String>> arrOfarr)
{    
    for (ArrayList<String> s : arrOfarr)
    {
        System.out.println(s   " ");
    }

}

Output is:

[62, G, Zach Simpson, 6'3", 272, Hollidaysburg, Pa., Hollidaysburg Area] 
[24, S, Anthony Smith, 6'0", 206, Dover, N.J., Pope John XXIII]
[47, LB, Brandon Smith, 6'0", 223, Winfield, Pa., Lewisburg]

Please let me know if there's anything more you need to know.

CodePudding user response:

You can use String#substring to remove the first and last characters.

for (ArrayList<String> s : arrOfarr){
    String str = s.toString();   
    System.out.println(str.substring(1, str.length() - 1));
}

CodePudding user response:

By concatenating s, you're implicitly calling the default collection toString(), which joins the elements on ", " and encloses it all in brackets. You can do the join yourself and omit the brackets:

System.out.println(String.join(", ", s)   " ");

CodePudding user response:

You have a list-of-a-list. Thus, you need a for-in-a-for.

linesDisplay(ArrayList<ArrayList<String>> arrOfarr)

for (ArrayList<String> s : arrOfarr)
   {
       System.out.println(s   " ");
   }

This tries to execute s " "; given that s is an arbitrary object, and the right hand side is specifically a string, java sees this as string concatenation, and turns the object into a string by calling .toString() on that list. That is the source of your [].

Either write another for loop (inside that for loop), or use a string joiner from various libraries, or use the joining collector:

String whatYouWanted = String.join(", ", s);

The join method will take a collection of String objects (s, here), and concatenates them together, using the provided string as infix separator. Contrast with s.toString(), which does that and adds those [ and ] you want to get rid of.

list.stream().collect(Collectors.joining(","));

CodePudding user response:

You have a list of lists. When you are iterating first time, you go over the inner arraylists. So when printing (s " ") you print the whole arraylist. You should iterate one more time inside the first loop to print the actual items of arraylists.

for (ArrayList<String> s : arrOfarr)
    {
        for (String item : s)
        {
            System.out.println(item   " ");
        }
    }

And you can learn java streams to handle this even better

CodePudding user response:

The problem is the toString() method, which is called by println() (whether or not you print s " " or just s), of List wraps a csv of its elements in [ and ].

So instead use String.join():

public void linesDisplay(List<List<String>> arrOfarr) {    
    for (List<String> s : arrOfarr) {
        System.out.println(String.join(", ", s));
    }
}

Note also best practice of using the abstract type List instead of the concrete type ArrayList - see Liskov substitution principle.

  • Related