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,728 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,120 points

Related Questions In Java

0 votes
0 answers

How to manage two JRadioButtons in java so that only one of them can be selected at a time?

How to manage two JRadioButtons in java ...READ MORE

Apr 21, 2020 in Java by kartik
• 37,530 points
227 views
+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,170 points
279 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,350 points
4,223 views
0 votes
2 answers

How to make a new List in Java?

Initializing a List in Java The Java.util.List is a child ...READ MORE

answered Dec 28, 2020 in Java by Thomas Walenta
84 views
+11 votes
13 answers

Default parameters of XMS and XMX in JVM

Run this code to see all the ...READ MORE

answered Nov 13, 2018 in Java by anonymous
299,326 views
0 votes
1 answer

What is $stateProvider in AngularJs?

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

answered Feb 11, 2020 in Angular by Niroj
• 82,820 points
870 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,900 points
5,378 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,630 points
1,558 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,120 points

edited Aug 8, 2019 by Kalgi 1,598 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
• 59,070 points
975 views