Results 1 to 7 of 7
- 06-10-2012, 06:25 PM #1
Member
- Join Date
- May 2012
- Posts
- 30
- Rep Power
- 0
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
- 06-10-2012, 06:39 PM #2
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 iIf you don't understand my response, don't ignore it, ask a question.
- 06-10-2012, 07:03 PM #3
Member
- Join Date
- May 2012
- Posts
- 30
- Rep Power
- 0
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!
- 06-10-2012, 07:35 PM #4
Re: Outputting Elements of a Linked List
Read the API doc for the class you are using and see if there are any methods that will do what you want.access a particular item. So, if the player chooses item n, how would you access the data?If you don't understand my response, don't ignore it, ask a question.
- 06-10-2012, 07:46 PM #5
Member
- Join Date
- May 2012
- Posts
- 30
- Rep Power
- 0
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:
Again, the only issue is how to get itr.next to output the name within the class that the list is made up of.Java Code:itr = list.iterator(); while(itr.hasNext()) { g.drawString(itr.next(), 50, yDraw); yDraw += 15; }
Thanks!
- 06-10-2012, 07:53 PM #6
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.
- 06-10-2012, 08:03 PM #7
Member
- Join Date
- May 2012
- Posts
- 30
- Rep Power
- 0
Re: Outputting Elements of a Linked List
It's a class I made. Here it is:
And here's the code from the class where it is outputted: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 }
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?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. } } }
Similar Threads
-
Linked List, Array List time complexity
By Rick99771977 in forum New To JavaReplies: 4Last Post: 08-18-2011, 05:37 AM -
How to access an element of a linked list inside another linked list?
By smtwtfs in forum New To JavaReplies: 4Last Post: 02-21-2011, 09:34 AM -
Linked list inside a linked list
By viperlasson in forum New To JavaReplies: 5Last Post: 07-26-2010, 11:15 PM -
Linked list
By rosh72851 in forum New To JavaReplies: 1Last Post: 02-05-2009, 07:21 AM -
Linked List integer list
By igniteflow in forum Advanced JavaReplies: 1Last Post: 12-10-2008, 08:53 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks