How to convert vector to string in java?

2019-05-31 02:24发布

问题:

I have a vector and it contains the value 5. When i do System.out.Println(vectorVariable), the value outputted is [5]. I want to convert the vectorVariable to string (without the []). I have tried vectorVariable.toString(), which converts it to a string but it still retains the []. So i guess i what i really want is the first element to be returned as a string. How is this possible?

I want to store the value of the vector inside a string without the [].

回答1:

Try System.out.println(vector.get(0)). You are trying to print the entire vector, whereas this will only print the selected index, in this case 0.



回答2:

What you need is to get the value from the vector, rather than print the vector itself.

  System.out.println(vectorVariable.get(0));

This gets the first value and prints it. Of course you need to verify that the vector contains a value before you do this.



回答3:

you can try:

for (String string : vectorVariable) {
    System.out.println(string);
}


标签: java vector