2015-10-24 142 views
0

所以这是针对类项目的,并且遇到了一个我不知道如何导致的问题。我正在写一种方法,假设要获取一个RNA或DNA序列,将其解析为一个char数组,验证所有字符适合于序列类型,然后将该数组转换为单个链接列表,其中每个节点包含一个字母,那么我想将这个列表传递给外部链表的fList节点。尝试将序列作为链接​​列表存储在链接列表中 - Java

//if sequence is valid fill frag with base sequence and pass the list to 
    //fragment list (fList) at position of insert 
    if(validSeq){ 
     fList.moveTo(pos); 
     frag.moveToStart(); 
     for(int count = 0;count < sequence.length(); count++){    
      frag.insert(seq[count]); 
     } 

     fList.insert(frag); 
     System.out.print(fList.getValue()); 
    } 
    else{ 
     System.out.print("Invalid Sequence: not stored "); 
    } 

,但我已经得到的输出是:

  • [cccccccccccccccccccccc对于attacgatctgcacaagatcct
  • [tttttttt对于ggggtttt
  • [AAAAAA]为aaccaa

它似乎是重复整个事情的第二封信,但我不知道为什么。我确信是什么导致它在方法的上述块中。第一次在这里发帖非常抱歉,如果我没有正确地做到这一点或缺少明显的东西。

编辑:这是我使用

/** 
* Singly Linked list implementation which 
* uses package private {@link Node} to store node values. 
* @see List 
*/ 
public class SLList<E> implements List<E> { 
    private transient Node<E> head;  // Pointer to list header 
    private transient Node<E> tail;  // Pointer to last element 
    private transient Node<E> curr;  // Access to current element 
    private transient int listSize;  // Size of list 

    /** 
    * Create a new empty singly linked list. 
    */ 
    public SLList() { 
    curr = tail = new Node<E>(null); 
    head = new Node<E>(tail); 
    listSize = 0; 
    } 

    /** 
    * {@inheritDoc} 
    */ 
    @Override 
    public void clear() { 
    curr = tail = new Node<E>(null); 
    head = new Node<E>(tail); 
    listSize = 0; 
    } 

    /** 
    * {@inheritDoc} 
    */ 
    @Override 
    public boolean insert(E it) { 
    curr.setNext(new Node<E>(curr.element(), curr.next())); 
    curr.setElement(it); 
    if (tail == curr) { 
     tail = curr.next(); 
    } 
    listSize++; 
    return true; 
    } 

    /** 
    * {@inheritDoc} 
    * This append operatoin will not increment the current element reference. 
    */ 
    @Override 
    public boolean append(E it) { 
    tail.setNext(new Node<E>(null)); 
    tail.setElement(it); 
    tail = tail.next(); 
    listSize++; 
    return true; 
    } 

    /** 
    * {@inheritDoc} 
    */ 
    @Override 
    public E remove() { 
    if (curr == tail) { 
     return null;   // Nothing to remove 
    } 
    E it = curr.element();     // Remember value 
    curr.setElement(curr.next().element()); // Pull forward the next element 
    if (curr.next() == tail) { 
     tail = curr; // Removed last, move tail 
    } 
    curr.setNext(curr.next().next());  // Point around unneeded link 
    listSize--;        // Decrement element count 
    return it;        // Return value 
    } 

    /** 
    * Move the current element reference to the head of the list. 
    */ 
    @Override 
    public void moveToStart() { 
    curr = head; 
    } 

    /** 
    * Move the current element reference to the tail of the list. 
    */ 
    @Override 
    public void moveToEnd() { 
    curr = tail; 
    } 

    /** 
    * Move the current element reference one step closer to the 
    * list head. 
    * If the current element is already at the head, this method 
    * does nothing. 
    * <dl> 
    * < dt>Note:</dt> 
    * <dd>As this is a singly linked list, the {@link #prev} operation 
    * can be expensive - up to O(n^2) on large lists.</dd> 
    * </dl> 
    * @return the value of the previous node in the list. 
    */ 
    @Override 
    public E prev() { 
    if (head == curr) { 
     return null; // No previous element 
    } 
    Node<E> temp = head; 
    // March down list until we find the previous element 
    while (temp.next() != curr) { 
     temp = temp.next(); 
    } 
    curr = temp; 
    return curr.element(); 
    } 

    /** 
    * Move the current element reference one step closer to the 
    * list tail. 
    * If the current element is already at the tail, this method 
    * returns null. 
    * @return the value of the next node in the list. 
    */ 
    @Override 
    public E next() { 
    if (curr != tail) { 
     curr = curr.next(); 
    } 
    return curr.element(); 
    } 


    /** 
    * {@inheritDoc} 
    */ 
    @Override 
    public int length() { 
    return listSize; 
    } 


    /** 
    * {@inheritDoc} 
    */ 
    @Override 
    public int position() { 
    Node<E> temp = head; 
    int i; 
    for (i=0; curr != temp; i++) { 
     temp = temp.next(); 
    } 
    return i; 
    } 

    /** 
    * {@inheritDoc} 
    * @return null if pos does not refer to a valid position in the list 
    */ 
    @Override 
    public E moveTo(int pos) { 
    if ((pos < 0) || (pos > listSize)) { 
     return null; 
    } 
    curr = head; 
    for(int i=0; i<pos; i++) { 
     curr = curr.next(); 
    } 
    return curr.element(); 
    } 

    /** 
    * {@inheritDoc} 
    */ 
    @Override 
    public boolean isAtEnd() { 
    return curr.next() == tail; 
    } 

    /** 
    * {@inheritDoc} 
    * Note that null gets returned if the current reference is at the tail 
    */ 
    @Override 
    public E getValue() { 
    return curr.element(); 
    } 

    /** 
    * Display the string representation of the value stored within 
    * each element in the list. 
    * The entire list is bounded by square brackets. 
    */ 
    @Override 
    public String toString() { 
    StringBuilder sb = new StringBuilder(); 
    curr = head; 
    for(int i=0; i<listSize; i++) { 
     sb.append(curr.next().toString()); 
    } 
    return "[" + sb.toString() + "]"; 
    } 

} 

编辑单向链表:这是使我整个方法麻烦

//sends sequence to flist after verifying their validity 
    public static void sendToFlist(String sequence, sequenceType stype, int pos, 
      SLList fList){ 

     char[] seq = new char[sequence.length()]; 
     boolean validSeq = true; 
     SLList frag = new SLList(); 
     frag.clear(); 

     //turn sequence into an array for validity parsing 
     for(int count = 0;count < sequence.length(); count++){ 
      seq[count] = sequence.charAt(count);      
     } 

     //check sequence validity based on sequence type enum 
     switch(stype){ 
       case RNA: 
        for(int count = 0;count < sequence.length(); count++){ 
         if(!(Character.valueOf(seq[count]).equals('a'))&& 
           !(Character.valueOf(seq[count]).equals('g'))&& 
           !(Character.valueOf(seq[count]).equals('c'))&& 
           !(Character.valueOf(seq[count]).equals('u'))){ 
          validSeq = false; 
         } 
        } 
        break; 

       case DNA: 
        for(int count = 0;count < sequence.length(); count++){ 
         if(!(Character.valueOf(seq[count]).equals('a'))&& 
           !(Character.valueOf(seq[count]).equals('g'))&& 
           !(Character.valueOf(seq[count]).equals('c'))&& 
           !(Character.valueOf(seq[count]).equals('t'))){ 
          validSeq = false; 
         } 
        } 
        break; 
     } 

     //if sequence is valid fill frag with base seqence and pass the list to 
     //fragment list (fList) at position of insert 
     if(validSeq){ 
      fList.moveTo(pos); 
      frag.moveToStart(); 
      for(int count = 0;count < sequence.length(); count++){    
       frag.insert(seq[count]); 
      } 

      fList.insert(frag); 
      System.out.print(fList.getValue()); 
     } 
     else{ 
      System.out.print("Invalid Sequence: not stored "); 
     } 
    } 

但我检查的阵列,它似乎要正确地存储序列

+0

发布你的列表实现的代码,或考虑使用标准的ArrayList –

+0

给我们的单向链表是我们应该使用的,我为什么这让我如此卡住 – Tredecian

+0

你可以发布整个方法错误在哪里发生? –

回答

0

这实际上很有趣:

StringBuilder sb = new StringBuilder(); 
    curr = head; 
    for(int i=0; i<listSize; i++) { 
     sb.append(curr.next().toString()); 
    } 
    return "[" + sb.toString() + "]"; 

您不会递增curr,因此一遍又一遍地打印相同的元素curr.next()

您也可能在那里有错误的订单,因此curr.next是前一个,而不是第一个之后的一个,但这是另一个问题。

我相信它应该是这样的:

for(int count = 0;count < sequence.length(); count++){    
     frag.append(seq[count]);      
    } 

所以大概名单几乎是OK,只是输出是错误的。

+0

是的,我想我明白了。非常感谢!! – Tredecian