Sometimes you want to create an ArrayList with values, just like you initialize t at the time of declaration, as shown below:
int[] primes = {2, 3, 5, 7, 11, 13, 17};
or
String[] names = {"john", "Johnny", "Tom", "Harry"};
but unfortunately, ArrayList doesn't support such kind of declaration in Java. But don't worry, there is a workaround to declare an ArrayList with values e.g. String, integers, floats or doubles by using Arrays.asList() method, which is nothing but a shortcut to convert an Array to ArrayList.
Read more »
int[] primes = {2, 3, 5, 7, 11, 13, 17};
or
String[] names = {"john", "Johnny", "Tom", "Harry"};
but unfortunately, ArrayList doesn't support such kind of declaration in Java. But don't worry, there is a workaround to declare an ArrayList with values e.g. String, integers, floats or doubles by using Arrays.asList() method, which is nothing but a shortcut to convert an Array to ArrayList.
No comments:
Post a Comment