Results 1 to 7 of 7
  1. #1
    Grimey is offline Member
    Join Date
    May 2012
    Posts
    30
    Rep Power
    0

    Default Outputting Elements of a Linked List

    Hey!
    I have looked around and seen a few things on this, but I'm not exactly sure how some of them work. I have created a linked list in my program of type class. The class holds a few pieces of data, mostly ints and strings. I have a for loop in another piece of code that I want to draw the list to the window using drawString. How would I access the string. I saw on one forum that you could uses list[i].string, but that seemed strange to me as it is the same syntax as an array. Sure enough, it didn't work too.

    Thanks a lot!

    Grimey

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Outputting Elements of a Linked List

    What methods do the nodes in the list have? If they have a getNext() method, you could get the first node's link from the linked list and then use the getNext() method to traverse the list.
    Or have a method in the linked list class that returns the node at index i
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Grimey is offline Member
    Join Date
    May 2012
    Posts
    30
    Rep Power
    0

    Default Re: Outputting Elements of a Linked List

    I am using the linkedList class from java.util. The class I made is called Items, so I declare an array of 20 items, just until I get more of the details fiured out. It creates the items and gives them a name of item + i, and assigns the value and weight of the item to a random integer. The linkedList is declared like this:
    LinkedList<Items> stock = new LinkedList<Items>();
    I then choose 10 random items from the array of 20 and use stock.Add(item(randInt)). Now I want to cycle through the list and output the name, the value, and the weight in three columns. I'm still pretty new to linked lists, so addmittedly, I'm only half sure of what I'm talking about, but I think I am doing everything the right way... Also, how would you just access a particular item. So, if the player chooses item n, how would you access the data? I'm assuming in a similar way to retrieving information for output.
    Thanks again!

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Outputting Elements of a Linked List

    access a particular item. So, if the player chooses item n, how would you access the data?
    Read the API doc for the class you are using and see if there are any methods that will do what you want.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Grimey is offline Member
    Join Date
    May 2012
    Posts
    30
    Rep Power
    0

    Default Re: Outputting Elements of a Linked List

    Yes, I was just looking at it. I could use the get(index) method, then to access a certain variable within the class, would I just use get(index).var? As for outputting the list, should I be using a list iterator? So using something like this:
    Java Code:
    itr = list.iterator();
    
    while(itr.hasNext()) {
         g.drawString(itr.next(), 50, yDraw);
         yDraw += 15;
    }
    Again, the only issue is how to get itr.next to output the name within the class that the list is made up of.
    Thanks!

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Outputting Elements of a Linked List

    The next() method returns the next element from the list. When you have that element you can use it as you want.
    What class are the elements in the list? Does it have methods you can use to get its contents?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Grimey is offline Member
    Join Date
    May 2012
    Posts
    30
    Rep Power
    0

    Default Re: Outputting Elements of a Linked List

    It's a class I made. Here it is:
    Java Code:
    public class Items {
    	String name;			//Name of the item
    	char type;				//Type of item
    								//w - weapon
    								//h - helmet
    								//c - cuirass
    								//b - boots
    								//g - gloves
    								//m - miscellaneous
    	int value;				//Value of the object
    	
    	int defence;			//Not used if the item type is a weapon
    	int attack;				//Not used if the item type is armour
    	
    	int weight;				//Used to determine how much the player can carry
    }
    And here's the code from the class where it is outputted:

    Java Code:
    public class Shop {
    	public SystemFunctions lib = new SystemFunctions();
    	
    	LinkedList<Items> stock = new LinkedList<Items>();
    	int numberItems = 10;
    	
    	Items things[] = new Items[20];
    	
    	public void initItems() {
    		//Initialises the items
    		for (int i = 0; i < 20; i++) {
    			things[i] = new Items();
    		}
    		
    		for (int i = 0; i < 20; i++) {		//Creates 10 random items... This will eventually be an actual list of items
    			things[i].name = "Item" + i;
    			things[i].weight = lib.randInt(10) + 1;		//Gives the object a weight from 1 to 10
    			things[i].value = lib.randInt(100) + 1;		//Gives the object a value from 1 to 100 
    		}
    	}
    	
    	public void initShop() {
    		int choose;                  //A random integer to decide which item to use
    		
    		for (int i = 0; i < 10; i++) {		//Populates the store with 10 items
    			choose = lib.randInt(20);		//Chooses a random item
    			
    			stock.add(things[choose]);		//Adds the item to the list
    		}
    	}
    	
    	public void renderShop(Graphics g) {		
    		//Draws the shop screen
    		g.drawImage(Main.invScreen, 0, 0, null);
    		
    		g.setColor(Color.BLACK);
    		g.drawString("Store", 45, 45);
    		
    		//Lists the items... Eventually...
    		int yDraw = 75;
    		for (int i = 0; i < numberItems; i++) {
    			yDraw += 15;		//Increments the y pixel location
    			
    			g.drawString("" + (i + 1), 50, yDraw);
                            //This is where I would like to draw the elements of the list. 
    		}
    	}
    }
    When the player moves over the shop, it should bring up a menu which lists the items. The firs column, which I have working, is just the items numbered in order. The second column, I would like to be the item's name, the third, the value, and the fourth, the weight. Does that make sense?

Similar Threads

  1. Linked List, Array List time complexity
    By Rick99771977 in forum New To Java
    Replies: 4
    Last Post: 08-18-2011, 05:37 AM
  2. Replies: 4
    Last Post: 02-21-2011, 09:34 AM
  3. Linked list inside a linked list
    By viperlasson in forum New To Java
    Replies: 5
    Last Post: 07-26-2010, 11:15 PM
  4. Linked list
    By rosh72851 in forum New To Java
    Replies: 1
    Last Post: 02-05-2009, 07:21 AM
  5. Linked List integer list
    By igniteflow in forum Advanced Java
    Replies: 1
    Last Post: 12-10-2008, 08:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •