How to Create an ArrayList in Java


In this example, we will learn the method to create an ArrayList in Java.

Source Code

package com.beginner.examples;

import java.util.ArrayList;

public class CreateArrayList {
  public static void main(String[] args) { 
    ArrayList arrays = new ArrayList(); // create an ArrayList
    arrays.add("a");
    arrays.add("b");
    System.out.println(arrays);
  } 
}

Output:

[a, b]

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

How to Create an ArrayList in Java


In this example, we will learn you how to create an ArrayList that should store numbers in Java.

Source Code

package com.beginner.examples;

import java.util.ArrayList;

public class CreateAnArrayList { 
  public static void main(String[] args) { 
    ArrayList numbers = new ArrayList(); // create an ArrayList
    numbers.add(102);
    numbers.add(151);
    System.out.println(numbers);
  } 
}

Output:

[102, 151]

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments