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 5 of 5

Thread: Filling a 2D array with other arrays.

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    46
    My Mood
    Asleep
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Filling a 2D array with other arrays.

    I am completely novice to the 2D arrays but so far I have been grasping the concept. Except one issue. I have an assignment that in one of the methods, a multi-dimensional array with columns and rows with lengths of other arrays, must be built. That part is done, and I figured out by now that I need a for loop within another for loop to fill it with the content of 2 other separate arrays. So I have:

    int [] sot = {20, 25, 30, 35, 40, 45, 50}; 
    int [] fet = {25, 30, 35, 40, 45, 50}; 
    double[][] myHUT = new double [sot.length][fet.length];
     
    public void trackerUlt(){
            for (int row = 0; row < myHUT.length; row++)
                for (int col = 0; col < myHUT[0].length; col++) 
                    myHUT[row][col] += //???needs sot and fet array data in there.
    }

    I just don't know how to make it so that I have all the numbers from sot and all the numbers from the fet array filled into the myHUT multi-dimensional array. Help me out.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Filling a 2D array with other arrays.

    You should think about the sizes of the arrays you are working with and where the element in the two single dim arrays are to go. The two dim arrays have 7 and 6 elements for a total of 13 elements. The 2 dim array has 7x6 slots for a total of 42 elements. There are a few extra slots in the 2 dim array. What do you want to put in them?

    You can treat the 2 dim array as an array of arrays:
    if twoDim has two dim and oneDim has one dim then:
    twoDim[i] = oneDim; // assign the one dim array to one of the rows of the 2 dim array

  3. #3
    Member
    Join Date
    Oct 2011
    Posts
    46
    My Mood
    Asleep
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Filling a 2D array with other arrays.

    What is i in your example? So in comparison to my example, you mean set it up like:
    myHUT[0][] = sot[];
    myHUT[1][] = fet[];
    wouldn't that mean not using the for loops then? I sort of need to use those.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Filling a 2D array with other arrays.

    Leave off the '[]'s.

    As I said before the two 1 dim arrays have 13 elements and the 2 dim array has 42.
    What goes in those extra slots?

  5. #5
    Member
    Join Date
    Oct 2011
    Posts
    46
    My Mood
    Asleep
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Filling a 2D array with other arrays.

    Hmmm, well in the output there is supposed to be 42 numbers of data, 7 per column. So I don't really know....

    Ahh stupid me. After the second for loop, a formula must be added to take the calculations of both arrays, and adds that to the 2-dim array. Not just the numbers themselves.

Similar Threads

  1. Arrays and array list
    By rob17 in forum Collections and Generics
    Replies: 2
    Last Post: February 19th, 2012, 06:10 PM
  2. sum of arrays and printing even and odd numbers in the array
    By senecawolf in forum Collections and Generics
    Replies: 3
    Last Post: November 8th, 2011, 03:07 PM
  3. Filling Arrays
    By av8 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 25th, 2011, 11:41 PM
  4. filling an array with strings
    By exose in forum Collections and Generics
    Replies: 3
    Last Post: February 18th, 2011, 09:44 AM
  5. Filling an Array?
    By Bascotie in forum Collections and Generics
    Replies: 5
    Last Post: October 14th, 2009, 06:27 PM