2

I have three arrays of equal lengths that I combine (as I will sort them on column c later):

double abc[][] = {
    Arrays.copyOf(a, a.length),
    Arrays.copyOf(b, a.length),
    Arrays.copyOf(c, a.length)
};

When I call

System.out.println(Arrays.deepToString(abc));

I receieve:

[[4.0, 2.0, 1.3333333333333333, 5.0, 2.5, 1.6666666666666667 ....

However, I would prefer something like:

[[1.0, 1.0, 4.0], [2.0, 2.0, 5.0], [3.0, 3.0, 7.0]]

This is possible using double singlets

double test[][] = {{1,1,4},{2,2,5},{3,3,7}};

How can I populate/initialize three columns using three double[ ]?


EDIT:

Solution based on vojta's answer:

double abcT[][] = new double[abc[0].length][abc.length];
    for (int i = 0; i < abc.length; i++) {
         for (int j = 0; j < abc[0].length; j++) {
            abcT[j][i] = abc[i][j];
         }
      }
System.out.println(Arrays.deepToString(abcT));
4
  • I really do not understand your question... Please, try to explain in once again in different words. What is ratio? What is your goal? Do you really want to use a.length in all your Arrays.copyOf?
    – vojta
    Commented Mar 22, 2015 at 18:15
  • Sorry, typo. Ratio should be "abc". My goal is to sort a, b, and c as a row on column c. To do this I must first create a row by row structure: [a, b, c].
    – noumenal
    Commented Mar 22, 2015 at 18:18
  • 1
    Oh, I get it, maybe... You would like to populate your abc array with existing arrays, but use these arays as columns, not as rows, right? So basicaly you need to transpose your resulting matrix, right?
    – vojta
    Commented Mar 22, 2015 at 18:18
  • Yes, transposing would be one solution.
    – noumenal
    Commented Mar 22, 2015 at 18:19

2 Answers 2

1

I think there is no one-line solution of your problem, unfortunately. You will have to use some homemade code:

static <T> T[][] createMatrix(T[]... columns) {
  if (columns== null || columns.length == 0)
    return new T[0][0];

  int wid = columns.length;
  int ht = columns[0].length;

  T[][] result = new T[ht][wid];

  for (int x = 0; x < wid; x++) {
    for (int y = 0; y < ht; y++) {
      result[y][x] = columns[x][y];
    }
  }
  return result;
}

I hope it was useful.

1
  • JAMA uses this solution for transposing, except for the generics. I guess I will start building my own library. +1 for reuseability.
    – noumenal
    Commented Mar 24, 2015 at 19:57
0

I finally reached this more elegant solution:

    for (int i = 0; i < a.length; i++){
            abc[0][i] = a[i];
            abc[1][i] = b[i];
            abc[2][i] = c[i];
    }

Although it is not a general solution for n[], this avoids the need to make intermediate copies of the original arrays. And then I just swap the for-loops for row and column:

    for (int j = 0; j < abc[0].length; j++) {
        for (int i = 0; i < abc.length; i++) {
            System.out.print(abc[i][j] + " ");
        }
        System.out.print("\n");
    }

Note: This solution does not store in the intended R:C format, but retrieves in C:R.

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.