我想让它变得更简单

时间:2016-11-22 09:45:04

标签: java

我这里有我的java代码,目标是输出应该如下所示: 问题NEL01L01A021问题NEL01L01A021英语11-22-2016 NEL01L01A021 / NEL01L01A021-problem.txt

我已经完成了它,但我想让它变得更简单。你能帮我么?         import java.util.ArrayList;

public class TestTabSeparator {

String str;
ArrayList<String> strings = new ArrayList<>();

TestTabSeparator(ArrayList<String> strings) {
    this.strings = strings;
}

void showArrayList() {

    for (int index = 0; index < this.strings.size(); index++) {
        System.out.print(this.strings.get(index) + "   ");
    }
}
}

另一堂课:

import java.util.Arrays;
import java.util.ArrayList;

public class TabSeparator {

public static void main(String[] args) {

    ArrayList<String> stringList = new ArrayList();

    String topicName = "problem";
    String title = "NEL01L01A021";
    String idTitle = "問題";
    String questionNo = "NEL01L01A021";
    String keyword = "英語";
    String comment = "11-22-2016";
    String bodyKey = "NEL01L01A021/NEL01L01A021-problem.txt";

    String[] str2 = new String[7];
    str2[0] = topicName;
    str2[1] = title;
    str2[2] = idTitle;
    str2[3] = questionNo;
    str2[4] = keyword;
    str2[5] = comment;
    str2[6] = bodyKey;

    stringList.addAll(Arrays.asList(str2));
    TestTabSeparator strList = new TestTabSeparator(stringList);
    strList.showArrayList();
}
}

2 个答案:

答案 0 :(得分:0)

我有两个建议。首先,您可以直接在变量上执行Arrays.asList,而不是先创建数组。

ArrayList<String> stringList = Arrays.asList(topicName, title, idTitle, questionNo, keyword, comment, bodyKey)

使用String.join可以简化第二个showArrayList方法。

void showArrayList() {
    System.out.print(String.join(" ", strings));
}

答案 1 :(得分:0)

我是这样做的:

TypeError                                 Traceback (most recent call last)
<ipython-input-18-21a3dc9eb8ad> in <module>()
     33 print (type(i))
     34 for i in bvoca:
---> 35         vocabulary.write(i.encode('UTF-8')+"\n")
     36 print ("successfully create vocabulary!")
     37 

TypeError: can't concat bytes to str