Configuring specified index for insertAt method


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Configuring specified index for insertAt method

Threaded View

  1. #1
    Join Date
    Jul 2005
    Posts
    3

    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;
    else
    {
    //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;

    }
    count++;
    }

    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

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

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
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

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