About 12,000,000 results
Open links in new tab
  1. declaring ArrayList in java Constructor - Stack Overflow

    Mar 29, 2014 · If you want to just declare it in the constructor you can have the code: ArrayList<String> name = new ArrayList<String>(); Otherwise you can declare it as a field, and then initialize it in the constructor. private ArrayList<String> name; And then in the constructor: name = new ArrayList<String>();

  2. Initialize an ArrayList in Java - GeeksforGeeks

    Apr 7, 2025 · Below are the various methods to initialize an ArrayList in Java. 1. Initialization with add () Syntax: ArrayList<Type> al= new ArrayList<Type> (); al.add (“Geeks”); al.add (“for”); al.add (“Geeks”); Example 1: This example demonstrates how to …

  3. java - initializing ArrayList<> () in methods or constructor - Stack ...

    Feb 17, 2018 · import java.util.ArrayList; public class Team { private String name; private ArrayList<Player> team; public Team(String name) { this.name = name; //how come i have to initialize it here in the constructor to see the full list? this.team = new ArrayList<Player>(); } public void addPlayer(Player player) {

  4. java - Initializing List in constructor or field declaration - Stack ...

    Nov 20, 2015 · In that setting, you need to initialize upon declaration and not in the constructor. Consider the following example that gives you a NullPointerException: public static ArrayList<Dog> dogList; public Dogs(){ dogList = new ArrayList<>(); String breed; public Dog(String breed){ this.breed = breed; public static void main(String[] args) {

  5. ArrayList in Java - GeeksforGeeks

    Mar 12, 2025 · In order to Create an ArrayList, we need to create an object of the ArrayList class. The ArrayList class consists of various constructors which allow the possible creation of the array list.

  6. Initialize an ArrayList in Java - HowToDoInJava

    Aug 4, 2023 · To initialize an ArrayList in a single line statement, get all elements from an array using Arrays.asList method and pass the array argument to ArrayList constructor.

  7. Java ArrayList - W3Schools

    The ArrayList class is a resizable array, which can be found in the java.util package. The difference between a built-in array and an ArrayList in Java, is that the size of an array cannot be modified (if you want to add or remove elements to/from an array, you have to create a new one).

  8. How to Create and Initialize ArrayList in Java - springjava

    Nov 4, 2023 · In this topic, we will learn what is ArrayList in Java, how to create an ArrayList object in Java, the constructors of ArrayList and how to initialize ArrayList in Java.

  9. Initialize an ArrayList in Java: Easy and Advanced Methods

    Oct 26, 2023 · In Java, the simplest way to initialize an ArrayList involves using the ‘new’ keyword and the ‘ArrayList’ constructor. This method is perfect for beginners and is often used in a wide range of Java programs.

  10. Use Array Lists in Java - dummies

    Mar 26, 2016 · To create an array list in Java, you declare an ArrayList variable and call the ArrayList constructor to instantiate an ArrayList object and assign it to the variable:

Refresh