By Alexandru


2010-04-09 12:20:20 8 Comments

How do I convert an array to a list in Java?

I used the Arrays.asList() but the behavior (and signature) somehow changed from Java SE 1.4.2 (docs now in archive) to 8 and most snippets I found on the web use the 1.4.2 behaviour.

For example:

int[] spam = new int[] { 1, 2, 3 };
Arrays.asList(spam)
  • on 1.4.2 returns a list containing the elements 1, 2, 3
  • on 1.5.0+ returns a list containing the array spam

In many cases it should be easy to detect, but sometimes it can slip unnoticed:

Assert.assertTrue(Arrays.asList(spam).indexOf(4) == -1);

Related Questions

Sponsored Content

96 Answered Questions

[SOLVED] How can I remove a specific item from an array?

  • 2011-04-23 22:17:18
  • Walker
  • 6824892 View
  • 8406 Score
  • 96 Answer
  • Tags:   javascript arrays

36 Answered Questions

[SOLVED] Create ArrayList from array

26 Answered Questions

29 Answered Questions

[SOLVED] How do I determine whether an array contains a particular value in Java?

  • 2009-07-15 00:03:21
  • Mike Sickler
  • 1979364 View
  • 2293 Score
  • 29 Answer
  • Tags:   java arrays

87 Answered Questions

[SOLVED] Is Java "pass-by-reference" or "pass-by-value"?

40 Answered Questions

[SOLVED] How to make a flat list out of list of lists?

27 Answered Questions

[SOLVED] How do I check if a list is empty?

  • 2008-09-10 06:20:11
  • Ray
  • 2924972 View
  • 3234 Score
  • 27 Answer
  • Tags:   python list

44 Answered Questions

[SOLVED] How do I convert a String to an int in Java?

40 Answered Questions

[SOLVED] For-each over an array in JavaScript

59 Answered Questions

[SOLVED] How do I read / convert an InputStream into a String in Java?

Sponsored Content