Lal
Lal

Reputation: 14810

combine array elements with elements of same array

What i'm trying to achieve is that, I have an array,

for eg: a={1,2,3}

Now, I need an array which combines all the elements in the array.

ie, the output that is needed should be like.

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

I know that, in StackOverflow asking a question without showing what I have done is not at all appreciated, but still, I'm totally blank and i dont have any ideas to code this. Any help is very much appreciated.

Upvotes: 0

Views: 128

Answers (2)

Francisco Romero
Francisco Romero

Reputation: 13199

What I think it's to make a double loop. Like this:

for(int i = 0; i < array.length; i++)
{
   for (int j = 0; j < array.length; j++)
   {
      System.out.println("[" + array[i] + "," + array[j] + "]");
   }
}

Upvotes: 1

T.J. Crowder
T.J. Crowder

Reputation: 1074555

To generate the resulting array, use two loops, one nested inside the other, both using indexes (say, i and j) going from 0 through < length on the array. The two values for the resulting element in the new array come from a[i] and a[j].

Upvotes: 2

Related Questions