-8

concatenate two arrays into single dimensional array?

For example:-

Take two arrays

Array1[10] = {3,3,3}

Array2[10] ={3,2,1}

output

Array3[] = {33,32,31}

How will I get this output ?

  • What have you tried already? Seriously, someone will probably answer anyway for the rep points but you should have a go and then ask if your attempt isn't working. – Ben Thurley Feb 26 '14 at 12:10
  • 1
    what's the data type of element of the array?? – TheLostMind Feb 26 '14 at 12:10
  • Have you heard about this Java thing called loops and String concatenation? – Smutje Feb 26 '14 at 12:10
  • try arrayutils http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/ArrayUtils.html – arajashe Feb 26 '14 at 12:10
  • That's not concatenating arrays. That's creating an array concatenating (or something) the entries from two source arrays. – T.J. Crowder Feb 26 '14 at 12:11
  • I have a question to this, is stack overflow become a free debugging tool online. Share what have you done, rather than putting questions online like this. – D3X Feb 26 '14 at 12:12

4 Answers4

1
  1. Make a for-loop an iterate over Array1 and Array2
  2. Do Integer.parseInt(String.valueOf(Array1[i])+String.valueOf(Array2[i]));
  3. Fill the Array3 in the for-loop with the calculated valued.

That's it.

    int[] Array1 = new int[] {3,3,3};
    int[] Array2 = new int[] {3,2,1};
    int[] Array3 = new int[Array2.length];

    for(int i = 0; i<Array1.length; i++) {
        Array3[i] = Integer.parseInt(String.valueOf(Array1[i])+String.valueOf(Array2[i]));
    }

Output:

Array3[] = {33,32,31}
kai
  • 6,702
  • 22
  • 38
1

This is not concatenating arrays, because that would look more like:

Array3[] = {3,3,3,3,2,1}

What you want to do is to concatenate each element inside the array, which is a different process entirely, and one you could accomplish with a simple for loop.

if(array1 is the same size as array2)
{
    define array3 = new array[array1.length];

    for(int x = 0 to array1.length)
    {
        array3[x] = array1[x] + array2[x]. 
    }
}

This is pseudocode, which I provide when the question doesn't contain any code. It's up to you to figure out how to turn this into Java code, although that isn't too difficult. You'll also notice I'm using the + operator for concatenation. I'm assuming your values are String type. If they're not, then get creative, using the Integer.parseInt method.

christopher
  • 26,815
  • 5
  • 55
  • 89
  • 1
    it will be a sum of the integers, not a concatenation – XpressOneUp Feb 26 '14 at 12:12
  • It will be a concatenation depending on the array type as noted. That is pseudocode. – Piovezan Feb 26 '14 at 12:13
  • 1
    I anticipated this, which is why I wrote *"You'll also notice I'm using the + operator for concatenation. I'm assuming your values are String type. If they're not, then get creative, using the Integer.parseInt method."*. All this code does is explain the core principles. Implementation details are up to OP. – christopher Feb 26 '14 at 12:14
1

Quick hint: to concatenate two integer values simply use:

array3[i] = array1[i] + "" + array2[i]; 

The result will be transformed to Srting because the second operand "" is a String.

XpressOneUp
  • 195
  • 10
0

try this:

for(int i = 0; i< Array1.length; i++)
        {
            Array3[i] = Integer.parseInt(String.format("{0}{1}", Array1[i],Array2[i])); 
        }
user2875912
  • 109
  • 2
  • 10