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: Processing Arrays

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

    Default Processing Arrays

    I have an int array called oddNumbers: int [] oddNumbers = {1,3,5,7,9}

    I want to process oddNumbers in the following way;

    For each element of oddNumbers (except the last element at index 4 i.e 9 ) to copy into that element
    the value from the element to its immediate right.

    This means that the value in index 1 (i.e 3) is copied into index 0, the value in index 2 is
    copied into index 1 and so on, until the element at index 4 is copied into index 3.
    The contents of the rightmost index 4 (i.e 9) will remain unaffected.

    After processing the array oddNumbers, it should have this appearance: int [] oddNumbers = {3,5,7,9,9}

    I would be grateful for some help in coding to achieve this.

    Best wishes

    av8.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Processing Arrays

    Please do not post the same thing twice to the forums - your other post has been removed.

    If you are looking for precise help, I recommend you give this a try (hint, a for loop might get you started), and when your stuck post the code you have with any and all errors messages and a precise description of the problem. Hoping someone will answer this without showing you've made an attempt is typically quite unproductive.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Processing Arrays

    For this kind of problem, take a piece of paper and a pencil. Draw the array with the numbers.
    Put a pointer under the source element, note the position/index.
    Put a pointer under the target element, note its position.
    Move the element at the source to the target by erasing the target value and writing in a copy of the source element.
    Change the values of the indexes to do the next step.

    Now look at how you had to change the index values to get to the next step.
    That will tell you how to write a program to do this.

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

    Default Re: Processing Arrays

    I appologise copeg. Having posted my problem on what's wrong with my code I thought the arrays/collections might be a more appropriate forum.

    Looking at the paper/pencil sketch Norm the index/position decrements by -1 ?

    Best regards


    av8
    Last edited by av8; June 27th, 2011 at 03:26 AM.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Processing Arrays

    After you get the idea of how to move the index, have you tried writing the code?

Similar Threads

  1. Image processing tutorial
    By ChristopherLowe in forum File Input/Output Tutorials
    Replies: 8
    Last Post: December 19th, 2013, 11:19 PM
  2. Image Processing
    By subash in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: June 8th, 2011, 08:40 AM
  3. Signal processing API.
    By ivanloes in forum Java Theory & Questions
    Replies: 4
    Last Post: December 18th, 2010, 05:56 PM
  4. token-based processing
    By amandat in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 21st, 2010, 12:05 AM
  5. ERROR WHILE PROCESSING JSF FILE
    By Pankaj in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: July 10th, 2010, 12:42 AM