Извините за туманную информацию. Вот что я пытаюсь. Я пытаюсь создать метод, который принимает мой объект 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;
}
}
Вопрос по запросу не имеет никакого смысла. –