Results 1 to 2 of 2
  1. #1
    VettesRus is offline Member
    Join Date
    Jun 2012
    Posts
    35
    Rep Power
    0

    Default Convert Array class to Linked List?

    I have been trying to convert my array class into a Node class to implement a linked list in my program instead of using an array. I am not having much luck with my add method. The program takes ten items and paints them into a bar graph, then I can select a min or max and remove it. Right now my program does not paint anything with my Node implementation.

    my array class
    Java Code:
    import java.awt.*;		
    
    
    public class ArrayDataCollection implements DataCollection
    {
    
    
    	//
    	//	Sets initial capacity of array to 15, none are in collection, none are selected.
    	//
    	public ArrayDataCollection()
    	{											//	Instantiate an array of a
    		theItems = new Item[15];			//	beginning default size.
    		size = 0;							//	There are no items yet
    		selected = -1;						//		and no selected item either
    
    	}
    	
    	public ArrayDataCollection(int someX, int someY){
    		this();
    		x = someX;
    		y = someY;
    	}
    
    
    	public void add(Item someItem)
    	{
    		if(selected==theItems.length){
    			Item[] temp = new Item[theItems.length * 2];
    			for(int i = 0; i< theItems.length; i++)
    				temp[i] = theItems[i];
    			theItems = temp;
    		}
    		//set the location of the item to follow the previous
    		someItem.setLocation(x + size * Item.OVERALL_WIDTH, y);
    		theItems[size] = someItem;		//		Set the new element as		
    		changeSelected(size++);				//	selected
    	}
    
    	//
    	//	The only "graphical" method of the class is the paint method.
    	//
    	public void paint(Graphics pane)
    	{
    		if(theItems != null){
    			for(int i = 0; i<size; i++)
    				theItems[i].paint(pane);
    		}
    	}
    
    
    	public boolean hasNext()
    	{
    		return (size > 0) && (selected >= 0) && (selected < size);
    	}
    
    
    	public Object next()
    	{
    		Item result = null;
    
    		if ((size > 0) && (selected >= 0) && (selected < size)){
    			result = theItems[selected];
    			changeSelected(selected + 1);
    		}
    
    		return result;
    	}
    
    
    	private Item [] theItems;				//	Holds the items
    	private int size;						//	Counts the number of items
    	private int x, y;						//	holds the location of the collection
    	private int selected;					//	Keeps track of the "selected" item
    
    	
    
    }

    What I have so far for my add method. It seems to print 10 items in this format Node@7e7b28dc
    Java Code:
    	public void add(Item someItem)
    	{
    		Node temp= last;
    		last= new Node();
    		last.setItem(someItem);
    		last.setNextNode(null);
    		
    		System.out.println(last);
    		
    	}
    
    public void paint(Graphics pane)
    	{
    		if(temp != null){
    			for(int i = 0; i<size; i++)
    				temp.paint(pane);
    		}
    	}
    Any help is appreciated.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,609
    Rep Power
    5

    Default Re: Convert Array class to Linked List?

    The problem is probably within you Item class. You need to provide a short example of how you are painting. Do not include a large amount of code. Probably less than 100 lines should be sufficient to demonstrate the problem.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 0
    Last Post: 09-25-2012, 04:07 AM
  2. Replies: 6
    Last Post: 03-28-2012, 10:09 PM
  3. Linked List, Array List time complexity
    By Rick99771977 in forum New To Java
    Replies: 4
    Last Post: 08-18-2011, 05:37 AM
  4. linked list or array?
    By sick_peng in forum New To Java
    Replies: 6
    Last Post: 04-15-2009, 07:33 PM
  5. Convert Linked List Object element to String
    By CirKuT in forum New To Java
    Replies: 2
    Last Post: 12-13-2008, 05:22 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
  •