【发布时间】:2019-07-16 02:23:01
【问题描述】:
我在 java 中创建了自己的链表(代码如下),我试图将来自文本文件的 DNA?RNA 序列存储在包含枚举 DNA/RNA 的自定义数据类型的数组中,以及链接的包含实际 DNA 序列的列表。我不知道字符是否只是没有插入到链接列表中,或者我的 toString 是否存在问题,但输出仅打印位置和枚举类型,而不是列表中的序列。代码如下 我的节点类
public class Node<E> {
private Node<E> next;
protected E data;
Node(E data,Node<E> nextVal){
this.data=data;
next=nextVal;
}
Node(Node<E> nextVal){
next=nextVal;
}
Node<E> Next(){
return next;
}
Node<E>setNext(Node<E> nextVal){
return next=nextVal;
}
E data(){
return data;
}
E setData(E it){
return data=it;
}
}
我的链表类
public class MyLinkedList<E> implements List<E>{
private Node<E> head;
private Node<E> tail;
protected Node<E> curr;
private int size;
MyLinkedList(int size){
this();
}
MyLinkedList(){
curr=tail=head=new Node<E>(null);
size=0;
}
@Override
public void clear() {
head.setNext(null);
curr=tail=head=new Node<E>(null);
size=0;
}
@Override
public void insert(E item) {
curr.setNext(new Node<E>(item, curr.Next()));
if(tail==curr)
tail=curr.Next();
size++;
}
@Override
public void append(E item) {
tail=tail.setNext(new Node<E>(item, null));
size++;
}
@Override
public E remove() {
if(curr.Next() ==null)
return null;
E item=curr.Next().data();
if(tail==curr.Next())
tail=curr;
curr.setNext(curr.Next().Next());
size--;
return item;
}
@Override
public void moveToStart() {
curr =head;
}
@Override
public void moveToEnd() {
curr=tail;
}
@Override
public void prev() {
if(curr==head)
return;
Node<E> temp=head;
while (temp.Next()!=curr)
temp=temp.Next();
curr=temp;
}
@Override
public void next() {
if(curr!=tail)
curr=curr.Next();
}
@Override
public int length() {
return size;
}
@Override
public int currPos() {
Node<E>temp=head;
int i;
for(i=0;curr!=temp;i++)
temp.Next();
return i;
}
@Override
public void moveToPos(int pos) {
assert (pos>=0)&& (pos<=size):
"Position out of Range";
curr=head;
for(int i=0;i<pos;i++)
curr.Next();
}
@Override
public E getValue() {
if(curr.Next()==null)
return null;
return curr.Next().data();
}
@Override
public String toString() {
String result = "";
Node current = head;
while(current.Next() != null){
result += current.data();
if(current.Next() != null){
result += ", ";
}
current = current.Next();
}
return "" + result;
}
}
这是处理上述数组操作的 SequenceArr 类(此处不完整,但本示例中使用的所有内容)
public class SequenceArr {
private TypeSeq [] SeqArr;
private int size=0;
private int MAXSIZE;
public SequenceArr(int MAXSIZE){
this.MAXSIZE=MAXSIZE;
SeqArr =new TypeSeq[MAXSIZE];
size=0;
}
public void insert(int pos, Type t, MyLinkedList<Character> seq){
TypeSeq currentEl=new TypeSeq(t,seq);
assert pos<=MAXSIZE: "Position over maximum size of array";
SeqArr[size]=currentEl;
size++;
}
public void remove(int pos){
if(SeqArr[pos]!=null){
while(SeqArr[pos+1]!=null){
SeqArr[pos]=SeqArr[pos+1];
}
if(SeqArr[pos+1]==null){
SeqArr[pos]=null;
}
}
else
System.out.print("No sequence to remove at specified position");
}
public void print(){
int i=0;
while (SeqArr[i]!=null){
System.out.println(i+"\t"+SeqArr[i].getType()+"\t"+SeqArr[i].getBioSeq().toString());
i++;
}
}
public void print(int pos){
if(SeqArr[pos]==null)
System.out.print("No sequence to print at specified position");
else
System.out.println(SeqArr[pos].getType()+"\t"+SeqArr[pos].getBioSeq().toString());
}
我创建的由数组组成的自定义数据类型
public class TypeSeq {
private Type type;
private MyLinkedList<Character> BioSeq;
public TypeSeq(Type type, MyLinkedList<Character> BioSeq){
this.type=type;
this.BioSeq=BioSeq;
}
public MyLinkedList<Character> getBioSeq() {
return BioSeq;
}
public void setBioSeq(MyLinkedList<Character> bioSeq) {
BioSeq = bioSeq;
}
public Type getType() {
return type;
}
public void setType(Type type) {
this.type = type;
}
}
我的 DNAList 类处理输入并包含主要方法
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class DNAList {
static SequenceArr seqar;
public static void main(String []args){
MyLinkedList<String> hey=new MyLinkedList<>();
hey.append("Hello");
int arraysize= Integer.parseInt(args[0]);
String filePath=args[1];
File file=new File(filePath);
seqar=new SequenceArr(arraysize);
exefromFile(file);
}
public static void exefromFile(File file){
Scanner sc;
try{
sc=new Scanner(file);
while(sc.hasNextLine()){
String cmd=sc.nextLine();
if(!cmd.equals(""))
execute(cmd);
}
}catch (FileNotFoundException e){
e.printStackTrace();
}
}
public static void execute(String s){
s=s.trim();
String [] commands=s.split("\\s+");
switch (commands[0])
{
case "insert":
int pos=Integer.parseInt(commands[1]);
Type t=Type.fromString(commands[2]);
char [] charArr=commands[3].toCharArray();
MyLinkedList<Character> seq=new MyLinkedList<>(charArr.length);
char curChar;
for(int i=0;i<seq.length();i++){
curChar=charArr[i];
if(t==Type.DNA&&(curChar=='A'||curChar=='C'||curChar=='G'||curChar=='T'))
seq.append(charArr[i]);
else
System.out.print("Error occurred while inserting");
}
seqar.insert(pos,t,seq);
break;
case "remove":
pos=Integer.parseInt(commands[1]);
seqar.remove(pos);
break;
case "print":
if(commands.length>1&&commands[1]!=null){
pos=Integer.parseInt(commands[1]);
seqar.print(pos);
}
else
seqar.print();
break;
case "clip":
pos=Integer.parseInt(commands[1]);
int start =Integer.parseInt(commands[2]);
int end =Integer.parseInt(commands[3]);
seqar.clip(pos,start,end);
break;
case "copy":
int pos1=Integer.parseInt(commands[1]);
int pos2=Integer.parseInt(commands[2]);
seqar.copy(pos1,pos2);
break;
case "transcribe":
pos=Integer.parseInt(commands[1]);
seqar.transcribe(pos);
break;
}
}
}
输入的 .txt 文件会说类似
插入 0 个 DNA AATTCCGGAATTCCGG
打印
但输出只是
0 DNA 并且不会打印序列。有什么想法吗?
【问题讨论】:
-
在您的 TypeSeq 类中,您使用的是“私有类型类型;”这个 Type 类属于哪个包?还是定制的?
-
Type 属于 java.lang.reflect,但是我不确定你是如何编译它的,因为它在我的机器上找不到 Type.fromString 并且我没有看到这个Java 类型文档中任何地方引用的方法。
-
另外你还没有放完整的 SequenceArr 类
-
那这段代码不是你写的吗?
-
您的主要兴趣是 1) 学习如何实现链表,或 2) 将 DNA 序列存储在列表中?因为如果 #2,Java 已经提供了多种列表的出色实现,包括链接。只是说..
标签: java arrays linked-list singly-linked-list