【问题标题】:Convert integer to string for linked list? [closed]将整数转换为链表的字符串? [关闭]
【发布时间】: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


【解决方案1】:

您不能创建LinkedList&lt;int&gt;,因为泛型类型不能是原始类型,但话虽如此,将整数添加到 LinkedList 很容易,无需将它们“转换”为字符串。

即,使用LinkedList&lt;Integer&gt;

否则,如果这不能回答您的问题,那么请告诉我们更多有关实际问题的信息,而不是您提出的代码解决方案。我怀疑你的问题真的是伪装的XY-problem

【讨论】:

  • 对于每个值,可以使用toString()方法将其转换成String。
  • @RahulKumar:但是为什么呢?这有什么暗示?所提出的原始问题过于模糊和过于宽泛,目前无法回答。
  • 我想是因为他要求将整数的 LL 转换为字符串。
【解决方案2】:

您可以使用迭代来做到这一点。
然后使用Integer.toString方法将每个元素从Integer转换为String。

LinkedList<String> strings = new LinkedList<String>();
for(LinkedList<Integer> item : integers ){
   strings.add(Integer.toString(item));
}

这对你有用。

【讨论】:

    猜你喜欢
    • 2012-11-04
    • 2012-05-01
    • 1970-01-01
    • 2013-12-29
    • 2015-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多