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 in Java by Priyanka
582 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 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
74 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
60 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,380 points

edited Nov 6, 2018 by Omkar 11,445 views
0 votes
2 answers

How can I convert a String variable to a primitive int in Java

 Here are two ways illustrating this: Integer x ...READ MORE

answered Aug 20 in Java by Sirajul
• 41,000 points
219 views
+8 votes
11 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
135,926 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,880 points
1,197 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 parth295
• 4,640 points
655 views
0 votes
1 answer

java.lang.OutOfMemoryError: GC overhead limit exceeded Error

This simply means that for some reason ...READ MORE

answered May 31, 2018 in Java by misc.edu04
• 1,430 points
1,742 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 in Java by sampriti
• 1,100 points

edited Aug 8 by Kalgi 492 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 in Java by Sirajul
• 41,000 points
105 views