将具有各种数据成员的对象添加到链接列表

时间:2014-04-04 00:31:49

标签: java object linked-list project

我的代码在这里遇到了一些麻烦。我试图建立一个患者对象的链接列表,每个患者都有各自的数据成员,包括姓名,身份,身高等。所有患者数据都在我的文本文件中。我有我已经放在一起的Linked List类。我在这里的问题是,当我将每个患者添加到我的链接列表(LLPatient),然后我去显示列表。该列表要么读回null,要么读回空白。在我做了一些调整之前,我得到了最后添加的患者姓名。我无法弄清楚我的生活有什么不对。我搜索和搜索,似乎无法得到答案。

最终我需要为我的项目做些什么才能带走患者并显示/添加/获得生日/比较。但是如果我不能在列表中找到它们并且能够在列表中使用它们,我就不能使用任何其他方法。我需要在星期一之前完成这项工作,所以我非常渴望得到答案。

聚苯乙烯。我还有一个add方法,用于检查ID是否已被使用(我的arraylist ID),如果没有将新的患者详细信息写入txt文件。这就像一个魅力。

public class LLPatient{

protected Node head;
private Node tail;
private int size;
public String info;
public LLPatient(){
    head = tail = null;

    size = 0;

}
public int size(){
    return size;
}

public Node nodeAt(int index){
    if (index < 0 || index >= size)
    throw new IndexOutOfBoundsException();

    Node n = head;
    for (int i =0; i < index; i++, n = n.next);
    return n;
}

public Object get(int index){
    Node n = nodeAt(index);
    return n.element;

}
public Object set(int index, Object elem){
    Node n = nodeAt(index);
    Object prevElem = n.element;
    n.element = elem;
    return prevElem;
}
public void insert(int index, Object elem){

    if(index < 0 || index > size)
        throw new IndexOutOfBoundsException();
    Node newNode = new Node(elem, null);
    if(index == 0){
        newNode.next = head;
        head = newNode;
    }
    else{
        Node pred = nodeAt(index -1);
        newNode.next = pred.next;
        pred.next = newNode;

    }
    size++;
}
public void remove(int index){
    if(index<0 || index >= size)
        throw new IndexOutOfBoundsException();

        Node removedNode = null;

        if(index == 0){
            removedNode = head;
            head = head.next;
        }else{
            Node pred = nodeAt(index-1);
            removedNode = pred.next;
            pred.next = removedNode.next;

        }

        size--;
    }

public String toString(){
    if(head == null){
        return "";
    }
    Node n = head;
    Object result = n.element;
    n= n.next;

    while(n != null){
        result += " | " + n.element;
        n= n.next;
    }
    return result.toString();
}
public void print(){
    System.out.println(this);
}
public void add(Node elem){
    if(head == null){
        head = elem;
    }
    else{
        tail.next = elem;
    }
    tail = elem;
    size++;

}

public class Node {
    public Object element;
    public Node next;

    public Node(Object e, Node n){
        element = e;
        next = n;
    }
    public void setNodeInfo(String s){

        info = s;
    }
    public String getNodeInfo(){
        return info;
    }


}
}

和我的耐心课为我的对象     公共类患者延伸LLPatient {

public String info;
public String name;
public String id;
public String address;
public int height;
public double weight;
public Date dob = new Date();
public Date initialV = new Date();
public Date lastV = new Date();
public static ArrayList<String> IDs = new ArrayList<String>();

LLPatient Patients = new LLPatient();
static SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");

public Patient(){

}
public Patient(String name){
    setName(name);
}
public Patient(String name, String id){
    setName(name);
    setID(id);
}
public Patient(String name, String id, String address, int height,
        double weight, Date dob, Date initialV, Date lastV
        ){

        setName(name);
        setID(id);
        setAddress(address);
        setHeight(height);
        setWeight(weight);
        setDob(dob);
        setInitialV(initialV);
        setLastV(lastV);
}

public void setInfo(String name, String id){
    info = name + ", " + id;
}
public String getInfo(){
    return info;
}
public void setName(String tempName){
    name = tempName;
}
public void setID(String tempID){
    id = tempID;
}
public void setAddress(String tempAddress){
    address = tempAddress ;
}
public void setHeight(int tempHeight){
     height = tempHeight;
}
public void setWeight(double tempWeight){
     weight = tempWeight;
}
public void setDob(Date tempDob){
    dob = tempDob ;
}
public void setInitialV(Date tempInitialV){
    initialV = tempInitialV;
}
public void setLastV(Date tempLastV){
    lastV = tempLastV;
}
public Date getDob(){
    return dob;
}

public void ReadData(String fileName) throws IOException, NumberFormatException, ParseException{
    Node n = null;
    File theFile = new File(fileName);
    @SuppressWarnings("resource")
    Scanner input = new Scanner(theFile);
    while(input.hasNext() ){         
         Patient record = new Patient();
         record.setName(input.nextLine());
         record.setID(input.nextLine());
         record.setAddress(input.nextLine());
         record.setHeight(Integer.parseInt(input.nextLine()));
         record.setWeight(Double.parseDouble(input.nextLine()));
         record.setDob(dateFormat.parse(input.nextLine()));
         record.setInitialV(dateFormat.parse(input.nextLine()));
         record.setLastV(dateFormat.parse(input.nextLine()));


         Patients.insert(Patients.size(), record);

         IDs.add(record.id);
    }
    Patients.print();
    System.out.println(Patients.size());
    System.out.println(Patients.toString());

此外,如果我要做

之类的事情
  Patients.insert(Patients.size(), record.name);

它会相应地打印出这些名字。

0 个答案:

没有答案