Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Again i need some advice please

  1. #1
    Member
    Join Date
    Nov 2013
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Again i need some advice please

    Sorry to bother you that much.
    I have a project that contain an array of people(each index contain name,salary and id number).
    i need to make a matrix that each name will be in a new row.
    so i did count how many different names i have(this is what will be my matrix size).
    but i am not sure how to continue ,any ideas?

    example:"console" -this is my array:{
    1 james bond 1000
    2 tami 400
    3 james bond 500
    4 tami 1
    5 james bond 1} first-id,second-name,third-salary.{5 indexes}

    i need to make matrix (for this example will be): { james bond james bond james bond
    tami tami}
    hope that you understood if not nvm but thanks for any help


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,343
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Again i need some advice please

    -this is my array
    Is that a 2 dim String array? Can you post code that shows how it is defined? It needs "s and , to show what the elements in the array are.

    i need to make matrix
    That looks like a one dim array? Is that correct?

    Is the content of the one dim array copied from the first column of the 2 dim array?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Nov 2013
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Again i need some advice please

    Quote Originally Posted by Norm View Post
    Is that a 2 dim String array? Can you post code that shows how it is defined? It needs "s and , to show what the elements in the array are.


    That looks like a one dim array? Is that correct?

    Is the content of the one dim array copied from the first column of the 2 dim array?

    You know what lets say i have an array of strings{"james","john","james","john","james"}

    I need to initialize a matrix that each row contain a different name.

    --- Update ---

    The output will be a matric look like that: john john john
    james james

    --- Update ---

    3 times james***
    2 times john.

    Each in sep row .

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,343
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Again i need some advice please

    What have you tried?
    The code would have some loops and do compares on what it sees in the one dim array.
    When the contents of the one dim array changes, the code would go to the next row in the 2 dim array that is being filled.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Nov 2013
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Again i need some advice please

    	int arr[]=new int[]{1,1,1,2,2};
     
     
        int row=2;  //two different number=two rows
    	int[][]matrix=new int[row][];
        for (int i=0;i<matrix.length;i++){
        	for(int j=0;j<matrix[i].length;j++){
     
     
        	}
        }
    I stuck on how do i apply this via matrix to get in one row (3 times '1'): 1 1 1
    and second row(different number '2') : 2 2

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,343
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Again i need some advice please

    Is the contents of the one dim array sorted? That will make it much easier.
    Stop writing code until you can explain what the the code must do.
    Can you explain in English the steps the code must do to scan a one dim array and find all the elements in it that should go on a row of the two dim array?
    Also it needs to determine how many rows are needed.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Nov 2013
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Again i need some advice please

    yes it is sorted.
    i need to make a matrix from this array -each row different number and the times the number occuered
    1.i need to check how many numbers are different to determine how many rows i need(i check and i need 2 rows)
    2.and here is where i am stuck , i need to make a matrix from that(i hope you understand).
    here is what i'v tried so far but the program fail:
    	int arr[]=new int[]{1,1,1,2,2};
     
        int row=2;
    	int[][]matrix=new int[row][];
    	for (int i=0;i<row;i++){
    		for(int j=0;j<arr.length;j++){
    			if(arr[i]!=arr[i+1]){
    				matrix[i][j]=arr[i];
    			}
    		}
     
     
     
    	}

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,343
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Again i need some advice please

    i need to make a matrix from that
    What does "make" mean?
    At least two parts to that:
    define the size of each row
    copy the values from the one dim array to a row in the two dim array

    What does the code need to do to determine the size of a row in the two dim array?
    When it gets the size, then it can define the size of the row.

    The posted code has the number of rows hard coded, vs doing a scan to compute the number of rows needed. That's ok for testing, but will need to be changed later to do a scan to count the number of rows needed.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Nov 2013
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Again i need some advice please

    yes for array of example like i show{1 1 1 2 2}

    to matrix like that: 1 1 1
    2 2


    i count the number of different number=2.that's the row number.
    but how can i continue from their?can you show me the right way?or direct me.
    i am completely lost here

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,343
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Again i need some advice please

    Now you need to scan the one dim array and find the number of like numbers that will go on each row so the code can define the array with the correct size.
    With {1 1 1 2 2} the first row would have 3 and the second row 2. Use a loop to scan the array to count the like numbers.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Need some serious advice
    By Rituparna in forum The Cafe
    Replies: 4
    Last Post: September 25th, 2013, 09:26 AM
  2. [SOLVED] i need an advice!
    By waseem1345 in forum Java Theory & Questions
    Replies: 2
    Last Post: November 14th, 2012, 12:15 PM
  3. Hello, need some advice.
    By mango7777 in forum Member Introductions
    Replies: 1
    Last Post: June 15th, 2012, 09:10 AM
  4. need for some advice
    By kasiopi in forum AWT / Java Swing
    Replies: 6
    Last Post: January 26th, 2011, 10:34 AM
  5. i need some advice ....
    By mdstrauss in forum Java Theory & Questions
    Replies: 8
    Last Post: July 24th, 2009, 03:29 PM