Intellipaat Back

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

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

    }

}

1 Answer

0 votes
by (46k points)

You can do like this :

List<String> temp = new ArrayList<String>(Arrays.asList("1", "12"));

Related questions

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

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...