We help IT Professionals succeed at work.

How do you print an ArrayList so that it displays as a string?

Instead of my ArrayList printing as:
 [1, 2, 3, 4]

I need it to print as:
1, 2, 3, 4.

How can this be done in Java?
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
For ease you can do
String s = list.toString().replaceAll("\\^[|\\]$", "");

Open in new window

Awarded 2011
Awarded 2011

Commented:
One awey  to do it is

jsuts

String s = ar.toString();

System.out.println(s.substring(1,s.length()-1)) + ".");

Author

Commented:
Not sure how that would work.  It's a randomly generated ArrayList of varying sizes based on user choice.  
Awarded 2011
Awarded 2011
Commented:
      String s101 = ar.toString();

System.out.println(s101.substring(1,s101.length()-1) + ".");

Open in new window

this is without any misprints - tested with my arraylist:
62, 64, 7, 93, 19, 55, 15, 0, 54, 26, 68, 23, 72, 62, 63, 98, 29, 50, 55, 48, 29, 79, 24, 12, 65.

Open in new window

CERTIFIED EXPERT
Top Expert 2016

Commented:
>>Not sure how that would work.

Did you try it?
Awarded 2011
Awarded 2011

Commented:
this is your random list from previous question printed:
     Random r = new Random();
         ArrayList<Integer> ar = new ArrayList<Integer>();
        int count = 0;
        while(count < 25) {

        ar.add(r.nextInt(100));
        count++ ;

        }

     //   System.out.println(ar);

        String s101 = ar.toString();

System.out.println(s101.substring(1,s101.length()-1) + ".");

Open in new window


62, 64, 7, 93, 19, 55, 15, 0, 54, 26, 68, 23, 72, 62, 63, 98, 29, 50, 55, 48, 29, 79, 24, 12, 65.

Open in new window

Awarded 2011
Awarded 2011

Commented:
This is very simple - you make a string with square brcakets with toString() method - and sthen suyubstring
reoves the first ad last cgharcatcter and ads period in the end
Awarded 2011
Awarded 2011

Commented:
This is another varaint
(you print each one before last with the comma and apspace after it
and the last one with period and carriage return):
        for(int j=0; j<ar.size(); j++){

            if(j < ar.size()-1){
                System.out.print(ar.get(j) + ", ");
            }
            else System.out.println(ar.get(j) + ".");



        }

Open in new window


77, 49, 62, 49, 12, 79, 95, 32, 47, 51, 3, 24, 36, 99, 63, 63, 6, 10, 8, 78, 53, 32, 94, 42, 4.

Open in new window

Author

Commented:
CEHJ - yes I did try it.  I get an error message when i run it:

Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 6
\^[|\]$
      ^
CERTIFIED EXPERT
Top Expert 2016
Commented:
Sorry - typo
String s = list.toString().replaceAll("^\\[|\\]$", "");

Open in new window

Author

Commented:
for_yan - The first option works.  I think that will be the least complicated to use than the for - loop variant.  Thanks again!

Author

Commented:
CEHJ -  that is definitely much simpler.  Thanks!
CERTIFIED EXPERT
Top Expert 2016

Commented:
:)

Explore More ContentExplore courses, solutions, and other research materials related to this topic.