【发布时间】:2013-11-22 22:57:46
【问题描述】:
抱歉,信息含糊。这是我正在尝试的。我正在尝试创建一个方法,该方法采用我的对象 LString 并将整数参数转换为对象 LString。这是使用链接列表。我有一个 Node 类,它构造和初始化节点。这是我完成一个项目的最后一种方法。
但是,我对如何去做感到困惑。我一直在使用链表和节点创建一个字符串类。如何将此整数参数转换为我的 LString 对象类型?
以下是我的 LString 类中似乎与问题相关的部分:
public class LString{
private Node front ; //first val in list *******CHANGED
private Node back; //last val in list
private int size = 0;
private int i;
private int offset;
public LString(){
//construct empty list
Node LString = new Node();
front = null;
}
//return value of specified index
public char charAt(int index){
Node current = front;
for(int i = 0; 0 < index; i++){
current = current.next;
}
return current.data;
}
//return number of chars of lstring
public int length(){
int count = 0;
Node current = front;
while(current != null){
current = current.next;
count++;
}
return count++;
}
public String toString(){
if(front == null){
return "[]";
} else {
String result = "[" + front.data;
Node current = front.next;
while(current != null){
result += current.data; //might need to add ", page 967
current = current.next;
}
result += "]";
return result;
}
}
//****我的尝试虽然非常错误*强>
public static LString valueOf(int i){
int c;
char m;
LString ans = new LString();
Node current = new Node();
// convert the String to int
for(int w = i;w < i; w++) {
c = i % 10;
i = i / 10;
m = (char) ('0' + c);
}
return ans;
}
我的节点类:
public class Node{
public char data;
public Node next;
//constructors from page 956
public Node()
{
this('\0',null); //'\0' is null char for java
}
public Node(char initialData, Node initialNext)
{
data = initialData;
next = initialNext;
}
public void addNodeAfter(char element)
{
next = new Node(element, next);
}
public char getData()
{
return data;
}
public Node getNext(){
return next;
}
public void setNext(Node n){
next = n;
}
public void setData(char d){
data = d;
}
}
【问题讨论】:
-
所提出的问题没有任何意义。
标签: java linked-list typeconverter