• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Array

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Help please
How do I get 2 arrays
String[][] stringArray1
String[][] stringArray2
into one array?
 
Manuel Paco
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And How do convert them to Vector/
 
Ranch Hand
Posts: 1376
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1376
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much.
 
reply
    Bookmark Topic Watch Topic
  • New Topic