DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2005

    Unhappy Configuring specified index for insertAt method

    I have an IntList that I am adding methods to and an IntListTest driver that allows me to add those options to test. I am having difficulty adding the two methods:

    public void removeAt() - removes the value at a specified location at given index with consideration of the size of the list when user enters a valid position

    public void insertAt(int index, int newValue) - inserts integer and puts it in the specified index of list however...i also have to take into consideration the size of the list as the user enters a valid position

    I started on the insertAt(int index, int newValue) method:

    // **Inserts an element to the list at a specified position

    public void insertAt(int index, int newValue)
    IntNode newnode = new IntNode(newValue, null);

    for (int index =0; index < count; index++)
    if(list[index] == newValue) //error occurs, brackets pertain to arrays, correct?
    //if list is empty, this will be the only node in it
    if (list == null)
    list = newnode;
    //temp point directed to the last thing on list, but want to be
    //able to insert any where, specified location

    IntNode temp = list;
    while (temp.next != null)
    temp = temp.next;

    //link new node into list and insert
    temp.next = newnode;


    First off, how do I write the method so that the user specifies a value at a given index and is located...From what I understand, this portion of the insertAt method is stating that the temp point will go directly to the last index:

    //temp point directed to the last thing on list, but want to be
    //able to insert any where, specified location

    IntNode temp = list;
    while (temp.next != null)
    temp = temp.next;

    Here's where I am having complications, because I don't know where to go from here. I want the method to be able to insert at any specified index rather than just the last...I will use the insertAt method as an example for my removeAt method, so hopefully I can get input from anyone willing to help...Thanks - using textpad editor with java 1.5 compiler

    Last edited by onip28; 07-18-2005 at 06:13 AM.

  2. #2
    Join Date
    Aug 2003
    Consider the following:
    [5] -> [3] -> [2] -> [0] -> []
    where [5] represents the data in an IntNode and -> represents the reference to the next. If we want to insert something at index 0 then we just say:
    new list = [added node] -> (old list)
    If we want to add something in the middle of the list then we find the element before it, set teh next of the new node to the next of that node, and then set the next of that node to be the new node. For example:
    [5] -> [3] -> [2] -> [0] -> []
    would go as follows if we were adding [4] at position 1 (after the [5]):
    [5] would be the element before it [5].next is [3]
    so we make a new node with [4] and set its next to [3] ([4].next = [3])
    then we set the next of [5] to the new node [4] ([5].next = [4])

    To find the element before where we want to insert we might try something like this:
    int index = ...; // index to insert at
    IntNode prior = first;
    while( prior != null && index > 1 ) {
      // set prior to the node immediately after prior
      // decrement index by 1
    if( prior == null ) // error index was too large
    else // insert after prior
    Hope this helps.

    (Oops, forgot to decrement index )

  3. #3
    Join Date
    Jul 2005
    Thanks for you help...this should get me back on my feet! Much appreciated

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.