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

Thread: Need an Idea: storing elements of two different arrays?

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Need an Idea: storing elements of two different arrays?

    Heres what I got so far.

    1. Size of both array.
    2. An object array that has the size of both combined

    How would I pass all the elements of both array into that new object?.

    ex:
    Array 1 would have : 1,2,3,4,5,6;
    Array 2 would have : 7,8,9;

    How would I get
    Array 3 to have: 1,2,3,4,5,6,7,8,9;
    Last edited by red7; May 12th, 2012 at 03:19 PM.


  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: Need an Idea: storing elements of two different arrays?

    If the data types are compatible, copy the two arrays into the third array.
    You could use one of the Arrays class's copy methods for one and a loop for the other.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need an Idea: storing elements of two different arrays?

    How would I use a for loop to accomplish this?
    temp = Array 1 length + Array 2 length
    I would have:
    for(int i = 0; i<=temp;i++)
    Stuck there

  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: Need an Idea: storing elements of two different arrays?

    To get an idea of how the indexes would go, take a piece of paper and draw the slots for the third array to receive the contents of the other two. Then beneath it, draw the slots for the two shorter arrays that are to be copied. Write down the source indexes and the target indexes for each array as you would copy the contents from one to the other.
    You should see that there are more than one index value being used. For the first array, the source and target indexes will be the same. for the second array, they will be different.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    red7 (May 12th, 2012)

  6. #5
    Junior Member
    Join Date
    Apr 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need an Idea: storing elements of two different arrays?

    Wow, couldn't believe how clear it was when writing it down, feel kinda stupid now haha. Finally got it to work, much much thanks, problem solved.

  7. #6
    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: Need an Idea: storing elements of two different arrays?

    I used paper and pencil a lot for arrays and linked lists. It's good to see all the details visually before trying to write any code.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] ArrayList object's elements confusing??? doesnt replace the elements? set() method?
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 21st, 2011, 01:20 PM
  2. How to divide elements in two different arrays
    By BuhRock in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 11th, 2011, 06:38 PM
  3. help w/ storing/scanning numbers in two dimensional arrays
    By robertsbd in forum What's Wrong With My Code?
    Replies: 10
    Last Post: December 1st, 2010, 11:56 PM
  4. help w/ storing/scanning numbers in arrays
    By robertsbd in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 17th, 2010, 10:55 PM
  5. Help with Arrays - Counting elements
    By ShakeyJakey in forum Collections and Generics
    Replies: 7
    Last Post: August 8th, 2010, 04:09 PM