1

我有一个字符数组,我试图将每个字符转换为一个节点,该节点链接到行中的下一个节点。问题是我一直陷入无限循环,我不知道为什么。这是我的代码:

String map = "ABBACBCCA";
char[] charArray = map.toCharArray();
ListNode head;
ListNode temp;
ListNode next;

for (int i = 0; i < charArray.length - 1; i++) {
     temp = new ListNode(charArray[i]);
     next = new ListNode(charArray[i+1]);
     temp.next = next;

     if (i == 0) {
          head = temp;
     }
}

ListNode 类看起来像:

class ListNode<T> {
     public T data = null;
     public ListNode next = null;

     public ListNode(T data) {
          this.data = data;
     }
}

看起来它到达了 for 循环的最后一次迭代,然后陷入了无限循环。有人知道为什么吗?

4

4 回答 4

1

首先,我认为您会想要:

next = new ListNode(charArray[i]);

成为

next = new ListNode(charArray[i+1]);

我注意到的另一件事:

for (int i = 0; i < charArray.length - 1; i++) {
     temp = new ListNode(charArray[i]);
     next = new ListNode(charArray[i+1]);
     temp.next = next;

          if (i == 0) {
            head = temp;
           }
     }

我不认为这会产生你想要的。它不会给你 A->B->B->A 等等。它会给你 -> A->B,B->B 等等等等。不确定这是否是你所追求的。

此外,我认为这应该会让你很好:

String map = "ABBACBCCA";
        ListNode<Character> head = null;
        ListNode<Character> newHead = null;
        ListNode<Character> next = null;

        char[] charArray = map.toCharArray();

        head = newHead = new ListNode<Character>(charArray[0]);
        for (int i = 1; i < charArray.length - 1; i++) {
            next = new ListNode<Character>(charArray[i]);

            newHead.next = next;

            newHead = next;
        }

基本上创建和链接以及创建和链接。(对我来说很好测试)进来丑陋!

System.out.println(head.data);
        ListNode<Character> nextptr = head.next;
        while (true) {

            if (nextptr.next == null) {
                break;
            }
            System.out.println(nextptr.data);
            nextptr = nextptr.next;
        }
于 2013-10-23T03:43:47.700 回答
0

如果您想继续开发自己的代码,最好使用调试器。您可能应该创建一些公共方法来设置 LinkList 节点的下一个元素,就像我在本示例中所做的那样。解释会很长,所以这里是代码。

public class Test {

 public static void main(String[] args) {
    ListNode<Character> head = new Test().arrayToLinkList();

    while ((head = head.nextNode()) != null) {
        System.out.println(head.readData());
    }
 }

 public ListNode<Character> arrayToLinkList() {
    String map = "ABBACBCCA";
    char[] charArray = map.toCharArray();
    ListNode<Character> head, next;
    head = new ListNode<Character>(charArray[0]);
    next = head;
    for (int i = 0; i < charArray.length - 1; i++) {
        next = next.next(new ListNode<Character>(charArray[i + 1]));
    }
    return head;
 }
}

class ListNode<T> {
 private T data = null;
 private ListNode<T> next = null;

 public ListNode(T data) {
    this.data = data;
 }

 public ListNode<T> next(ListNode<T> next) {
    this.next = next;
    return this.next;
 }

 public ListNode<T> nextNode() {
    return this.next;
 }

 public T readData() {
    return data;
 }
}
于 2013-10-23T05:01:56.307 回答
0

在每次迭代期间,您的参考变量 temp 和 next 都被分配给新对象,并且您失去了对 next 指针的跟踪。您可以按照其他人的建议使用调试器解决此问题。这是工作示例。

public class Test {

    public static void main(String args[]) {
        String map = "ABBACBCCA";
        char[] charArray = map.toCharArray();
        ListNode<Character> head = null, temp = null;
        for (int i = 0; i < charArray.length; i++) {
            ListNode<Character> obj = new ListNode<Character>(charArray[i]);
            if (temp != null) {
                temp.next = obj;
            } else {
                head = obj;
            }
            temp = obj;
        }
        // Print the list
        while (head != null) {
             System.out.println(head.data);
             head = head.next;
        }
    }
}

class ListNode<T> {
    public T data = null;
    public ListNode<T> next;
    public ListNode(T data) {
        this.data = data;
        this.next = null;
    }
}
于 2013-10-23T06:58:53.080 回答
0

这可能是构建链表的更简单方法之一:

String map = "ABBACBCCA";

ListNode<Character> head = null;
ListNode<Character> tail = null;

for (char c:map.toCharArray()) {
  final ListNode<Character> node = new ListNode<>(c);
  if (head == null) {
    head = node;
  } else {
    tail.next = node;
  }
  tail = node;
}

同时照顾你的类型参数无处不在

class ListNode<T> {
  public T data = null;
  ListNode<T> next = null;

  public ListNode(T data) {
    this.data = data;
  }
}

更不用说您也可以充分利用Java api

LinkedList<Character> ll =
  "ABBACBCCA".chars()
    .mapToObj(i->(char)i) // takes care of boxing char
    .collect(Collectors.toCollection(LinkedList<Character>::new));

另外要遍历你ListNode的,你可以考虑添加:

class ListNodeIterator<T> implements Iterator<T> {
  private ListNode<T> current;

  public ListNodeIterator<T> ListNodeIterator(ListNode<T> node) {
    current = node;
  }

  public boolean hasNext() {
    return current.next != null;
  }

  public T next() {
    current = current.next;
    return current.data;
  }
}

通过以下修改:

class ListNode<T> implements Iterable<T> {
  public T data = null;
  public ListNode<T> next = null;

  public ListNode(T data) {
    this.data = data;
  }

  public Iterator<T> iterator() {
    return new ListNodeIterator<>(this);
  }
}

因此,您可以按如下方式使用它:

for (char c:head) {
  System.out.println("Character: "+c);
}

甚至

head.forEach(c->{System.out.println("Character: "+c);});

嗯……这不是一次愉快的爪哇之旅吗?

于 2017-04-10T04:27:03.150 回答