2015-10-24 140 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