How to add items in list at a time?

0 votes
Hey, I was trying to initialize list at one line.

previously my code was

ArrayList<String> abc = new ArrayList<String>();
abc.add("Buenos Aires");
abc.add("Córdoba");
abc.add("La Plata");

Is there any option to add them in one line?

Thanks
Aug 9, 2019 in Java by Priyanka
1,379 views

1 answer to this question.

0 votes

Hi Priyanka ,

I think this code snippet will help you out,

please check

import java. util.*;

public class Main

{

public static void main(String[] args) {

System.out.println("Hello World");

List<String> abc = Arrays.asList("Buenos Aires", "Córdoba", "La Plata");

System.out.println(abc);


}

}

Thanks

answered Aug 9, 2019 by sampriti
• 1,100 points

Related Questions In Java

+1 vote
3 answers

How to convert a List to an Array in Java?

Either: Foo[] array = list.toArray(new Foo[list.size()]); or: Foo[] array = ...READ MORE

answered Aug 7, 2018 in Java by Akrati
• 3,150 points
102 views
0 votes
1 answer

How to count the number of occurrences of an element in a List?

We can use the static frequency() method. int ...READ MORE

answered Aug 21, 2018 in Java by sharth
• 3,370 points
122 views
+1 vote
1 answer

How to extend a class at runtime in Java?

Yes: you can do it with Burningwave ...READ MORE

answered Mar 16 in Java by Roberto
• 370 points
167 views
+5 votes
3 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,400 points

edited Nov 6, 2018 by Omkar 17,434 views
+9 votes
12 answers

Default parameters of XMS and XMX in JVM

You use these Java command-line parameters to ...READ MORE

answered Nov 13, 2018 in Java by Murali
189,227 views
0 votes
1 answer

What is $stateProvider in AngularJs?

Routing is just another way of fixing some content ...READ MORE

answered Feb 11 in Angular by Niroj
• 9,980 points
361 views
0 votes
3 answers

Does Java support Default Parameters?

You can try this with method overloading. void ...READ MORE

answered Sep 21, 2018 in Java by Sushmita
• 6,890 points
2,689 views
0 votes
1 answer

Is it possible to create a memory leak with Java

Here's a good way to create a memory ...READ MORE

answered May 30, 2018 in Java by Parth
• 4,640 points
1,103 views
0 votes
1 answer

how to compare two words in java?

hey,  I think in your code the function ...READ MORE

answered Aug 7, 2019 in Java by sampriti
• 1,100 points

edited Aug 8, 2019 by Kalgi 1,231 views
0 votes
2 answers

How do I get distinct element from list in java?

You should use a Set. it doesn't contain duplicates. If ...READ MORE

answered Aug 23, 2019 in Java by Sirajul
• 50,550 points
295 views