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

Thread: insertItem method

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default insertItem method

    I am having issues with the following code:
        @Override
        public void insertItem(Comparable item, int position) {
            //throw new UnsupportedOperationException("Not supported yet.");
            if (!(this.full())){
                throw new SimpleListException("Full list");
            }
            else{
                    for(int i = 0; i < freeListArray.length; i++){ //find first free location
                        if(freeListArray[i] != NULL){
    			break;
                        }
                    }
    		linksArray[i] = freeListArray[i]; //put location in links
    		dataArray[linksArray[i]] = item;  //put item into data
    		temp = linksArray[i];
    		linksArray[dataArray[temp]] = -1; //show where end of array is        
                }
        }
     
    Constructor is as follows:
     
        public SimpleComparableList(){
            this.capacity = DEFAULT_CAPACITY;
            this.currentPosition = 1;
            this.dataArray = new String[capacity];
            this.linksArray = new int[capacity];
            this.freeListArray = new int[capacity];
        }
     
        public SimpleComparableList(int capacity){       
        // Constructs and initializes a list that is empty. Current list size is 
        // zero and current position is set to one.        
            this.capacity = capacity;                 
            this.currentPosition = 1; 
            this.dataArray = new String[capacity];
            this.linksArray = new int[capacity];
            this.freeListArray = new int[capacity];        
        }

    Can someone please fix my code and explain what was wrong with it?

    Thank you!


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: insertItem method

    You need to explain a bit more about what is wrong...since there is no SSCCE we can't run the code and test how it misbehaves. Do you get exceptions? Does it compile? Does it misbehave? You need to be as precise as possible

  3. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: insertItem method

    It compiles with errors. It does not misbehave. It runs a Test GUI consisting of:

    Default Constructor OK
    Constructor with given max size OK
    iterator()
    insertItem(Comparable item)
    insertItem(Comparable item, int position)
    removeItem()
    removeItem(int position)

  4. #4
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: insertItem method

    Please post the full text of the error messages. Java error messages usually tell you what's wrong and where.

    I suspect that when you do finally get it to compile, throwing a SimpleListException if the list is not full will make it hard to insert anything:
    if (!(this.full())){
       throw new SimpleListException("Full list");
    }
    WTF?
    Last edited by dlorde; June 13th, 2011 at 08:04 PM.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: insertItem method

    @dlorde
    I think this OP submits his sub program to an online black box that compiles it and executes it and returns some report.
    But he refuses to tell us about it.

  6. #6
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: insertItem method

    Yeah, looks like it. There does seem to be a rash of posts at present from people who won't listen or answer or follow links, or who keep changing their minds about what their problem is...

    This one made me laugh!

Similar Threads

  1. Can you pass parameters from one method into another method?
    By u-will-neva-no in forum Java Theory & Questions
    Replies: 2
    Last Post: April 14th, 2011, 07:46 AM
  2. Help with toString method and an addObject method?
    By Camisado in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 12th, 2011, 07:00 AM
  3. Can i call init() method in destroy method.?
    By muralidhar in forum Java Servlet
    Replies: 1
    Last Post: October 22nd, 2010, 11:18 AM