Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,911 questions

51,843 answers

573 users

How to convert an ArrayList of String to String array in Java

5 Answers

0 votes
import java.util.ArrayList; 
import java.util.Arrays; 
 
public class MyClass {
    public static void main(String args[]) {
        ArrayList<String> al = new ArrayList<String>(); 
   
        al.add("java"); 
        al.add("c++"); 
        al.add("nodejs"); 
        al.add("php"); 
 
        String[] array = new String[al.size()]; 
         
        for (int i = 0; i < al.size(); i++) { 
            array[i] = al.get(i); 
        } 

        System.out.println(Arrays.toString(array)); 
    }
}
 
 
 
/*
run:
 
[java, c++, nodejs, php]
 
*/

 



answered May 19, 2020 by avibootz
edited Nov 15, 2023 by avibootz
0 votes
import java.util.ArrayList; 
import java.util.Arrays; 
 
public class MyClass {
    public static void main(String args[]) {
        ArrayList<String> al = new ArrayList<String>(); 
   
        al.add("java"); 
        al.add("c++"); 
        al.add("nodejs"); 
        al.add("php"); 
 
        Object[] obj = al.toArray(); 
   
        String[] arr = Arrays.copyOf(obj, obj.length, String[].class); 
         
        System.out.print(Arrays.toString(arr)); 
    }
}
 
 
 
/*
run:
 
[java, c++, nodejs, php]
 
*/

 



answered May 19, 2020 by avibootz
edited Nov 15, 2023 by avibootz
0 votes
import java.util.ArrayList; 

public class MyClass {
    public static void main(String args[]) {
        ArrayList<String> al = new ArrayList<String>(); 
   
        al.add("java"); 
        al.add("c++"); 
        al.add("nodejs"); 
        al.add("php"); 
 
        String[] arr = new String[al.size()];
         
        arr = al.toArray(arr);
 
        for (String s : arr) {
            System.out.println(s);
        }
    }
}
 
 
 
/*
run:
 
java
c++
nodejs
php
 
*/

 



answered May 19, 2020 by avibootz
edited Nov 15, 2023 by avibootz
0 votes
import java.util.ArrayList; 

public class MyClass {
    public static void main(String args[]) {
        ArrayList<String> al = new ArrayList<String>(); 
   
        al.add("java"); 
        al.add("c++"); 
        al.add("nodejs"); 
        al.add("php"); 
 
        String[] arr = al.toArray(new String[al.size()]);
 
        for (String s : arr) {
            System.out.println(s);
        }
    }
}
 
 
 
/*
run:
 
java
c++
nodejs
php

*/

 



answered May 19, 2020 by avibootz
edited Nov 15, 2023 by avibootz
0 votes
import java.util.ArrayList; 
import java.util.Arrays; 

public class MyClass {
    public static void main(String args[]) {
        ArrayList<String> al = new ArrayList<String>(); 
   
        al.add("java"); 
        al.add("c++"); 
        al.add("nodejs"); 
        al.add("php"); 

        String[] arr = al.toArray(new String[0]);
         
        System.out.print(Arrays.toString(arr)); 
    }
}
 
 
 
/*
run:
 
[java, c++, nodejs, php]
 
*/

 



answered Jun 15, 2021 by avibootz

Related questions

1 answer 116 views
1 answer 144 views
2 answers 282 views
5 answers 272 views
1 answer 159 views
1 answer 120 views
1 answer 211 views
...