1

i want to transfer an n elements array to a two-d array in java.

For example:

[1,2,3,4]

this array should be transferred to an two-d array

[[1, 2], [1, 3], [1, 4], [2, 3], [2, 4] [3,4]]

  • the first line of the two-d array is 1, 2.
  • the second line of the two- array is 1, 3.
  • the third line of the two-array is 1, 4.
  • the forth line of the two-array is 2, 3.
  • the fifth line of the two-array is 2, 4.
  • the last line is 3, 4
7
  • 4
    What have your tried?? And should the number of columns always be 2?? Commented Oct 11, 2012 at 4:35
  • 1
    @Justin.. Then how would you divide it when there are 10 elements in your array?? Please be clear with your specs.. Commented Oct 11, 2012 at 4:37
  • 1
    So, you are trying to create a two-dimensional array of all combinations of the elements in the first array? Specific details are necessary. Commented Oct 11, 2012 at 4:40
  • 1
    if there is a 6 element array. [1,2,3,4,5,6] the first line of the two-d array should be 1, 2 the second..................................1, 3 the third ..................................1, 4 the fifth...................................1, 5 the sixth...................................1, 6 the seventh.................................2, 3 the eight ..................................2, 4 so on so far Commented Oct 11, 2012 at 4:41
  • 1
    @Justin.. Well now that it is clear what you want.. It doesn't seem to be that tough. You should give it a try.. Write some Algorithm on Paper. and then see how it works.. Commented Oct 11, 2012 at 4:44

2 Answers 2

8

Without writing any code for you...

Think about how big your 2d array needs to be. Recognize that you'll need to loop over the contents of your source array to get each value into your destination array.

So it will look something like...

Create a 2d array of appropriate size. Use a for loop to loop over your 1d array. Inside that for loop, you'll need to figure out where each value in the 1d array should go in the 2d array. Try using the mod function against your counter variable to "wrap around" the indices of the 2d array. I'm being intentionally vague, seeing as this is homework. Try posting some code so we can see where you get stuck.

Or you can use that code:-

int array2d[][] = new int[10][3];


for(int i=0; i<10;i++)
   for(int j=0;j<3;j++)
       array2d[i][j] = array1d[(j*10) + i]; 
Sign up to request clarification or add additional context in comments.

1 Comment

array2d[i][j] = array1d[(i*3) + j];
2

From Permutation and combination you can get the row length of the 2D array (ie. n!/r!*(n-r)!).

  • getLengthOfArray method does this.
  • calculatePermutation is a helper method to calculate permutaion.
  • transferToTwoDArray will give you the right answer.

public int[][] transferToTwoDArray(final int[] oneDArray)
{
    assert null != oneDArray;
    final int length = oneDArray.length;
    assert length > 2;

    final int newArrayLength = getLengthOfArray(length, 2);
    final int[][] newArray = new int[newArrayLength][2];

    int nextRowIndex = 0;
    for (int i = 0; i < length; i++)
    {
        int nextValue = i + 1;

        while (nextValue < length)
        {
            newArray[nextRowIndex][0] = oneDArray[i];
            newArray[nextRowIndex][1] = oneDArray[nextValue++];
            nextRowIndex++;
        }
    }
    return newArray;
}

/**
 * Calculate permutation.
 * 
 * @param length
 *            the length
 * @return the int
 */
private int calculatePermutation(final int length)
{
    if (length == 0)
    {
        return 1;
    }
    return length * calculatePermutation(length - 1);
}

/**
 * Gets the length of array.
 * 
 * @param length
 *            the length
 * @param arrayDimention
 *            the array dimention
 * @return the length of array
 */
private int getLengthOfArray(final int length, final int arrayDimention)
{
    return calculatePermutation(length)
            / ((calculatePermutation(arrayDimention)) * (calculatePermutation(length - arrayDimention)));
}

Cheers, Chand.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.