-1
CharSequence listOfWords = ("word");

上面的代码成功定义listOfWords为:“ word”但我需要listOfWorlds在其中包含很多单词,而不仅仅是单个“ word”。

CharSequence listOfWords = ("word")("secondword");

上面的代码是我想要的,但显然不正确。

我希望能够打电话listOfWords并将其定义为“单词”或“第二单词”。CharSequence这里甚至是正确的变量吗?有什么帮助吗?

4

1 回答 1

1

您可能最好使用字符串列表。作为参考,http://docs.oracle.com/javase/7/docs/api/java/lang/String.html,你可以看到String实现CharSequence

public static void main(String eth[]) {
    List<String> listOfWords = new ArrayList<>();
    listOfWords.add("word");
    listOfWords.add("secondWord");
    listOfWords.add("thirdWord");

    // You use your list as followed
    System.out.println(listOfWords.get(0)); // .get(0) gets the first word in the List
    System.out.println(listOfWords.get(1)); // .get(1) gets the second word in the List
    System.out.println(listOfWords.get(2)); // .get(2) gets the third word in the List
}

结果:

在此处输入图像描述

于 2015-04-26T22:37:23.043 回答