无法创建追加方法以帮助替换LinkedList中的子串

时间:2016-03-01 06:32:54

标签: java methods linked-list substring nodes

我有方法replace()和append()允许替换方法替换子字符串的字符" LString"与#34; lStr"中的人物在Linked List类LString中。但是我无法在我的替换类中解析我的append方法。

可能是因为我没有为节点构造函数提供LString参数。我不确定如何纠正这个问题。相关方法是我班级的最底层。

public class LString {

    node front;
    node end;
    int length;
    LString next;
    char letter;


    // create node class
    public class node {
        char data;
        node next;

        public node(LString newData) {
            LString data;
        }


        public node(char newData, node newNext) {
            data = newData;
            next = newNext;
        }
    }

    // LString constructor
    // constructs LString object representing empty list of chars
    public LString() {
        this.length = 0;
        this.front = null;
    }

    // Construct LString copy of original parameter
    public LString(String original) {
        //assign first char to front
        node curr = this.front;
        this.length = original.length();
        // loop through
        for (int i = 0; i < original.length(); i++) {
            if (i == 0) {
                front = new node(original.charAt(i), null);
                curr = front;
            } else {
                curr.next = new node(original.charAt(i), null);
                curr = curr.next;
            }
        }
    }

    // return length in this LString
    // can use in LString constructor
    public int length() {
        return this.length;
    }

    //create and return string with contents of LString
    public String toString() {
        // use string builder to meet time limit
        StringBuilder builder = new StringBuilder();
        if (front == null) {
            return "";
        } else {
            node curr = front;
            while (curr != null) {
                builder.append(curr.data);
                curr = curr.next;

            }
            return builder.toString();
        }
    }

    //compares string lists 0 if equal -1 if less, and 1 if greater
    public int compareTo(LString anotherLString) {
        //save lowest length of strings
        int minLength;
        // get front spots
        node curr = this.front;
        node otherCurr = anotherLString.front;
        // get lengths
        int thisString = length();
        int otherString = anotherLString.length();
        // get shortest length of 2 strings
        if (thisString < otherString) {
            minLength = thisString;
        } else {
            minLength = otherString;
        }
        //go through characters in each string and compare for lexicographic order
        int iterate = 0;
        while (iterate < minLength) {
            char string1Char = curr.data;
            char string2Char = otherCurr.data;
            if (string1Char != string2Char) {
                return string1Char - string2Char;
            }
            iterate++;
            curr = curr.next;
            otherCurr = otherCurr.next;
        }
        return thisString - otherString;
    }

    //Return true if LString represents the same list of characters as other
    @Override
    public boolean equals(Object other) {
        if (other == null || !(other instanceof LString))
            return false;
        else {
            // use compareTo to determine if strings are the same
            LString otherLString = (LString) other;
            if (compareTo(otherLString) == 0) {
                return true;
            } else {
                return false;
            }
        }
    }

    //Return the char at the given index in this LString.
    public char charAt(int index) {
        int length = this.length();
        // check for index out of bounds
        if (index < 0 || index >= length) {
            throw new IndexOutOfBoundsException();
        }
        // returns char at index
        node curr = front;
        for (int i = 0; i < index; i++) {
            curr = curr.next;
        }
        return curr.data;
    }

    //Set the char at the given index in this LString to ch.
    public void setCharAt(int index, char ch) {
        // check for index out of bounds
        int length = this.length();
        if (index < 0 || index >= length) {
            throw new IndexOutOfBoundsException();
        }
        // replaces char at index
        node curr = front;
        for (int i = 0; i < index; i++) {
            curr = curr.next;
        }
        curr.next = new node(curr.data, curr.next);
        curr.data = ch;
    }

    //Returns a new LString that is a sub-string of this LString.
    public LString substring(int start, int end) {
        LString newLString = new LString();
        // handle exceptions
        if (start < 0 || start > end) {
            throw new IndexOutOfBoundsException();
        } else if (end > this.length()) {
            throw new IndexOutOfBoundsException();
            //return null in special case (empty LString)
        } else if (start == end) {
            //&& end == this.length()
            return newLString;
        } else {
            node node = this.front;
            for (int i = 0; i < start; i++) {
                node = node.next;
                // insert substring
            }
            node copy = new node(node.data);
            newLString.front = copy;
            for (int i = start + 1; i < end; i++) {
                node = node.next;
                copy = copy.next = new node(node.data);
            }
            return newLString;
        }
    }

    // Replaces this characters in a sub-string of this LString with the characters in lStr.
    public LString replace(int start, int end, LString lStr) {

        // handle exceptions
        if (start < 0 || start > end) {
            throw new IndexOutOfBoundsException();
        } else if (end > this.length()) {
            throw new IndexOutOfBoundsException();
        }
        LString replacedLs = new LString();
        replacedLs.append(substring(0, start));
        replacedLs.append(lStr);
        replacedLs.append(substring(end, this.length));
        return replacedLs;
    }


    //append helper method
    public void append(LString data) {
        this.length++;
        if (front == null) {
            front = new node(data);
            return;
        }
        node curr = front;
        while (curr.next != null) {
            curr = curr.next;
        }
        curr.next = new node(data);
    }
}

感谢您的帮助,谢谢。

1 个答案:

答案 0 :(得分:0)

看看你的node构造函数,它什么都不做:

public node(LString newData) {
    LString data;
}

要使其工作,您应该将所有LString.front字段分配给当前对象,所以:

public node(LString newData) {
    this.data = newData.front.data;
    this.next = newData.front.next;
}