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
2,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,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,520 points
680 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,190 points
1,137 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
5,656 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
784 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
402,630 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,840 points
2,732 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,920 points
6,935 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 31, 2018 in Java by Parth
• 4,640 points
2,266 views
0 votes
1 answer

how to compare two words in java?

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

answered Aug 8, 2019 in Java by sampriti
• 1,120 points

edited Aug 8, 2019 by Kalgi 2,266 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,230 points
2,186 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP