Thread: Store 2D array in 1D array

1. Member
Join Date
Dec 2012
Posts
42
Rep Power
0

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:
Java 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:
Java Code:
`Employee76359279nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull`

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

I had to do this:
Java 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?
Last edited by codyjava; 11-03-2013 at 05:32 AM.

2. 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

3. Senior Member
Join Date
Jan 2013
Location
United States
Posts
2,684
Rep Power
4

Re: Store 2D array in 1D array

Originally Posted by codyjava
Does anyone have any other ideas of how I can dynamically set the array length though?
Java Code:
```int n = matrix.length;
int m = matrix[0].length; // assumes a rectangular matrix.
String array[] = new String[n * m];```
Regards,
Jim

4. Member
Join Date
Dec 2012
Posts
42
Rep Power
0

Thanks

5. Member
Join Date
Oct 2013
Posts
11
Rep Power
0

Re: Store 2D array in 1D array

This is like 1D array of another array.
*** URL REMOVED <<<<
Last edited by Norm; 11-05-2013 at 02:17 PM. Reason: Removed URL

6. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
2,610
Rep Power
3

Re: Store 2D array in 1D array

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.
Last edited by gimbal2; 11-05-2013 at 01:42 PM.

7. Re: Store 2D array in 1D array

See related thread: Suggestions on homework

8. Member
Join Date
Nov 2012
Location
Johannesburg, South Africa
Posts
83
Rep Power
0

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

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•