Java Vector toString() Method
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:
- The string starts with a left square bracket
[at the beginning - followed by the string representations of all elements, separated by a comma
,and a space `` separator - and ends with a right square bracket
]at the end
Internal Implementation
This method actually calls AbstractCollection The class's toString() implementation, which:
- Use an iterator to traverse
Vectorall elements in the - Calls each element's
toString()Method - Joins these strings with
", "together - 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:
-
Different Applicable Objects:
Vector.toString(): Used for Vector UnionArrays.toString(): Used for arrays-
Different Implementation Approaches:
Vector.toString(): Implemented based on iteratorsArrays.toString(): Directly accesses array elements-
Performance considerations:
Vector.toString() may have extra iteration overheadPractical 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:
- Memory Usage: For very large
Vector, the generated string may consume a lot of memory - Time Overhead: Needs to traverse the entire
Vectorto build the string
For performance-sensitive scenarios, consider:
- Output only some elements
- Use
StringBuilderCustomize the output format - For logging, use the logging framework's lazy evaluation feature
Summary
Vector 's toString() method is a simple but practical tool that:
- Provides a quick way to inspect
Vectorof contents - Returns a uniformly formatted string representation
- Automatically handles elements of various types
- 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