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

Java Vector toString() Method

Java Vector toString() Method

Java Vector Java Vector


toString() is a method in Java's Vector A built-in method of the class, used to return the string representation of the vector (Vector). This method is inherited from AbstractCollection class and overridden by the Vector class to implement a specific string representation.

Method Syntax


public String toString()

Return Value

Returns a string representing Vector all elements in the collection.


Method details

Vector 's toString() The method returns the string in the following format:

  1. The string starts with a left square bracket [ at the beginning
  2. followed by the string representations of all elements, separated by a comma , and a space `` separator
  3. and ends with a right square bracket ] at the end

Internal Implementation

This method actually calls AbstractCollection The class's toString() implementation, which:

  1. Use an iterator to traverse Vector all elements in the
  2. Calls each element's toString() Method
  3. Joins these strings with ", " together
  4. Adds at the outermost level [ and ]

Usage examples

Basic Example

Example

import

java.util.Vector

;

public

class

VectorToStringExample

{

   

public

static

void

main

(

String

[

]

args

)

{

       

// Create a Vector

        Vector

<
String
fruits
=
new
Vector
<>
(
)

;

       

       

// Add elements

        fruits.

add

(

"Apple"

)

;

        fruits.

add

(

"Banana"

)

;

        fruits.

add

(

"Orange"

)

;

       

       

// Use the toString() method

       

String

vectorString

=

fruits.

toString

(

)

;

       

       

System

.

out

.

println

(

vectorString

)

;

   

}

}

Output result

[Apple, Banana, Orange]

Example with Elements of Different Types

Example

import

java.util.Vector

;

public

class

MixedVectorToString

{

   

public

static

void

main

(

String

[

]

args

)

{

        Vector

<
Object
mixedVector
=
new
Vector
<>
(
)

;

       

        mixedVector.

add

(

"String"

)

;

        mixedVector.

add

(

123

)

;

        mixedVector.

add

(

45.67

)

;

        mixedVector.

add

(

true

)

;

       

       

System

.

out

.

println

(

mixedVector.

toString

(

)

)

;

   

}

}

Output result

[String, 123, 45.67, true]

Notes

1. Element Order

toString() The order of elements in the string returned by the method is the same as their order in the Vector , because the Vector is an ordered collection.

2. The Case of null Elements

If both the Vector contains null element, it is converted to the string "null":

Example

Vector

<
String
vectorWithNull
=
new
Vector
<>
(
)

;

vectorWithNull.

add

(

"First"

)

;

vectorWithNull.

add

(

null

)

;

vectorWithNull.

add

(

"Last"

)

;

System

.

out

.

println

(

vectorWithNull.

toString

(

)

)

;

Output result

[First, null, Last]

3. Handling Custom Objects

For custom class objects,toString() method calls that object's toString() method. If the custom class does not override toString(), it uses the Object class's default implementation.

Example

class

Person

{

   

String

name

;

   

int

age

;

   

    Person

(

String

name,

int

age

)

{

       

this

.

name

=

name

;

       

this

.

age

=

age

;

   

}

}

public

class

CustomObjectToString

{

   

public

static

void

main

(

String

[

]

args

)

{

        Vector

<
Person
people
=
new
Vector
<>
(
)

;

        people.

add

(

new

Person

(

"Alice"

,

25

)

)

;

        people.

add

(

new

Person

(

"Bob"

,

30

)

)

;

       

       

System

.

out

.

println

(

people.

toString

(

)

)

;

   

}

}

Output (without overriding toString())

[Person@15db9742, Person@6d06d69c]

Improved Person Class

Example

class

Person

{

   

String

name

;

   

int

age

;

   

    Person

(

String

name,

int

age

)

{

       

this

.

name

=

name

;

       

this

.

age

=

age

;

   

}

   

    @Override

   

public

String

toString

(

)

{

       

return

"Person{name='"

name

"', age="

age

"}"

;

   

}

}

Improved Output

[Person{name='Alice', age=25}, Person{name='Bob', age=30}]

Differences from Arrays.toString()

Although Vector 's toString() and Arrays.toString() both return similar string formats, but they have the following differences:

  1. Different Applicable Objects:

  2. Vector.toString(): Used for Vector Union
  3. Arrays.toString(): Used for arrays
  4. - `Vector.toString()`: Used for `Vector` collections - `Arrays.toString()`: Used for arrays
    1. Different Implementation Approaches:

    2. Vector.toString(): Implemented based on iterators
    3. Arrays.toString(): Directly accesses array elements
    4. - `Vector.toString()`: Implemented based on iterators - `Arrays.toString()`: Directly accesses array elements
      1. Performance considerations:

      2. For large collections,Vector.toString() may have extra iteration overhead
      3. - For large collections, `Vector.toString()` may have extra iteration overhead

        Practical Application Scenarios

        1. Debugging and Logging

        toString() The method is often used for quick inspection during debugging Vector of contents:

        Example

        Vector

        <
        Integer
        numbers
        =
        getNumbersFromSomewhere
        (
        )

        ;

        System

        .

        out

        .

        println

        (

        "Current vector contents: "

        numbers.

        toString

        (

        )

        )

        ;

        2. Simple Data Display

        When you need to simply display Vector contents, use:

        Example

        Vector

        <
        String
        options
        =
        getMenuOptions
        (
        )

        ;

        JOptionPane

        .

        showMessageDialog

        (

        null

        ,

        "Available options: "

        options.

        toString

        (

        )

        )

        ;

        3. Combining with Other String Operations

        Example

        Vector

        <
        String
        words
        =
        new
        Vector
        <>
        (
        Arrays

        .

        asList

        (

        "Hello"

        ,

        "World"

        ,

        "Java"

        )

        )

        ;

        String

        sentence

        =

        words.

        toString

        (

        )

                             .

        replace

        (

        "["

        ,

        ""

        )

                             .

        replace

        (

        "]"

        ,

        ""

        )

                             .

        replace

        (

        ","

        ,

        ""

        )

        ;

        System

        .

        out

        .

        println

        (

        sentence

        )

        ;

         

        // Output: Hello World Java


        Performance considerations

        Although toString() method is convenient, but when handling large Vector , note the following:

        1. Memory Usage: For very large Vector, the generated string may consume a lot of memory
        2. Time Overhead: Needs to traverse the entire Vector to build the string

        For performance-sensitive scenarios, consider:

        1. Output only some elements
        2. Use StringBuilder Customize the output format
        3. For logging, use the logging framework's lazy evaluation feature

        Summary

        Vector 's toString() method is a simple but practical tool that:

        1. Provides a quick way to inspect Vector of contents
        2. Returns a uniformly formatted string representation
        3. Automatically handles elements of various types
        4. Is a good helper for debugging and simple output

        Understanding and properly using this method can greatly improve development efficiency and code readability.

        Java Vector Java Vector