2

I believe you can add objects use list.add(); However, is there an alternative way of making an ArrayList of strings? Something like ArrayList("heyey","hgfhgfh","fhfghgf") ?

Adam
  • 8,849
  • 16
  • 67
  • 131
  • 2
    Check out [this SO question][1] about initializing ArrayList in Java. [1]: http://stackoverflow.com/questions/2760995/java-arraylist-initialization – orip Nov 08 '11 at 00:15

2 Answers2

2
List<String> listString = Arrays.asList(new String[] {"heyey","hgfhgfh","fhfghgf"});

With listString being a fixed size list (see Arrays.asList).

If you need a variable size list:

List<String> listString = new ArrayList<String<(Arrays.asList(new String[] {"heyey","hgfhgfh","fhfghgf"}));
jalopaba
  • 8,039
  • 2
  • 44
  • 57
1
List<String> strings = Arrays.asList("ohai", "I", "have", "varargs");

Or if you're kickin' it old school:

List strings = new ArrayList() {{
    add("ohai");
    add("Java 1.4");
    add("lives");
}};
Dave Newton
  • 158,873
  • 26
  • 254
  • 302