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

Thread: copying 2dim array into 1 dim array

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default copying 2dim array into 1 dim array

    Hello

    I would like to write a intance method called holdingColumn() which takes an int as an argument and returns a 1 dim int array called holdingArray of length 4.

    This method will copy all the elements of a single column (column index indicated by the integar argument) of a 2 dim array called poolArray into holdingArray.

    The method should then return holdingArray.


    public void holdingColumn(int anInt)
    int [][] poolArray = new int[4][3];
    poolArray = new int [][] {{1,3,5},{7,9,11},{13,15,19},{21,23,25}};

    int [][] holdingArray = new int[4][1];

    for (int i = 0; i < 4;i++)

    holdingArray[i][anInt] = poolArray [i][anInt];
    I get this error when I run the program with any int other than 0.
    Exception: line 3. java.lang.ArrayIndexOutOfBoundsException: 1

    If I use public void holdingColumn() i.e without an argument and choose a specific column to copy as part of the method as follows:

    public void holdingColumn()
    int [][] poolArray = new int[4][3];
    poolArray = new int [][] {{1,3,5},{7,9,11},{13,15,19},{21,23,25}};

    int [][] holdingArray = new int[4][1];

    for (int i = 0; i < 4;i++)

    holdingArray[i][1] = poolArray [i][1];

    then no problem the second column of poolArray gets copied into holdingArray.

    I think it is because I have created a 2 dim array called holdingArray(albeit it is 1 dim), I do not another way I can copy elements of a 2dim array into 1 dim array.


    Can you please advise on my method,

    rgds av8.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 168 Times in 159 Posts

    Default Re: copying 2dim array into 1 dim array

    Quote Originally Posted by av8 View Post
    a 1 dim int array called holdingArray
    So why have you declared holdingArray as a 2D array?

  3. #3
    Member
    Join Date
    Jun 2011
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: copying 2dim array into 1 dim array

    I dont know the syntax to copy from a 2dim array into a 1dim array proper, so I created a 2 dim array which has only 1 dim if that makes sense.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 168 Times in 159 Posts

    Default Re: copying 2dim array into 1 dim array

    The syntax is basically the same: copy the value at position x,y into position x. I assume you know how to access elements in an array.

  5. #5
    Member
    Join Date
    Jun 2011
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: copying 2dim array into 1 dim array

    Yes it works with and also with an int as argument aswell ! Thanks and what line of code would I add to the end of my method to return holdingArray?

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 168 Times in 159 Posts

    Default Re: copying 2dim array into 1 dim array

    A return statement.

  7. #7
    Member
    Join Date
    Jun 2011
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: copying 2dim array into 1 dim array

    Yes it works with and also with an int as argument aswell ! Thanks and what line of code would I add to the end of my method to return holdingArray?

  8. #8
    Member
    Join Date
    Jun 2011
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: copying 2dim array into 1 dim array

    OK thanks for your help.

  9. #9
    Junior Member
    Join Date
    May 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: copying 2dim array into 1 dim array

    junky is right, change your holding array to int [] holdingArray = new int[4];
    then your transfer line should look like holdingArray[i]=poolArray[i][1];
    that way your taking a single int from the position you want to, tell me if this helps

Similar Threads

  1. Doubling The Array Size And Randomizing Array Return
    By Pingu00 in forum What's Wrong With My Code?
    Replies: 18
    Last Post: June 27th, 2011, 10:50 AM
  2. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM
  3. Replies: 2
    Last Post: May 6th, 2011, 05:19 PM
  4. Copying Array Problems.. Not what you think
    By xXRedneckXx in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 5th, 2011, 11:01 AM
  5. 2d (4x4) array insdie a 1d array. (Block cipher)
    By fortune2k in forum Collections and Generics
    Replies: 13
    Last Post: November 23rd, 2010, 04:29 PM