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

Thread: Question - implement insertAtPosition with the signature

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

    Default Question - implement insertAtPosition with the signature

    Good afternoon all.

    I have just joined these forums, and I thank you for your help in advance.. I was browsing through this forum and it seems to have quite the community for java users, which will be very helpful for me as I have just begun a java orientated class at University.. I am working away through my 4th lab , and have hit a road block, and unfortunately haven't been able to have any classmates help. I was hoping you could point me in the right direction how how I could solve this problem :

    Add code to implement insertAtPosition with the signature

    public void insertAtPosition(Object what, int insertPosition);

    This method inserts the 'what' item at the indicated position. Elements following that position are moved one position in the +1 direction to make room for the new element. Items should only be inserted at already occupied positions, except that it is legal to insert an element at the position just past the filled positions (or at the zero position if there are no items). Note that for a linked list, the elements logically 'move' positions but don't physically move.



    Any help would be appreciated. Thank yoU!


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

    Default Re: Question - implement insertAtPosition with the signature

    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Question - implement insertAtPosition with the signature

    I am just trying to figure out exactly what it is I am trying to solve with the method.. I am currently drawing out a diagram so I can see what it is I am doing a little better.. So far I know I want to find the element in the linked list before this element,

    Set the 'next' pointer of the object being inserted to the object being pointed to by the preceding object, and

    Set the 'next' pointer of the preceding object to the object being inserted.

    I am going to keep plugging away, if you have any suggestions fell free! Cheers[COLOR="Silver"]

    --- Update --
    	public void insertAll(int position, ObjectList otherList) {
    		for (int i = 0; i < otherList.nextEmptySlot; i++) {
    			iLikeToMoveIt(position);
    			Object other = otherList.allItems[i];
    			allItems[position] = other;
    			position++;
     
    		}
     
    	}[COLOR="Silver"]
     
    --- Update ---
     
    [/COLOR]So... I have put together what I think is pretty good.. but I don't know if its really efficient and if I could change something.. It is passing all my tests, and is working .. what do you guys think
     
    public void insertAtPosition(Object theObject, int atPosition){
     
     	if(itemCount < atPosition){
     	 return;
     	}
     
     	if(atPosition == 0){
     	 LinkBox oneAfterInsert = headOfList;
     	 headOfList = new LinkBox(theObject, oneAfterInsert);
     	 itemCount++;
     	 return;
     
     	}
     	LinkBox end = headOfList;
     
     	for (int position = 0; position < itemCount; position++) {
     	 if (position == atPosition-1){
     	 LinkBox oneAfterInsert = end.getNext();
     	 LinkBox insertThis = new LinkBox(theObject, oneAfterInsert);
     	 end.setNext(insertThis);
     	 itemCount++;
     	 }
     	 end = end.getNext();
     	}
     }

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

    Default Re: Question - implement insertAtPosition with the signature

    is working .
    Without code to execute, your tests will have to do.

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Need help to build an app! signature analysis
    By braceforce in forum Java Theory & Questions
    Replies: 0
    Last Post: January 9th, 2013, 10:57 AM
  2. invalid encoding for signature
    By Silvery in forum JDBC & Databases
    Replies: 2
    Last Post: July 2nd, 2012, 03:20 PM
  3. Why does not forum allow signature?
    By hns1984 in forum The Cafe
    Replies: 0
    Last Post: October 20th, 2011, 11:04 AM
  4. Call with a different signature
    By brajesh in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 15th, 2010, 02:05 PM
  5. El Gamal signature -Handle big integer
    By miotvsingtel in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 9th, 2010, 12:05 PM