How to declare an ArrayList with values? [duplicate]

ArrayList or List declaration in Java has questioned and answered how to declare an empty ArrayList but how do I declare an ArrayList with values?

I’ve tried the following but it returns a syntax error:

import java.io.IOException;
import java.util.ArrayList;

public class test {
    public static void main(String[] args) throws IOException {
        ArrayList<String> x = new ArrayList<String>();
        x = ['xyz', 'abc'];
    }
}

6 Answers
6

Leave a Comment