java 如何在java中将向量转换为字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9895186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 22:47:50  来源:igfitidea点击:

How to convert vector to string in java?

javavector

提问by Rory Lester

I have a vectorand it containsthe 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?

我有一个vector和它containsvalue 5. 当我这样做时System.out.Println(vectorVariable),输出的值是[5]. 我想将 vectorVariable 转换为字符串(没有 [])。我试过了vectorVariable.toString(),它把它转换成一个字符串,但它仍然保留了 []。所以我想我真正想要的是作为字符串返回的第一个元素。这怎么可能?

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

我想将向量的值存储在没有 [] 的字符串中。

回答by Dan W

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.

试试System.out.println(vector.get(0))。您正在尝试打印整个向量,而这只会打印选定的索引,在本例中为 0。

回答by Oscar Castiblanco

you can try:

你可以试试:

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

回答by Vincent Ramdhanie

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.

这将获取第一个值并打印它。当然,在执行此操作之前,您需要验证向量是否包含一个值。