How to convert an array to arraylist?

0 votes
Apr 16, 2018 in Java by sophia
• 1,400 points
47 views

2 answers to this question.

0 votes
import java.util.*;

public class ArrToArrList {
     public static void main(String[] args) {

	  /* Array Declaration and initialization*/
	  String names[]={"George", "Sam", "Mac", "Alia"};

	  /*Array to ArrayList conversion*/
	  ArrList<String> names= new ArrList<String>(Arrays.asList(citynames));

	  /*Adding new elements to the converted List*/
	  names.add("Smit");
	  names.add("Peter");

	  /*Final ArrayList content display using for*/
	  for (String str: names)
	  {
		System.out.println(str);
       	  }
      }
}

answered Apr 16, 2018 by Akrati
• 3,150 points
0 votes

In Java 9 you can use:

List<String> list= List.of("Hello", "World", "from", "Java");
List<Integer> list = List.of(1, 2, 3, 4, 5);
answered Aug 1, 2018 by samarth295
• 2,190 points

Related Questions In Java

0 votes
2 answers
0 votes
2 answers

How to test that an array contains a certain value?

public static final String[] VALUES = new ...READ MORE

answered Jul 17, 2018 in Java by Sushmita
• 6,880 points
60 views
0 votes
2 answers

How do I read and convert an InputStream object to string type?

You can also use Java Standard Library ...READ MORE

answered Jul 17, 2018 in Java by Sushmita
• 6,880 points
3,362 views
0 votes
3 answers

How can I add new elements to an Array in Java

String[] source = new String[] { "a", ...READ MORE

answered Sep 19, 2018 in Java by Sushmita
• 6,880 points
114 views
0 votes
2 answers

One line initialization of an ArrayList object in Java

In Java 8 or earlier: List<String> string = ...READ MORE

answered Jul 26, 2018 in Java by samarth295
• 2,190 points
143 views
+1 vote
3 answers

What is the syntax to declare and initialize an array in java?

You can use this method: String[] strs = ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,190 points
323 views
0 votes
3 answers

How to sort an array in java?

import java.util.Arrays; public class Sort { ...READ MORE

answered Aug 24, 2018 in Java by parth295
• 4,620 points
62 views
0 votes
1 answer

How can I Sort an ArrayList in Java

You can sort the ArrayList in 2 ...READ MORE

answered Apr 23, 2018 in Java by sharth
• 3,320 points
59 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,150 points
55 views
0 votes
2 answers

How to convert an int array to string using tostring method in java?

Use java.util.Arrays: String res = Arrays.toString(array); System.o ...READ MORE

answered Aug 16 in Java by Sirajul
• 25,420 points
208 views