Java 打印没有括号和逗号的数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4389480/
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
Print array without brackets and commas
提问by SQDK
I'm porting a Hangman game to Android and have met a few problems. The original Java program used the console, so now I have to somehow beautify the output so that it fits my Android layout.
我正在将 Hangman 游戏移植到 Android,但遇到了一些问题。原始 Java 程序使用控制台,所以现在我必须以某种方式美化输出,使其适合我的 Android 布局。
How do I print an array without the brackets and commas? The array contains slashes and gets replaced one-by-one when the correct letter is guessed.
如何打印没有括号和逗号的数组?该数组包含斜杠,并在猜出正确的字母时被一一替换。
I am using the usual .toString()
function of the ArrayList
class and my output is formatted like: [ a, n, d, r, o, i, d ]
. I want it to simply print out the array as a single String
.
我正在使用类的常用.toString()
功能,ArrayList
我的输出格式如下:[ a, n, d, r, o, i, d ]
. 我希望它简单地将数组打印为单个String
.
I fill the array using this bit of code:
我使用这段代码填充数组:
List<String> publicArray = new ArrayList<>();
for (int i = 0; i < secretWordLength; i++) {
hiddenArray.add(secretWord.substring(i, i + 1));
publicArray.add("-");
}
And I print it like this:
我这样打印:
TextView currentWordView = (TextView) findViewById(R.id.CurrentWord);
currentWordView.setText(publicArray.toString());
Any help would be appreciated.
任何帮助,将不胜感激。
采纳答案by Jon Skeet
Basically, don't use ArrayList.toString()
- build the string up for yourself. For example:
基本上,不要使用ArrayList.toString()
- 为自己构建字符串。例如:
StringBuilder builder = new StringBuilder();
for (String value : publicArray) {
builder.append(value);
}
String text = builder.toString();
(Personally I wouldn't call the variable publicArray
when it's not actually an array, by the way.)
(publicArray
顺便说一下,当它实际上不是一个数组时,我个人不会调用该变量。)
回答by AlexR
first
第一的
StringUtils.join(array, "");
StringUtils.join(array, "");
second
第二
Arrays.asList(arr).toString().substring(1).replaceFirst("]", "").replace(", ", "")
Arrays.asList(arr).toString().substring(1).replaceFirst("]", "").replace(", ", "")
EDIT
编辑
probably the best one: Arrays.toString(arr)
可能是最好的: Arrays.toString(arr)
回答by user489041
Replace the brackets and commas with empty space.
用空格替换括号和逗号。
String formattedString = myArrayList.toString()
.replace(",", "") //remove the commas
.replace("[", "") //remove the right bracket
.replace("]", "") //remove the left bracket
.trim(); //remove trailing spaces from partially initialized arrays
回答by jazkat
I used join() function like:
我使用了 join() 函数,如:
i=new Array("Hi", "Hello", "Cheers", "Greetings");
i=i.join("");
Which Prints:HiHelloCheersGreetings
哪个打印:HiHelloCheersGreetings
See more: Javascript Join - Use Join to Make an Array into a String in Javascript
回答by David
the most simple solution for removing the brackets is,
删除括号的最简单的解决方案是,
1.convert the arraylist into string with .toString() method.
1. 使用 .toString() 方法将数组列表转换为字符串。
2.use String.substring(1,strLen-1).(where strLen is the length of string after conversion from arraylist).
2.use String.substring(1,strLen-1).(其中strLen是从arraylist转换后的字符串长度)。
3.Hurraaah..the result string is your string with removed brackets.
3.Hurraaah..结果字符串是去掉括号的字符串。
hope this is useful...:-)
希望这是有用的...:-)
回答by icastell
You can use joinmethod from android.text.TextUtilsclass like:
您可以使用android.text.TextUtils类中的join方法,例如:
TextUtils.join("",array);
回答by Ayush bhanu
Just initialize a String
object with your array
只需String
用您的数组初始化一个对象
String s=new String(array);
回答by Namo
If you use Java8 or above, you can use with stream() with native.
如果你使用 Java8 或更高版本,你可以使用 with stream() 和 native。
publicArray.stream()
.map(Object::toString)
.collect(Collectors.joining(" "));
References
参考
回答by ernest_k
With Java 8 or newer, you can use String.join
, which provides the same functionality:
对于 Java 8 或更高版本,您可以使用String.join
,它提供相同的功能:
Returns a new String composed of copies of the CharSequence elements joined together with a copy of the specified delimiter
返回由 CharSequence 元素的副本与指定分隔符的副本连接在一起的新字符串
String[] array = new String[] { "a", "n", "d", "r", "o", "i", "d" };
String joined = String.join("", array); //returns "android"
With an array of a different type, one should convert it to a String array or to a char sequence Iterable:
对于不同类型的数组,应将其转换为 String 数组或 char 序列 Iterable:
int[] numbers = { 1, 2, 3, 4, 5, 6, 7 };
//both of the following return "1234567"
String joinedNumbers = String.join("",
Arrays.stream(numbers).mapToObj(String::valueOf).toArray(n -> new String[n]));
String joinedNumbers2 = String.join("",
Arrays.stream(numbers).mapToObj(String::valueOf).collect(Collectors.toList()));
The first argument to String.join
is the delimiter, and can be changed accordingly.
第一个参数String.join
是分隔符,可以相应地更改。
回答by Marko Panushkovski
I have used Arrays.toString(array_name).replace("[","").replace("]","").replace(", ",""); as I have seen it from some of the comments above, but also i added an additional space character after the comma (the part .replace(", ","")), because while I was printing out each value in a new line, there was still the space charactershifting the words. It solved my problem.
我用过 Arrays.toString(array_name).replace("[","").replace("]","").replace(", ",""); 正如我从上面的一些评论中看到的那样,但我还在逗号之后添加了一个额外的空格字符(部分.replace(", ","")),因为当我在新行中打印出每个值时,仍然有空格字符在移动单词。它解决了我的问题。