打印没有括号和逗号的数组 [英] Print array without brackets and commas

查看:50
本文介绍了打印没有括号和逗号的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将 Hangman 游戏移植到 Android,但遇到了一些问题.原始 Java 程序使用控制台,所以现在我必须以某种方式美化输出,使其适合我的 Android 布局.

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.

如何打印没有括号和逗号的数组?该数组包含斜杠,当猜到正确的字母时会被一一替换.

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.

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

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.

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

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("-");
}

我是这样打印的:

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

任何帮助将不胜感激.

推荐答案

基本上,不要使用 ArrayList.toString() - 为自己构建字符串.例如:

Basically, don't use ArrayList.toString() - build the string up for yourself. For example:

StringBuilder builder = new StringBuilder();
for (String value : publicArray) {
    builder.append(value);
}
String text = builder.toString();

(顺便说一下,当它实际上不是一个数组时,我个人不会调用变量 publicArray.)

(Personally I wouldn't call the variable publicArray when it's not actually an array, by the way.)

这篇关于打印没有括号和逗号的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆