9 Replies Latest reply on Apr 9, 2008 1:57 AM by 807591

# Combining multiple arrays

Hi everybody,
I've been trying to combine any number of arrays in a specific way, but I think if anyone could give me an answer to this specific example, I should be fine.

Anyway, here's my problem. I have a 2D array of strings, like this:
``````String[][] test = {
{ "test1", "\n", "\n", "test2", "\n", "test3" },
{ "test1", "\n", "test2", "\n", "\n", "\n", "test3" }
};``````
What I'd like to do is combine the arrays together into one big array, so there are a maximum number of "newline" strings between each string I care about. So, the output for this example would be:
``{ "test1", "\n", "\n", "test2", "\n", "\n", "\n", "test3" }``
(2 newlines from the first array between test1 and test2, and 3 newlines from the second array between test2 and test3)

If anyone could give me ideas as to how to do this, I'd really appreciate it.
Thanks,
Jezzica85

Edited by: jezzica85 on Apr 8, 2008 12:34 PM
• ###### 1. Re: Combining multiple arrays
there is no clever trick to do this. you'll just have to use good old logic and iteration.
• ###### 2. Re: Combining multiple arrays
Add the strings which you care into one dimensional array
String s[] = {.........};
• ###### 3. Re: Combining multiple arrays
Thank you both for your help so far; I was afraid that logic and iteration were the only way to go. I've been fighting with it for about a day now and I can't seem to get it. Could either of you possibly suggest a general strategy for the logic, please?
Jezzica85
• ###### 4. Re: Combining multiple arrays
I could see this happening with one loop, two indices and two counters that get reset for each newline round. Can you visualize how that might work? Hint: one index stops progressing once it hits the next word...
• ###### 5. Re: Combining multiple arrays
Hmm...one loop, 2 indices, 2 counters. Sounds complicated, but I'll give it a shot.
Thanks,
Jezzica85
• ###### 6. Re: Combining multiple arrays
Before starting, you could write a little pseudocode of how you think it should work and perhaps post here. That exercise most often helps clarify algorithms that seem difficult at first. Sometimes the problems practically disappear before your eyes that way :)
• ###### 7. Re: Combining multiple arrays
I have a feeling you're going to think I have the IQ of a cauliflower, but I'm completely lost here.

Here's what I have for pseudocode so far:
``````int[] indexes = new int[numArrays];
while continue
for i = 0, i < numArrays
if array[i][indexes] == array[i-1][indexes[i]]
array[i].concat("\n")I have a feeling it was an exercise in futility (but that's my fault, not yours). I stink at writing pseudocode; that's the problem. Honestly, I have no clue how to do this.
Jezzica85

Edited by: jezzica85 on Apr 8, 2008 2:36 PM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ``````
• ###### 8. Re: Combining multiple arrays
Ok, here's what I was thinking:
``````//you have array1 and array2 and will output it to the list combined
idx1 = 0
idx2 = 0
counter1 = 0
counter2 = 0
while idx1<array1.length and idx2<array2.length
grabbingNewlines = false
if array1[idx1]=='\n'
grabbingNewlines = true
counter1++
idx1++
if array2[idx2]=='\n'
grabbingNewlines = true
counter2++
idx2++

if not grabbingNewlines
if counter1>0 || counter2>0