Store 2D array in 1D array

• 11-03-2013, 04:18 AM
codyjava
Store 2D array in 1D array
Hey all I'm trying to design a method that will take a 2D input calculate the sum of the total hours for each employee and return those totals in an array.

I'm not suppose to use a 2D array as a return. So I thought it would be easiest to store the 1st value of every row and then the sum of the rest of the row. (might be an easier way while still not using 2D array I'm up for suggestions I guess I could do two separate arrays somehow I'm just not sure how I could make sure the sums always corresponded to the right workers)

Right now I'm just trying to store all the values before I try to sum them and it seems to be starting on the last row then storing nothing for the remainder of the array. Why is this? Also I have set a static value to the array (64) because I'm not sure how to dynamically make it as long as the matrix.

Thanks everyone.

Code:
Code:

```public class Hw11 {         public static String[] sumMatrix(String[][] matrix){                                 String array[] = new String[64];                                 for(int i=1;i<matrix.length;i++){                         for(int j=0;j<matrix[i].length;j++){                                 array[j] = matrix[i][j];                         }                 }                 return array;         }                         public static void main(String [] args){                                 String[][] matrix = {                                 {" ","SU","M","T","W","R","F","SA"},                                 {"Employee0","2","4","3","4","5","8","8"},                                 {"Employee1","7","3","4","3","3","4","4"},                                 {"Employee2","3","3","4","3","3","2","2"},                                 {"Employee3","9","3","4","7","3","4","1"},                                 {"Employee4","3","5","4","3","6","3","8"},                                 {"Employee5","3","4","4","6","3","4","4"},                                 {"Employee6","3","7","4","8","3","8","4"},                                 {"Employee7","6","3","5","9","2","7","9"}                 };                                                 String array[] = sumMatrix(matrix);                 for(int i=0;i<array.length;i++){                         System.out.print(array[i]);                 }                                         }         }```
Result:
Code:

`Employee76359279nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull`

Update: I figured out why it wasn't storing values until the last of the matrix.

Code:

```        public static String[] sumMatrix(String[][] matrix){                                 String array[] = new String[64];                 int index = 0;                                 for(int i=1;i<matrix.length;i++){                         for(int j=0;j<matrix[i].length;j++){                                 array[index++] = matrix[i][j];                         }                 }                 return array;         }```
Does anyone have any other ideas of how I can dynamically set the array length though?
• 11-03-2013, 08:27 AM
JosAH
Re: Store 2D array in 1D array
I'm sorry, from your description I don't understand at all what the program is supposed to be doing; maybe an example can clarify it a bit ...
w.r.t. those arrays, have a look at the Collections framwork instead.

kind regards,

Jos
• 11-03-2013, 07:50 PM
jim829
Re: Store 2D array in 1D array
Quote:

Originally Posted by codyjava
Does anyone have any other ideas of how I can dynamically set the array length though?

Code:

```int n = matrix.length; int m = matrix[0].length; // assumes a rectangular matrix. String array[] = new String[n * m];```
Regards,
Jim
• 11-03-2013, 08:12 PM
codyjava
Re: Store 2D array in 1D array
Thanks
• 11-05-2013, 12:12 PM
ArnavKumar
Re: Store 2D array in 1D array
This is like 1D array of another array.
*** URL REMOVED <<<<
• 11-05-2013, 12:33 PM
gimbal2
Re: Store 2D array in 1D array
Quote:

Originally Posted by ArnavKumar
This is like 1D array of another array.

Yes. And why did you post that? What point are you trying to make?

EDIT: ah, spam.
• 11-05-2013, 02:15 PM
Norm
Re: Store 2D array in 1D array
• 11-09-2013, 03:11 PM
Kagiso
Re: Store 2D array in 1D array
I'm afraid both the method implementation and the 2d array in your code don't make any sense
All I know is that ur method takes a String[][] and returns String[]

and one more thing to note is that if you say "1" + "2" it wont give you a 3// strings are concatenated
so adding those elements of a 2d array is a no-no