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,870 questions

51,793 answers

573 users

How to concatenate two string arrays in Java

3 Answers

0 votes
public class Main {
    public static void main(String[] args) {
        String[] array1 = {"aaa", "bbb"};
        String[] array2 = {"ccc", "ddd", "eee"};
        
        String[] result = new String[array1.length + array2.length];
        
        System.arraycopy(array1, 0, result, 0, array1.length);
        System.arraycopy(array2, 0, result, array1.length, array2.length);
        
        for (String str : result) {
            System.out.print(str + " ");
        }
    }
}

   
   
/*
run:
   
aaa bbb ccc ddd eee 
  
*/

 



answered Feb 17, 2025 by avibootz
0 votes
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        String[] array1 = {"aaa", "bbb"};
        String[] array2 = {"ccc", "ddd", "eee"};
        
        String[] result = Arrays.copyOf(array1, array1.length + array2.length);
        System.arraycopy(array2, 0, result, array1.length, array2.length);
        
        for (String str : result) {
            System.out.print(str + " ");
        }
    }
}

   
   
/*
run:
   
aaa bbb ccc ddd eee 
  
*/

 



answered Feb 17, 2025 by avibootz
0 votes
import java.util.Arrays;
import java.util.stream.Stream;

public class Main {
    public static void main(String[] args) {
        String[] array1 = {"aaa", "bbb"};
        String[] array2 = {"ccc", "ddd", "eee"};
        
        String[] result = Stream.concat(Arrays.stream(array1), Arrays.stream(array2))
                                .toArray(String[]::new);
        
        for (String str : result) {
            System.out.print(str + " ");
        }
    }
}

   
   
/*
run:
   
aaa bbb ccc ddd eee 
  
*/


 



answered Feb 17, 2025 by avibootz

Related questions

2 answers 196 views
1 answer 181 views
1 answer 115 views
115 views asked Aug 11, 2021 by avibootz
1 answer 184 views
1 answer 149 views
3 answers 216 views
1 answer 152 views
...