Originally posted by Manuel Paco:
Help please
How do I get 2 arrays
String[][] stringArray1
String[][] stringArray2
into one array?
Okay, this is a little tricky because you have two-dimensional arrays. And in
Java, two dimensional arrays are not necessarily square. Each "row" in the array can have a different number of elements:
For example, stringArray1 could contain the following:
"Dog"
"Apple", "Pear", "Peach"
"Ice Cream", "Cookie"
While stringArray2 could contain:
"Tire Iron", "Jack", "Hubcap", "Jumper Cables"
"Sand", "Gravel", "Dirt"
"Lassie"
"Sophia", "Charlene", "Joanne", "Meredith"
So what do you want? Probably a single array like this:
"Dog"
"Apple", "Pear", "Peach"
"Ice Cream", "Cookie"
"Tire Iron", "Jack", "Hubcap", "Jumper Cables"
"Sand", "Gravel", "Dirt"
"Lassie"
"Sophia", "Charlene", "Joanne", "Meredith"
If so, it's relatively easy to do. First, declare an array as large as the two arrays combined. Then copy the data from each initial array into the new array.
The first lines get the lengths of the two arrays for later use. The next line creates an array as big as the two initial arrays combined. Then the System.arraycopy method is used to copy multiple elements from one array to another. There are two calls to arraycopy; review the last two arguments in particular very carefully. They indicate where in the target array to start, and how many entries to copy.
Hope this helps!
Joe