aspose file tools*
The moose likes Java in General and the fly likes Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Array" Watch "Array" New topic
Author

Array

Manuel Paco
Ranch Hand

Joined: Sep 23, 2001
Posts: 58
Help please
How do I get 2 arrays
String[][] stringArray1
String[][] stringArray2
into one array?
Manuel Paco
Ranch Hand

Joined: Sep 23, 2001
Posts: 58
And How do convert them to Vector/
Joe Pluta
Ranch Hand

Joined: Jun 23, 2003
Posts: 1376
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
Joe Pluta
Ranch Hand

Joined: Jun 23, 2003
Posts: 1376
Originally posted by Manuel Paco:And How do convert them to Vector

Ah, now this is a little trickier. Again, because the original array is two-dimensional, you have a choice to make. Do you want a Vector of arrays, an array of Vectors, or a Vector of Vectors?
Assuming you want a Vector of Vectors, the idea is to create an empty vector that will hold other vectors. Next run through the array and convert each row to a new vector, and add it to the original vector. The code is actually quite simple.

The first line creates a new Vector. The second line is a standard for loop used to process the elements in an array. Learn this syntax, know it, love it. You will use it over and over again. The third and fifth lines simply bracket the fourth line, which processes each row in the array.
The Arrays.asList() method converts the row into a List object, which can then in turn be used in the constructor of the Vector method. This creates a Vector with all the elements of the row. This new Vector is added to the original Vector. Once this loop is finished, you will end up with a Vector of Vectors.
Joe
Manuel Paco
Ranch Hand

Joined: Sep 23, 2001
Posts: 58
Thank you very much.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Array