我有一个链接列表问题源于我即将发布的这个作业提示。它可能有助于答案:
具体细节
Java String类是不可变的(您无法更改内容)。这有时会妨碍您对字符串的处理(即对现有字符串进行更改)。因此,对于此赋值,您将创建一个名为MutableString的类。注意/免责声明:Java API确实有一个StringBuffer类,它是可变的,但是对于这个赋值的意图和目的,我们假装我们不知道它;-)
An object of the MutableString class contains the following operations/behaviors -- which means the class itself must contain the following methods:
Character charAt(int index): returns the Character at the specified index in the string -- if the index lies outside the string, throw a MutableStringIndexOutOfBoundsException (you must write this class)
void set(int index, Character ch): replaces the existing Character at the specified location -- if the index lies outside the string throw a MutableStringIndexOutOfBoundsException
void add(int index, Character ch): creates a new spot in the list for the Character at the index specified -- if the index lies outside the string throw a MutableStringIndexOutOfBoundsException
Character remove(int index): removes the Character at specified index -- if the index lies outside the string, throw a MutableStringIndexOutOfBoundsException
boolean remove(Character ch): removes the first occurrence (starting from the beginning of the MutableString) of the Character -- if the Character is not found, return false
boolean removeAll(Character ch): removes all occurrences of the specified Character -- if Character is not found, return false
void toUpper(): converts the current string to all upper case
void toLower(): converts the current string to all lower case
MutableString substring(int start, int finish): returns a string starting at the Character specified by start and concluding with the Character specified by finish -- if start or finish is outside the the string, throw a MutableStringIndexOutOfBoundsException
char [] toCharArray(): returns a char array containing all the characters in the string -- be sure and handle all cases
int length(): reports/returns the length of the string
String toString(): returns a String containing all the Characters
String toReverseString(): returns a String containing all the Characters in reverse order -- you must utilize recursion to accomplish this task
int compareTo(MutableString that): allows comparison of two MutableString objects -- this implies you will implement the Comparable interface for your MutableString class
void sort(): alphabetizes the letters in case-insensitive fashion in ascending order -- you must write the code for this sort (no API calls are allowed)
您必须使用链接列表来表示字符串的字符(您必须编写 LinkeList类)。您的LinkedList类应包含更新和修改列表的基本操作。考虑到这一点,在Java API中实现List接口。对于您认为对LinkedList的功能不必要的方法,将这些方法存根并抛出一个Exception,其中包含有关调用的方法以及尚未实现的方法的信息。注意:确保您的LinkedList类不执行任何MutableString特定的操作。您可以随意使用以下存根的LinkedList类进行分配。它可能不包含您需要的所有方法,但它确实包含来自Java API的List接口中的方法。
MutableString必须尽可能利用LinkedList行为(方法)(不要在MutableString中编写与LinkedList类中的内容相同的代码)。因此,您的MutableString将包含一个对LinkedList对象的引用的字段。顺便说一下,这个概念被称为委托,这是软件工程和设计中非常重要的一个概念。不要设计MutableString,以便它继承LinkedList。
MutableString的每个节点都应包含一个字符 - 请注意,这并不意味着对节点类中数据的引用应该/必须是类型字符 - 这将使您的LinkedList类特定于MutableString,我们不要在这种情况下,我不希望这样。
**我不理解的部分是上面的第2段,它讨论了从链表类继承。我总是习惯写像公共类MutableString扩展LinkedList,但我们不能这样做,我真的不知道如何开始编写这个mutablestring类而不继承类。帮助解释如何做到这一点,差异将是非常棒的。感谢。
我不是很擅长链接列表这么问题:) **