Results 1 to 8 of 8
  1. #1
    codyjava is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default 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 04:32 AM.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,999
    Blog Entries
    7
    Rep Power
    19

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,924
    Rep Power
    4

    Default Re: Store 2D array in 1D array

    Quote Originally Posted by codyjava View Post
    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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    codyjava is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: Store 2D array in 1D array

    Thanks

  5. #5
    ArnavKumar is offline Member
    Join Date
    Oct 2013
    Posts
    11
    Rep Power
    0

    Default 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 01:17 PM. Reason: Removed URL

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,082
    Rep Power
    4

    Default Re: Store 2D array in 1D array

    Quote Originally Posted by ArnavKumar View Post
    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 12:42 PM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,564
    Rep Power
    23

    Default Re: Store 2D array in 1D array

    See related thread: Suggestions on homework
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default 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

Similar Threads

  1. How to Store an Array in an Array
    By Jtrtoday in forum New To Java
    Replies: 4
    Last Post: 09-14-2012, 05:05 AM
  2. Store image in an 2D array
    By zenitis in forum New To Java
    Replies: 5
    Last Post: 12-23-2011, 03:53 PM
  3. how to store data in array
    By aconti in forum New To Java
    Replies: 7
    Last Post: 06-18-2011, 06:24 PM
  4. HOw to Store Data in An Array
    By deshmukh.niraj04 in forum New To Java
    Replies: 4
    Last Post: 04-06-2011, 04:00 AM
  5. Replies: 1
    Last Post: 03-31-2009, 06:40 AM

Tags for this Thread

Posting Permissions

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