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: List problems

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

    Default List problems

    I am right now reading a Java course. And I have totally get stucked with one of my task.
    My task is that I am trying to make a list that i can add and get object from.
    I have an API I build my list from, so the methods and the constructor in my classes have to be there. The problem come when I am trying to add some objects to the list. When I am trying to add an object, I only get back null object. And I have no ideé what the problem is. I would be really happy if someone please could told me what I am doing wrong.



    import java.lang.Object.*;
    public class Node {
        Object item;
        Node next;
        public Node (java.lang.Object newItem) 
        {
            setItem(newItem);
            setNext(null);
        }
     
        public Node(java.lang.Object newItem, Node nextNode)
        {
            setItem(newItem);
            setNext(nextNode);
        }
     
        public void setItem (java.lang.Object newItem)
        {
           item = newItem;
        }
     
        public java.lang.Object getItem()
        {
            return item;
        }
     
        public void setNext(Node nextNode)
        {
            next = nextNode;
        }
     
        public Node getNext()
        {
            return next;
        }
     
    }

    import java.lang.Object.*;
    public class ListReferencedBased implements ListInterface{
        int numItems;
        Node head;
     
        private Node find (int index)
        {
         Node current = head;
         if (index >= numItems)
         {
           return null;
         }
         else
         {
          for (int count=0; count<index; count++)
          {
          current = current.getNext();
          }
          return current;
         }
         }        
     
        public ListReferencedBased()
        {
            head = null;
            numItems = 0;
        }
     
        @Override
        public void add(int index, Object item) throws ListIndexOutOfBoundsException {
            Node pre, cur;
             if (index < 1 || index > numItems+1)
             {
                 numItems++;
             }    
                if (isEmpty()) {
     
                head = new Node(item);
                numItems++;
                }
                else{
                    head = new Node(find(index), head);
                    numItems++;
              }
     
      /*      pre = find(index-1);
            cur = pre.getNext();
            pre.setNext(new Node(item, cur));
        */    
        }
     
        @Override
        public Object get(int index) throws ListIndexOutOfBoundsException {
           head.setNext(find(index));
           if(find(index) != null)
           {    
           return head.getItem();
           }else{
           return null;
           } 
        }
     
        @Override
        public boolean isEmpty() {
            if(numItems == 0) 
            {
            return true;
            }
            else
            {    
            return false;    
            }
        }
     
        @Override
        public void remove(int index) throws ListIndexOutOfBoundsException {
            throw new UnsupportedOperationException("Not supported yet.");
        }
     
        @Override
        public void removeAll() {
          head.next = null;  
        }
     
         @Override
        public int size() {
      /*    numItems=0;
            while(head!=null)
            {
                numItems++;
                head.getNext();
            }*/
        return numItems;  
        }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: List problems

    You have marked this thread as solved. Does that mean you figured this out?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. list in JSP
    By smackdown90 in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: November 13th, 2011, 01:08 PM
  2. SpinnerListModel setList(List<?> list)
    By roy epperson in forum Collections and Generics
    Replies: 4
    Last Post: November 29th, 2010, 10:30 AM
  3. ADT list help
    By jkalm in forum Collections and Generics
    Replies: 15
    Last Post: October 18th, 2010, 03:13 PM
  4. Getting List of Files Within Jar
    By aussiemcgr in forum Java Theory & Questions
    Replies: 1
    Last Post: October 13th, 2010, 09:38 AM
  5. need help with a list class
    By araujo3rd in forum Object Oriented Programming
    Replies: 1
    Last Post: February 25th, 2010, 07:58 PM

Tags for this Thread