0 votes
1 view
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 (46.1k 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
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...