Javelin question with linkedlist
MightyBeast
Posts: 6
i want to use linkedlist and i want the value of each node to be a array of char.
can anybody tell me how would i do this?
do i have to write a linkedlist class with add, remove functions, or can i just import it from library
thanks
can anybody tell me how would i do this?
do i have to write a linkedlist class with add, remove functions, or can i just import it from library
thanks
Comments
See page 157 and further in the manual.
As an alternative there is my implementation of a heap,·list, stack and queue.
http://tech.groups.yahoo.com/group/JavelinCode/files/Javelin%20Stamp%20IDE/lib/stamp/peripheral/memory/
If your goal is to store your list in an external memory like eeprom, then my
implementation is more suitable because it allows you to add and remove from the list
without copying it back to memory first (which must be when using object references).
regards peter
I have one more question, the add method in the list object take parameter of an object, what can i do to make it takes a string or an array of char?
Thanks
for a String you could define
package stamp.util;
public class StringLinkedListItem extends LinkedListItem {
· private String s = new String();
· public StringLinkedListItem() {
··}
··//example of method for reading a char
· public char charAt(int i) {
··· return s.charAt(i);
· }
}
In your main application program,use
static StringLinkedListItem myItem = new StringLinkedListItem();
You can of course define any datastructure plus methods that you like.
The key is to extend the LinkedListItem class, becaus these objects
can be inserted/removed/added to the LinkedList object.
regards peter
import stamp.util.*;
public class ListTest {
static StringLinkedListItem myItem = new StringLinkedListItem();
static LinkedList myList = new LinkedList();
public static void main() {
myItem.addString("Hello World");
myList.addItem(myItem);
System.out.println((myList.getFirst()).getString());
myList.removeItem(myItem);
}
}
class StringLinkedListItem extends LinkedListItem {
private String s = new String();
public StringLinkedListItem() {
}
public void addString(String a)
{
s = a;
}
public String getString()
{
return s;
}
}
In the main method, why it doesnt print the value of the first item? and also if i want to add more strings to the linkedlist, i have to create more StringLinkedListItem objects, and just add to the same list right?
So I changed it a little.
Here is the application file
import stamp.core.*;
import stamp.util.*;
public class LinkedListTest {
· static StringLinkedListItem myItem = new StringLinkedListItem();
· static LinkedList myList = new LinkedList();
· static StringLinkedListItem k;
· public static void main() {
··· myItem.addString("Hello World");
··· myList.addItem(myItem);
··· k = (StringLinkedListItem)myList.getFirst();
··· System.out.println(k.getString());
··· myList.removeItem(myItem);
· }
}
Notice that I cast LinkedListItem to StringLinkedListItem.
That is required because LinkedListItem does not have a getString() method.
To add more items to the list, you must indeed first create more StringLinkedListItem objects.
regards peter
Thanks a lot for your help. because of that, i was able to make a progress on my project.
thanks again