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 the Arrays.asList() method, which is nothing but a shortcut to convert an Array to ArrayList.
This is how you declare an ArrayList of Integer values. You can do the same to create an ArrayList with String objects as well, e.g.
Similarly here is how you should create ArrayList with float values:
don't forget the suffix 'f', it's important because by default floating-point numbers are double in Java.
Some programmer's also like to declare a List with values in one line as:
This is Ok to print values, but it's not an ArrayList. You cannot add or remove elements into this list but when you create an ArrayList like new ArrayList(Arrays.asList()), you get a regular ArrayList object, which allows you to add, remove and set values.
Here is a nice summary of code examples of how to make an ArrayList of values in Java:
That's all about how to declare an ArrayList with values in Java. You can use this technique to declare an ArrayList of integers, String, or any other object. It's truly useful for testing and demo purpose, but I have also used this to create an ArrayList of an initial set of fixed values.
Related Java ArrayList Tutorials
If you like this Java tip and wants to learn more about how to work with ArrayList in Java, check out following Java tutorials and articles:
Further Reading
If you a beginner in Java and just started learning, then you can read Head First Java 2nd Edition, but if you are an experienced Java developer and looking forward to taking your knowledge of Java Collections framework to next level then read Java Generics and Collections from Maurice Naftalin.
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 the Arrays.asList() method, which is nothing but a shortcut to convert an Array to ArrayList.
Declaring ArrayList with values in Java
Here is a code example to show you how to initialize ArrayList at the time of declaration:ArrayList<Integer> numbers = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6));
This is how you declare an ArrayList of Integer values. You can do the same to create an ArrayList with String objects as well, e.g.
ArrayList<String> cities = new ArrayList<>( Arrays.asList("London", "Tokyo", "New York"));
Similarly here is how you should create ArrayList with float values:
ArrayList<Float> floats = new ArrayList<>(Arrays.asList(3.14f, 6.28f, 9.56f));
don't forget the suffix 'f', it's important because by default floating-point numbers are double in Java.
Some programmer's also like to declare a List with values in one line as:
List<Integer> listOfInts = Arrays.asList(1, 2, 3);
This is Ok to print values, but it's not an ArrayList. You cannot add or remove elements into this list but when you create an ArrayList like new ArrayList(Arrays.asList()), you get a regular ArrayList object, which allows you to add, remove and set values.
Here is a nice summary of code examples of how to make an ArrayList of values in Java:
That's all about how to declare an ArrayList with values in Java. You can use this technique to declare an ArrayList of integers, String, or any other object. It's truly useful for testing and demo purpose, but I have also used this to create an ArrayList of an initial set of fixed values.
Related Java ArrayList Tutorials
If you like this Java tip and wants to learn more about how to work with ArrayList in Java, check out following Java tutorials and articles:
- What is the difference between an array and an ArrayList in Java? (answer)
- Difference between LinkedList and ArrayList in Java? (answer)
- How to remove elements from ArrayList in Java? (example)
- What is the difference between HashSet and ArrayList in Java? (answer)
- How to reverse an ArrayList in Java? (solution)
- What is the difference between ArrayList and HashMap in Java? (answer)
- How to loop over ArrayList in Java? (answer)
- How to sort an ArrayList? (solution)
- How to get the subList() from an ArrayList in Java? (solution)
- The difference between the length and size of ArrayList? (answer)
- How to sort the ArrayList in decreasing order? (example)
- Difference between CopyOnWriteArrayList and synchronized ArrayList in Java? (answer)
- What is the difference between ArrayList and Vector in Java? (answer)
- How to avoid ConcurrentModificationException while looping over ArrayList? (solution)
Further Reading
If you a beginner in Java and just started learning, then you can read Head First Java 2nd Edition, but if you are an experienced Java developer and looking forward to taking your knowledge of Java Collections framework to next level then read Java Generics and Collections from Maurice Naftalin.
And lastly one question for you? What happen if the underlying array of ArrayList get full? At what time ArrayList resize itself.
Good.
ReplyDeleteBad
ReplyDeleteand the Ugly
ReplyDeleteIf you have just one element, use:
ReplyDeleteList listOfInts = Collections.singletonList(1);
ok
ReplyDeleteNot helpful if you have to add elements, but also I was lazy and only skimmed through it
ReplyDelete