Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java ArrayList toString() Method · Private Archive

Java ArrayList toString() Method · Private Archive

Java ArrayList Java ArrayList

Java ArrayList toString() Method

The toString() method converts an ArrayList object into a string.


arraylist.toString()

**Note:** arraylist is an object of the ArrayList class.

Parameter Description:

Return Value

The syntax of the toString() method is:

Example

Returns the string representation of the arraylist.

Example

import

java.util.ArrayList

;

import

java.util.Comparator

;

class

Main

{

   

public

static

void

main

(

String

[

]

args

)

{

       

// Create a dynamic array

        ArrayList

<
String
sites
=
new
ArrayList
<>
(
)

;

       

        sites.

add

(

"Runoob"

)

;

        sites.

add

(

"Google"

)

;

        sites.

add

(

"Wiki"

)

;

        sites.

add

(

"Taobao"

)

;

       

System

.

out

.

println

(

"Website list: "

sites

)

;

       

Converting ArrayList to String type:

       

String

list

=

sites.

toString

(

)

;

       

System

.

out

.

println

(

"String: "

list

)

;

   

}

}

The output of the above program is:

  
网站列表: [Runoob, Google, Wiki, Taobao]
String: [Runoob, Google, Wiki, Taobao]

In the example above, we created an array named sites.

Note this line:

String list = sites.toString();

// Convert ArrayList to String type

We used the toString() method to convert the arraylist into a string; this method converts the entire arraylist into a String type.

Java ArrayList Java ArrayList