d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Two Dimensional Array Problem
Add Reply New Topic New Poll
Member
Posts: 24,101
Joined: Nov 8 2007
Gold: 5,561.70
Jun 1 2012 11:07am
Having trouble appending two 2-dimensional arrays together.

http://pastebin.com/4SM8QJRD - pastebin link to my code

The file outputs the info in a weird way. For example when I try to append the two arrays

double[][] first = {{10, 4}, {6, 14}};
double[][] second = {{20}, {34, 100, 200}};

It prints out in this exact order:

10.0
4.0
0.0
6.0
14.0
0.0
0.0
0.0
20.0
0.0
0.0
100.0

When it should print out

10.0
4.0
20.0
6.0
14.0
34.0
100.0
200.0

I know it has something to do with my loops but I don't really understand why.
Member
Posts: 24,101
Joined: Nov 8 2007
Gold: 5,561.70
Jun 1 2012 11:59am
I simplified the code with just seperate for loops, output is still incorrect.

http://pastebin.com/C2znjaFa - edited code

Output (with same numbers):


10.0,4.0
6.0,14.0
20.0
34.0,100.0,200.0

Should be coming out as:

10.0,4.0,20.0
6.0,14.0,34.0,100.0,200.0

The output is printed out using this code:

Code
for (int row = 0; row<results.length; row++) {
  for (int col=0; col<results[row].length-1; col++)
   output+=results[row][col] + ",";
  output+=results[row][results[row].length-1];
  output+="\n";
 }
 System.out.println(output);


This post was edited by lopelurag on Jun 1 2012 12:03pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jun 1 2012 05:36pm
personally, i dislike reading your code so i'm not going to debug it. i like to create extra functions to keep logic clear and to make testing a lot easier. since you're not really using a matrix concept, i like to think of 2D arrays simply as a single array of data (and that data just happens to be an array).

to merge two single arrays:
Code

double appendArrays(double[] array1, double[] array2){
 double[] array = new double[array1.length + array2.length];
 // copy array1 into our new array
 System.arrayCopy(array1, 0, array, 0, array1.length);
 // copy array2 after the first array
 System.arrayCopy(array2, 0, array, array1.length, array2.length);
 return array;
}


Now for each 2D array, i want to think of them as single arrays that have data in them. that data just happens to be another array. I'm assuming array1 and array2 have the same number of rows and nothing is null. you can compensate by yourself.
Code

void append2dArrays(double[][] array1, double[][] array2){
  double[][] array = new double[array1.length][];
  for (int i = 0; i < array.length; i++){
     array[i] = appendArrays(array1[i], array2[i]);
  }
}


then to print, do something similar. create a method that will print a single array, then another method loops through the 2D array and prints each row.

that's just how i'd do it. i dont know where your mistake is since i dont want to read through it :(
Member
Posts: 24,101
Joined: Nov 8 2007
Gold: 5,561.70
Jun 1 2012 10:26pm
Yeah I fixed it hours ago, sorry forgot to update. I was thinking entirely wrong about the whole process.

It was extremely simple
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll