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

Thread: Need help to solve this problem

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Location
    Dhaka,bangladesh
    Posts
    7
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Need help to solve this problem

    Write a Java Code of a method that takes an array of integers (of positive and negative numbers) and the length or size of the array as parameters, then modifies the array by getting rid of the negative elements (the numbers after the removed one will have to shift forward to fill the gap of course). If there are multiple negative elements, remove all of those. The function returns the new size of the array.
    Thanx in advance....


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Need help to solve this problem

    Suppose the things passed to the method were:

    array: [1,0,42,-1,2,666,-99,*,*,*] length: 7

    (I've written * because it might be the case that you want to pass longer arrays and simply ignore the elements that come after the length). What you want to end up with is:

    array: [1,0,42,2,666,*,*,*,*,*] length (returned): 5

    Can you see that this would meet the requirements of the method? And now the tricky question: how did you see that the array and its returned length are correct?

    -----

    Eliminating the negative values and determining the new length is child's play. What is difficult is formulating a recipe with steps that are that comprehensive (nothing left out) and unambiguously simple - formulating and expressing the recipe (algorithm) in words. Only once you have such a "plan of attack" can you begin to write code. And until you have expressed it all we have is a homework dump.

    One starting point might be to maintain a "pointer" or index which you increment as you move down the array. In fact you might need two such pointers: one describing the position you are looking at and one describing the position in the array that things should be shifted to.

    Take some numbers written on small pieces of paper and a line of empty squares representing array positions and play around with it. (Ie try the discard and shift operations you described) Aim to be conscious of the algorithm that you instinctively follow.

    [Edit] Rereading my reply it occurs to me that a whiteboard might be a better place to experiment than numbers written on pieces of paper. The problem is that there are never any empty array slots - they always have something in them.

    Given that one hand will hold the eraser and the other the whiteboard marker you soon find you have to enlist the help of an associate whose hands are free to point at where you got to with the eraser and where you are to write with the marker. In fact for really long arrays two friends will be needed: precisely the two pointers I suggested.
    Last edited by pbrockway2; June 3rd, 2012 at 05:14 AM.

  3. The Following User Says Thank You to pbrockway2 For This Useful Post:

    ahanf (June 3rd, 2012)

Similar Threads

  1. need help to solve the problem
    By priyadaradi in forum Member Introductions
    Replies: 1
    Last Post: May 24th, 2012, 10:44 AM
  2. help me to solve my problem
    By miszIna in forum Object Oriented Programming
    Replies: 3
    Last Post: February 14th, 2011, 09:40 AM
  3. Plz solve the problem
    By rasheedmgs in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: October 14th, 2010, 11:59 AM
  4. Questions about a Problem I'm trying to solve
    By DarkEssence in forum Java Theory & Questions
    Replies: 4
    Last Post: March 17th, 2010, 06:29 AM
  5. Replies: 3
    Last Post: June 14th, 2009, 09:31 PM