-1
CharSequence listOfWords = ("word");

the code above successfully defines listOfWords as: "word" But I need listOfWorlds to have lots of words in it, not just the single "word".

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

above code is what I want, but obviously incorrect.

I'd like to be able to call listOfWords and have it be defined as "word" OR "secondword". Is CharSequence even the correct variable here? Any help?

  • A `CharSequence` is a sequence of characters, like its name says; it won't "store" words. Also, if you look a little more carefully, you'll see that 1. methods of this interface are pretty much telling you that already, and 2. `String` implements `CharSequence`. – fge Apr 26 '15 at 22:38

1 Answers1

1

You're probably better off using a List of String(s). For reference, http://docs.oracle.com/javase/7/docs/api/java/lang/String.html, you can see that String implements 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
}

Results:

enter image description here

Shar1er80
  • 9,001
  • 2
  • 20
  • 29