Output 2 arrays in 1 new array | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Output 2 arrays in 1 new array

public class Program { static int[] h1 = {2,3}; static int[] h2 = {4,5}; public static void main(String[] args) { int[][] h3 = {h1, h2}; System.out.println(h3); } } This is the only code that makes sense for me and doesn’t end up with errors when i’m trying to output the two strings h1 and h2 together as h3 = {2,3,4,5} But it somehow doesn’t work either and outputs nothing, can someone help me make this work?

19th Nov 2022, 9:03 PM
Maximilian Biber
Maximilian Biber - avatar
2 Answers
0
You are making 2D array and holding array reference inside. To get combined array values in 1D : Create array of size h1.length + h2.length then copy values into h3 array.. int[] h3 = new int [h1.length+ h2.length]; First copy h1 values next h2.
19th Nov 2022, 9:44 PM
Jayakrishna 🇮🇳
19th Nov 2022, 10:05 PM
SoloProg
SoloProg - avatar