Back

Explore Courses Blog Tutorials Interview Questions
0 votes
5 views
in Java by (3.5k points)

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'];

    }

}

1 Answer

0 votes
by (46k points)

Use this one:

ArrayList<String> x = new ArrayList(Arrays.asList("abc", "mno"));

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...