List to Array, Array to List

Don’t understand why I keep forgetting this simple snippet. 😛

 

From Array to List

  1. String[] words = {“ace”, “boom”, “crew”, “dog”, “eon”};
  2. List<String> wordList = Arrays.asList(words);

 

From List to Array

  1. List<String> wordList = new ArrayList<String>();
  2. wordList.add(“ace”);
  3. wordList.add(“boom”);
  4. wordList.add(“crew”);
  5. wordList.add(“dog”);
  6. String [] words = wordList.toArray(new String[wordList.size()]);
Advertisements

Published by