TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to JAVA

Java Initialize ArrayList

Initialize ArrayLists with String arrays and for-loops. Use Collections.addAll.
Initialize ArrayList. With ArrayLists we have an expandable, fast collection. But often we must initialize them with values. The syntax for ArrayList initialization is confusing.ArrayList
With special methods, like Collections.addAll, we can add many elements at once. And ArrayList provides a constructor that copies another ArrayList collection. This is useful.
Initialize with array. In this example, we use a String array to initialize an ArrayList. We create an empty ArrayList of Strings.

Then: We invoke the Collections.addAll method. This method receives two arguments.

Collections.addAll

Argument 1: The first argument to Collections.addAll is the ArrayList we want to add elements to.

Argument 2: This is the String array reference. These elements are added to the end of the ArrayList.

Java program that initializes ArrayList with String array import java.util.ArrayList; import java.util.Collections; public class Program { public static void main(String[] args) { // Initialize ArrayList with contents of string array. String[] array = {"bird", "fish"}; ArrayList<String> animals = new ArrayList<>(); Collections.addAll(animals, array); // The ArrayList now contains all strings. System.out.println(animals); } } Output [bird, fish]
Another ArrayList. Here we initialize an ArrayList with the contents of another existing ArrayList. We combine ArrayLists. We use a copying constructor.

Copy: We use the ArrayList's copy constructor to add all the elements from one ArrayList to the new one.

Java program that uses ArrayList copy constructor import java.util.ArrayList; public class Program { public static void main(String[] args) { // Create an ArrayList with two elements. ArrayList<Integer> list1 = new ArrayList<>(); list1.add(10); list1.add(20); // Initialize an ArrayList from another ArrayList of the same type. ArrayList<Integer> list2 = new ArrayList<>(list1); list2.add(30); // The collections are separate. System.out.println(list1); System.out.println(list2); } } Output [10, 20] [10, 20, 30]
One statement. Sometimes it is convenient to add many values in one statement. We addAll to add 4 strings to an ArrayList. We initialize the ArrayList.
Java program that uses addAll, many arguments import java.util.ArrayList; import java.util.Collections; public class Program { public static void main(String[] args) { // Initialize many values in one statement. ArrayList<String> colors = new ArrayList<>(); Collections.addAll(colors, "blue", "red", "green", "yellow"); // Print colors. System.out.println(colors); } } Output [blue, red, green, yellow]
Int, For. Here we use a for-loop to initialize an Integer ArrayList. We add ints to the ArrayList, and these are cast to Integers to be stored in the ArrayList.Cast

Capacity: We use a capacity on our ArrayList. This reduces resizes when many elements are added—space is reserved.

Java program that uses ints, for-loop, initializes ArrayList import java.util.ArrayList; public class Program { public static void main(String[] args) { // Initialize with capacity. ArrayList<Integer> values = new ArrayList<>(10); // Initialize an ArrayList of Integers in a loop. for (int i = 0; i < 10; i++) { values.add(i); } // Print ArrayList and its size. System.out.println(values); System.out.println(values.size()); } } Output [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 10
Ints, Integers. An ArrayList can store Integer elements, but not ints. It must store a class. Integer is a class that encapsulates an int.ArrayList int, Integer
A summary. The first step to using an ArrayList is initializing it. We often must add initial data. We do this with Collections.addAll, and a copy constructor.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf