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: PLEASE HELP ME!!!

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Post PLEASE HELP ME!!!

    I am having trouble with this code.

    public E remove(int index){
         checkInvariants();
         if ((index < 0) || index >= size) {
              String warning = "The index has to be between 0 and " + (size - 1);
              throw new IndexOutOfBoundsException(warning);
         }
     
         verify (head != null);
         E val = head.item;
         if (size == 1) {
              head = null;
              tail = head;
         }
         else {
             LinkedNode<E> n = nodeAtPosition(index);
             val = n.item;
             if (index == 0) {
                  head = n.next;
     
             }
             n = n.next;
         }
         size--;
         checkInvariants();
         return val;
         // return null;
    }

    my code can compile, however, when I use the above method to remove anything other than the head, an exception is executed, saying that there is an assertion error. I realize that the tail might have something to do with it. If so, how can I correct this method?

    Also here is another remove method:

    public boolean remove(E element){
    LinkedNode<E> temp = head;
    if(head != null && element.equals(head.item)){
    if(head.next != null){
    head = head.next;
    size--;
    return true;
    }
    else{
    head = null;
    size--;
    return true;
    }
    }
    while(temp.next != null && element.equals(temp.next.item)){
    temp = temp.next;
    size--;
    return true;
    }
    return false;
    }

    This remove method above is supposed to be supporting the LinkedListIterator. However, it isn't. Is there any way to correct this method?
    Last edited by vk999; February 19th, 2011 at 03:13 AM.

  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: PLEASE HELP ME!!!

    an exception is executed, saying that there is an assertion error. I
    There is no where in your code that would cause an assertion error. Recommend you post the full error and use the line numbers it contains to backtrack through the code to find out where it is thrown. Further, we have no idea what many of the functions you call are actually doing (for example checkVariants) - and whether these could be the problem. This is where posting an SSCCE helps