正在拆分链表中的值



我正在编写一个表示重组DNA过程的代码。我正在学习如何用我的切割和连接方法向前迈进。我甚至写了一个循环,搜索我们正在寻找的酶的索引,但现在我遇到了一堵砖墙。

我的类表示一个链表,它的作用如下:

  • 首先,它得到一个用字符串表示的DNA链,并将整个LinkedList的第一个节点中的字符串
  • 然后它在链中搜索特定的酶序列(字符串node((cutAndConnect中的while循环(
  • 在这一点上,在酶的末端,切断酶
  • 然后它需要将连接放置在一个新节点中在另一个新节点中的酶序列之后的链(字符串(
  • 它需要对整个环节都这样做,所以在所有时候特定的酶序列出现在链中

我还添加了所有我还没有推杆的方法。DnaStreng是这个项目中使用的接口,但它没有将接口添加到这个问题中,因为它基本上只包含所有方法的javascript。

我希望你们中的一些人能帮助我继续前进。我有以下代码:

public class LinkStreng implements DnaStreng{
public static LinkStreng STRING_LEEG = new LinkStreng();
private String dna;
List<String> dnaList= new LinkedList<String>();
Node head;
Node next;
/* Makes a new String with length 0*/
public LinkStreng(){
this("");
}
/* Makes a new LinkStreng with the given DNA(in the main class there is a 
filechooser used.*/
public LinkStreng(String hetDna){
dna = hetDna;
head = null;
Node firstNode = new Node(hetDna);
}
/*Counts the number of Nodes in the list*/
public int getCount(){
Node temp = head;
int count = 0;
while (temp!= null){
count++;
temp = temp.next;
}
return count;
}
/ ** Simulates a cut by a restriction enzyme. Seeks the first occurrence
* of the enzyme in this strand and removes the enzyme and everything after it
* coming. Returns the part after the enzyme as a new strand. If the
* enzyme does not occur in this strength, it remains unchanged and an
* returned empty strand will be returned.
* @param enzyme the string to search for
* @return the part of this strength after the enzyme */
public DnaStreng cutWith(String enzym) {
// Here I get an error that it cannot find the getCount method
if (dnaList.getCount() >1){
throw new RuntimeException("Linkstreng heeft meer dan 1 node");
} else {             
int enzymBegin = dna.indexOf(enzym);
if (enzymBegin == -1) return STRENG_LEEG;
// Enzyme found: cut
String dnaAchterEnzym = dna.substring(enzymBegin + enzym.length());
DnaStreng achter = new LinkStreng(dnaAchterEnzym);
initialiseer(dna.substring(0, enzymBegin));
return achter;
}
}

/** Cut this strand wherever the enzyme occurs and connect the
* pieces by placing the connection between them.
* @param enzym the string to search for
* @param connectionthe DNA that will replace the enzyme
* @return the new strand (the original strand remains unchanged) */
public DnaStreng cutAndConnect(String enzym, String connection) {
// With this getCount I get the same error
if (dnaList.getCount() > 1){
throw new RuntimeException("Linkstreng heeft meer dan 1 node");
}
String teSplitsen = " " + dna + " ";
int i = 0;
while ( i < dna.length()){
dna.indexOf(enzym, i);
i++;
}
return new LinkStreng();
}
/* Gives the number of letters in the strand */
public long lengte() {
// this method isn't working as well, it returns the wrong number
return dnaList.toString().length();
}
/** Initializes this strand by inserting the DNA, any
* previous data is erased. Does not check for valid DNA letters.
* @param dna the DNA string placed in this strand * /
public void initialiseer(String hetDna) {
}

/** Adds the addition behind this strand.
* @param addition the strand being added */
public void voegToe(DnaStreng addition) {
}
/** Adds the addition behind this strand.
* @param addition the strand being added */
public void voegToe(String addition) {
}

public String toString(){
Node current = head;
StringBuilder recombinant = new StringBuilder((CharSequence) dnaList);
while(current != null){
recombinant = recombinant.append(head.value);
current = current.next;
}
return recombinant.toString();
}
}
public class Node {
public Node next;
public String value;
public Node(String s){
value = s;
next = null;
}
}

}

代码似乎是旨在实现相同目标的矛盾方法的混合体。

例如:

List<String> dnaList = new LinkedList<String>();
Node head;
Node next;
  • 一方面,本例中的第一行建议您尝试使用Java标准库中的LinkedList

  • 另一方面,接下来的两行是尝试使用链表数据结构的自定义实现的证据。

还有更多的例子说明为什么这个代码不起作用,我相信这一个已经足够令人信服了。

一般来说,为了避免一次编写整个程序的混乱方法(罗马不是一天建成的!(我强烈建议一次只写一小部分,并为每一部分编写测试。

我将演示提议的方法,并逐步浏览您的列表:

  1. 首先,它获取一条表示为字符串的DNA链,并将整个字符串放置在LinkedList的第一个节点中

很可能,您不想将整个字符串存储在一个节点中。你真正想要的是存储一个字符(它只是一个字母,而字符串是一系列字母(。

public class DNAStrand {
Node head = null;
public DNAStrand(final String dnaStrand) {
if (dnaStrand.isEmpty()) {
return;
}
// Initialise the first node of a list
Node currentNode = new Node(dnaStrand.charAt(0));
// Assign the first Node to `head`
head = currentNode;
// Iterate through the rest of string storing remaining characters
for (int i = 1; i < dnaStrand.length(); i++) {
final Node nextNode = new Node(dnaStrand.charAt(i));
currentNode.next = nextNode;
currentNode = nextNode;
}
}
private static class Node {
public Node next;
public char value;
public Node(char s) {
value = s;
next = null;
}
}
// Tests are located below this line
private static void testDnaStrandIsPlaceDInTheFirstNodeOfLinkedList() {
final DNAStrand dnaStrand = new DNAStrand("acgt");
if (dnaStrand.head == null) {
throw new AssertionError("The head is null after creating an object from a string");
}
List<Character> actualString = new ArrayList<>();
Node listPointer = dnaStrand.head;
do {
actualString.add(listPointer.value);
listPointer = listPointer.next;
} while (listPointer != null);
if (!"acgt".equals(actualString.stream().map(String::valueOf).collect(Collectors.joining()))) {
throw new AssertionError("Wrong value in the first node, expected: 'agct;, got: "
+ dnaStrand.head.value);
}
}
public static void main(String[] args) {
testDnaStrandIsPlaceDInTheFirstNodeOfLinkedList();
}
}

在我的示例中,main方法执行测试testDnaStrandIsPlaceDInTheFirstNodeOfLinkedList。这个测试可以确保一个简单的合同得到满足。随着代码越来越大,您最好添加更多的测试,以确保不会破坏旧功能并证明新代码运行良好。

  • 然后在链中搜索特定的酶序列(节点中的字符串((cutAndConnect中的while循环(
  • 然后它需要在这一点上切断绳子,在酶的末端,切断酶
  • 然后,它需要将连接放在一个新节点中,并将酶序列之后的链(字符串(部分放在另一个新的节点中
  • 它需要对整个链这样做,所以在特定酶序列出现在链中的所有时间里

本质上,这是对一个方法的描述。我将添加一个cutAndConnect(),它可以满足您的要求,以及解释/证明它有效的测试。

public class DNAStrand {
Node head = null;
public DNAStrand(final String dnaStrand) {
if (dnaStrand.isEmpty()) {
return;
}
head = stringToListNodes(dnaStrand);
}
private static class Node {
public Node next;
public Node previous;
public char value;
public Node(char character, final Node previousNode) {
this.value = character;
this.next = null;
this.previous = previousNode;
}
}
private Node stringToListNodes(final String string) {
// Initialise the first node of a list
Node currentNode = new Node(string.charAt(0), null);
// Assign the first Node to `head`
final Node head = currentNode;
// Iterate through the rest of string storing remaining characters
for (int i = 1; i < string.length(); i++) {
final Node nextNode = new Node(string.charAt(i), currentNode);
currentNode.next = nextNode;
currentNode = nextNode;
}
return head;
}
public void cutAndConnect(final String enzyme, final String linker) {
if (linker.isEmpty()) {
throw new IllegalArgumentException("Linked can't be empty");
}
Node potentialEnzymeStart = head;
while (potentialEnzymeStart != null) {
Node currentNode = potentialEnzymeStart;
boolean dismatchFound = false;
for (char enzymeSymbol : enzyme.toCharArray()) {
if (enzymeSymbol != currentNode.value) {
dismatchFound = true;
break;
}
currentNode = currentNode.next;
}
if (!dismatchFound) {
// The enzyme does match the sequence
// The DNA has the following structure <left piece> <enzyme> <right piece>. Find the left and right piece nodes
Node leftPieceEnd = potentialEnzymeStart.previous;
if (leftPieceEnd == null) {
// Replace the current head
head = stringToListNodes(linker);
} else {
// Simply connect a node of a doubly linked list
final Node linkedInAFormOfList = stringToListNodes(linker);
leftPieceEnd.next = linkedInAFormOfList;
linkedInAFormOfList.previous = leftPieceEnd;
}
Node connectionPoint = leftPieceEnd == null ? head : leftPieceEnd.next;
for (int i = 0; i < linker.length() - 1; ++i) {
connectionPoint = connectionPoint.next;
}
Node rightPieceStart = potentialEnzymeStart;
for (int i = 0; i < enzyme.length(); ++i) {
rightPieceStart = rightPieceStart.next;
}
if (rightPieceStart != null) {
connectionPoint.next = rightPieceStart;
rightPieceStart.previous = connectionPoint;
}
}
potentialEnzymeStart = potentialEnzymeStart.next;
}
}
// Tests are located below this line
private static void testDnaStrandIsPlaceDInTheFirstNodeOfLinkedList() {
final DNAStrand dnaStrand = new DNAStrand("acgt");
if (dnaStrand.head == null) {
throw new AssertionError("The head is null after creating an object from a string");
}
List<Character> actualString = new ArrayList<>();
Node listPointer = dnaStrand.head;
do {
actualString.add(listPointer.value);
listPointer = listPointer.next;
} while (listPointer != null);
if (!"acgt".equals(actualString.stream().map(String::valueOf).collect(Collectors.joining()))) {
throw new AssertionError("Wrong value in the first node, expected: 'agct`, got: "
+ dnaStrand.head.value);
}
}
private static void testCutAndConnectCutEntireDNAString() {
final DNAStrand dnaStrand = new DNAStrand("acgt");
dnaStrand.cutAndConnect("acgt", "a");
if (dnaStrand.head == null) {
throw new AssertionError("The head of a list must not be null");
}
if (dnaStrand.head.value != 'a') {
throw new AssertionError("The head of the list contains wrong value, expected: 'a`, got: " +
dnaStrand.head.value);
}
if (dnaStrand.head.next != null) {
throw new AssertionError("The list must have the length 1");
}
}
private static void testCutAndConnectCutTheMiddleOfDNAString() {
final DNAStrand dnaStrand = new DNAStrand("acca");
dnaStrand.cutAndConnect("cc", "g");
List<Character> actualString = new ArrayList<>();
Node listPointer = dnaStrand.head;
do {
actualString.add(listPointer.value);
listPointer = listPointer.next;
} while (listPointer != null);
if (!"aga".equals(actualString.stream().map(String::valueOf).collect(Collectors.joining()))) {
throw new AssertionError("Wrong value in the list, expected: 'aga`, got: "
+ dnaStrand.head.value);
}
}
private static void testCutAndConnectCutMultipleOccurrencesOfAnEnzyme() {
final DNAStrand dnaStrand = new DNAStrand("accacca");
dnaStrand.cutAndConnect("cc", "g");
List<Character> actualString = new ArrayList<>();
Node listPointer = dnaStrand.head;
do {
actualString.add(listPointer.value);
listPointer = listPointer.next;
} while (listPointer != null);
if (!"agaga".equals(actualString.stream().map(String::valueOf).collect(Collectors.joining()))) {
throw new AssertionError("Wrong value in the list, expected: 'agaga`, got: "
+ dnaStrand.head.value);
}
}
public static void main(String[] args) {
testDnaStrandIsPlaceDInTheFirstNodeOfLinkedList();
testCutAndConnectCutEntireDNAString();
testCutAndConnectCutTheMiddleOfDNAString();
testCutAndConnectCutMultipleOccurrencesOfAnEnzyme();
}
}

我增加了更多的测试,测试非常善于描述正在发生的事情。我建议开始从中阅读od。

这看起来像是你想要实现的目标吗?

一般来说,您发布的问题不符合StackOverflow标准。仅仅因为这不是一个问题,但更多的是"我的代码不起作用":(

下次,试着缩小问题的范围,并提供一个最小的代码示例。

相关内容

  • 没有找到相关文章

最新更新