打印数组没有括号和逗号

时间:2022-03-27 01:51:40

I have previously written a Hangman game for my java class in school. I am now currently porting it to Android and have met a few problems. The original java program wrote all outputs to the console. Now i have to somehow beutify the output so that it fits the layout that i have designed.

我以前在学校为我的java课程编写了一个Hangman游戏。我现在正在将它移植到Android并遇到了一些问题。原始java程序将所有输出写入控制台。现在我必须以某种方式使输出变得更好,以便它适合我设计的布局。

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.

我正在使用ArrayList类的通常的.toString()函数,我的输出格式如下:[a,n,d,r,o,i,d]。我希望它只是将数组打印为单个字符串。

This problems seems so simple although i haven't been able to find a solution to it. Not by using the search function either.

这个问题似乎很简单,虽然我还没有找到解决方案。不是通过使用搜索功能。

I fill the array using this bit of code:

我使用这段代码填充数组:

for(int i = 0;i<secretWordLength;i++){
        hiddenArray.add(secretWord.substring(i,i+1));
        publicArray.add("-");
    }

And i print it like this:

我打印它像这样:

final TextView currentWordView = (TextView) findViewById(R.id.CurrentWord);
    currentWordView.setText(publicArray.toString());

Any help would be appreciated

任何帮助,将不胜感激

8 个解决方案

#1


37  

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。)

#2


81  

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

#3


17  

You can use join method from android.text.TextUtils class like:

您可以使用android.text.TextUtils类中的join方法,如:

TextUtils.join("",array);

#4


10  

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)

#5


3  

the most simple solution for removing the brackets is,

删除括号最简单的解决方案是,

1.convert the arraylist into string with .toString() method.

1.使用.toString()方法将arraylist转换为字符串。

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...:-)

希望这很有用...... :-)

#6


3  

If you use Java8 or above, you can use with stream() with native.

如果您使用Java8或更高版本,则可以将stream()与native一起使用。

publicArray.stream()
        .map(Object::toString)
        .collect(Collectors.joining(" "));

References

#7


2  

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

查看更多:Javascript Join - 使用Join在Javascript中将数组转换为字符串

#8


-2  

Just initialize a String object with your array

只需使用您的数组初始化String对象

String s=new String(array);

#1


37  

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。)

#2


81  

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

#3


17  

You can use join method from android.text.TextUtils class like:

您可以使用android.text.TextUtils类中的join方法,如:

TextUtils.join("",array);

#4


10  

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)

#5


3  

the most simple solution for removing the brackets is,

删除括号最简单的解决方案是,

1.convert the arraylist into string with .toString() method.

1.使用.toString()方法将arraylist转换为字符串。

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...:-)

希望这很有用...... :-)

#6


3  

If you use Java8 or above, you can use with stream() with native.

如果您使用Java8或更高版本,则可以将stream()与native一起使用。

publicArray.stream()
        .map(Object::toString)
        .collect(Collectors.joining(" "));

References

#7


2  

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

查看更多:Javascript Join - 使用Join在Javascript中将数组转换为字符串

#8


-2  

Just initialize a String object with your array

只需使用您的数组初始化String对象

String s=new String(array);