合并三个数组成为一个多维数组。

huangapple go评论126阅读模式
英文:

Merge 3 arrays into a multidimensional array

问题

我有三个单独的数组,我想将它们合并成一个多维数组。

  1. 数组3 [1, 1, 2, 4, 1, 2, 1, 1]
  2. 数组2 [1, 1, 2, 2, 1, 2, 6, 1]
  3. 数组1 [1, 3, 1, 1, 1, 1, 2, 1]

我想要的输出是,所有数组都位于一个多维数组中,它们都叠放在一起。

最终数组 =

  1. 数组3
  2. 数组2
  3. 数组1

我该如何实现这个?

英文:

I have three separate arrays that I would like to merge into a multidimensional array

  1. Array 3 [1, 1, 2, 4, 1, 2, 1, 1]
  2. Array 2 [1, 1, 2, 2, 1, 2, 6, 1]
  3. Array 1 [1, 3, 1, 1, 1, 1, 2, 1]

The output that I would like to have is where all the arrays are in one multidimensional array where they are all stacked on top of each other.

Finial Array =

  1. Array 3
  2. Array 2
  3. Array 1

How would I go about doing this?

答案1

得分: 1

  1. import java.util.stream.IntStream;
  2. public class MultiDimensionalArrayDemo {
  3. public static void main(String[] args) {
  4. final int ROWS = 3;
  5. final int COLUMNS = 10;
  6. int array1 [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  7. int array2 [] = { 10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
  8. int array3 [] = { 20, 21, 22, 23, 24, 25, 26, 27, 28, 29};
  9. int finalArray[][] = new int[ROWS][];
  10. finalArray[0] = array1;
  11. finalArray[1] = array2;
  12. finalArray[2] = array3;
  13. System.out.println("Initial bi-dimensional array:");
  14. for(int i=0; i<ROWS; i++) {
  15. for(int j=0; j<COLUMNS; j++) {
  16. System.out.printf("%2d ",finalArray[i][j]);
  17. }
  18. System.out.println();
  19. }
  20. for(int i=0; i<ROWS-1; i++) {
  21. for(int j=0; j<COLUMNS; j++) {
  22. if(finalArray[i][j]==0) {
  23. moveColumnUp(IntStream.range(i, ROWS).mapToObj(obj -> finalArray[obj]).toArray(int[][]::new),ROWS-i,j);
  24. }
  25. }
  26. }
  27. System.out.println("After transformation:");
  28. for(int i=0; i<ROWS; i++) {
  29. for(int j=0; j<COLUMNS; j++) {
  30. System.out.printf("%2d ",finalArray[i][j]);
  31. }
  32. System.out.println();
  33. }
  34. }
  35. private static void moveColumnUp(int matrix[][], int numberOfRows, int columnIndex) {
  36. int firstRowElement = matrix[0][columnIndex]; // temporarily store first row so it can be placed last
  37. for(int i=0; i<numberOfRows-1; i++) {
  38. matrix[i][columnIndex] = matrix[i+1][columnIndex];
  39. }
  40. matrix[numberOfRows-1][columnIndex] = firstRowElement;
  41. }
  42. }
英文:

If I understood your question correctly, you're looking for something like this:

  1. import java.util.stream.IntStream;
  2. public class MultiDimensionalArrayDemo {
  3. public static void main(String[] args) {
  4. final int ROWS = 3;
  5. final int COLUMNS = 10;
  6. int array1 [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  7. int array2 [] = { 10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
  8. int array3 [] = { 20, 21, 22, 23, 24, 25, 26, 27, 28, 29};
  9. int finalArray[][] = new int[ROWS][];
  10. finalArray[0] = array1;
  11. finalArray[1] = array2;
  12. finalArray[2] = array3;
  13. System.out.println(&quot;Initial bi-dimensional array:&quot;);
  14. for(int i=0; i&lt;ROWS; i++) {
  15. for(int j=0; j&lt;COLUMNS; j++) {
  16. System.out.printf(&quot;%2d &quot;,finalArray[i][j]);
  17. }
  18. System.out.println();
  19. }
  20. for(int i=0; i&lt;ROWS-1; i++) {
  21. for(int j=0; j&lt;COLUMNS; j++) {
  22. if(finalArray[i][j]==0) {
  23. moveColumnUp(IntStream.range(i, ROWS).mapToObj(obj -&gt; finalArray[obj]).toArray(int[][]::new),ROWS-i,j);
  24. }
  25. }
  26. }
  27. System.out.println(&quot;After transformation:&quot;);
  28. for(int i=0; i&lt;ROWS; i++) {
  29. for(int j=0; j&lt;COLUMNS; j++) {
  30. System.out.printf(&quot;%2d &quot;,finalArray[i][j]);
  31. }
  32. System.out.println();
  33. }
  34. }
  35. private static void moveColumnUp(int matrix [][], int numberOfRows, int columnIndex) {
  36. int firstRowElement = matrix[0][columnIndex]; // temporarily store first row so it can be placed last
  37. for(int i=0; i&lt;numberOfRows-1; i++) {
  38. matrix[i][columnIndex] = matrix[i+1][columnIndex];
  39. }
  40. matrix[numberOfRows-1][columnIndex] = firstRowElement;
  41. }
  42. }

huangapple
  • 本文由 发表于 2020年9月10日 17:56:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63827183.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定