我是编程新手,正在做一个我希望能得到一些帮助的项目。项目规格如下:
通过使用字符的链接列表将ADT字符串实现为类LinkedString。包括以下LinkedString构造函数和方法:
LinkedString(char[]值)
分配一个新的字符链表,以便它表示当前包含在字符数组参数中的字符序列。
LinkedString(String original)
初始化新的字符链表,使其表示与参数相同的字符序列。
char charAt(整型索引)
返回指定索引处的字符值。链接字符串中的第一个字符位于零位置。
LinkedString合并(LinkedString字符串)
将指定的链接字符串连接到此链接字符串的末尾。
布尔型isEmpty()
当且仅当length()为0时返回true。
int length()
返回此链接字符串的长度。
LinkedString子串( int beginIndex,int endIndex)
返回一个新的链接字符串,它是此链接字符串的子字符串。
实现LinkedString,使其模仿Java String类。例如,字符位置应从零开始。此外,使用名为size的变量跟踪字符串中的字符数;长度应该在不遍历链表和计算节点的情况下确定。记住要包含一个测试类,该类创建一个或多个LinkedString对象并调用LinkedString ADT中的每个方法。
因此我有三个类: LinkedString类、Node类和一个运行main方法的LinkedStringTest类。到目前为止,我为LinkedString类提供了以下内容:
public class LinkedString {
private int size; //var keeps track of number of characters
private Node head;
public LinkedString(){ //no argument constructor
head = null;
size = 0;
}
public LinkedString(char[] value){
if(value.length == 0)
return;
Node node = new Node(value[0]);
head = node;
size++;
Node current = head;
for(int nodeIndex = 1; nodeIndex < value.length; nodeIndex++){
node = new Node (value[nodeIndex]);
current.next = node;
size++;
}
}
public LinkedString(String original){
if(original.length() == 0)
return;
Node node = new Node(original.charAt(0));
head = node;
size++;
Node current = head;
for(int nodeIndex = 1; nodeIndex < original.length(); nodeIndex++){
node = new Node(original.charAt(nodeIndex));
current.next = node;
current = current.next;
size++;
}
}
public char charAt(int index){
Node current = head;
for(int nodeIndex = 0; nodeIndex < size; nodeIndex++){
if(nodeIndex == index){
return current.item;}
else{
current = current.next;
}
}
}
public LinkedString concat(LinkedString str){
if(str.head == null){
return this;
}
else if(head == null){
size = str.length();
return str;
}
else{
Node current = head;
while(current.next != null)
current = current.next;
current.next = str.head;
size += str.length();
return this;
}
}
public boolean isEmpty(){
return length() == 0;
}
public int length(){
return size;
}
public LinkedString substring(int beginIndex, int endIndex){
String substr = " ";
for(int nodeIndex = beginIndex; nodeIndex <= endIndex; nodeIndex++)
substr += charAt(nodeIndex);
LinkedString linkedSubstring = new LinkedString(substr);
return linkedSubstring;
}
}
这是我的节点类:
public class Node {
char item;
Node next;
public Node() {
setItem(' ');
setNext(null);
}
public Node(char newItem) {
setItem(newItem);
setNext(null);
}
public Node(char newItem, Node newNext) {
setItem(newItem);
setNext(newNext);
}
public Node(Node newNext) {
setItem(' ');
setNext(newNext);
}
public void setItem(char newItem) {
item = newItem;
}
public void setNext(Node newNext) {
next = newNext;
}
public char getItem() {
return item;
}
public Node getNext() {
return next;
}
}
这是我的LinkedStringTest类:
import java.util.Scanner;
public class LinkedStringTest {
public static void main (String args[]){
Scanner sc = new Scanner(System.in);
char[] chars = {'H', 'e', 'l', 'l', 'o'};
LinkedString list1 = new LinkedString(chars);
System.out.print("The original string is ");
System.out.println(chars);
System.out.println("Is the list empty? " + list1.isEmpty());
System.out.println("The characters length: " + list1.length());
System.out.println("Enter the position of a character and press Enter: ");
int pos1 = sc.nextInt();
System.out.println("The character at position " + pos1 + " is " + list1.charAt(pos1));
System.out.println("Enter a string: ");
String strng1 = sc.next();
LinkedString list2 = new LinkedString(strng1);
System.out.println("The string is " + list2);
System.out.println("That string concatanated with the original string is " + list1.concat(list2));
System.out.println("Enter the starting and ending index of part of a string ");
int start = sc.nextInt();
int end = sc.nextInt();
System.out.println("The substring from " + start + " to " + end + " is " + list1.substring(start,end));
}
}
这是我在运行测试类时得到的输出:
run:
The original string is project2.LinkedString@55f96302
Hello
Is the list empty? false
The characters length: 5
Enter the position of a character and press Enter:
2
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - missing return statement
at project2.LinkedString.charAt(LinkedString.java:64)
at project2.LinkedStringTest.main(LinkedStringTest.java:28)
C:\Users\Im\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 47 seconds)
正如您所看到的,当我输入一个索引位置(在本例中是数字2)时,我会收到错误消息。第一个错误(第64行)位于我的charAt方法的开头。第二个错误(第28行)在main方法中,我尝试将整数(在本例中为2)发送给charAt方法。
我的charAt()方法出了什么问题,使得它不能在请求的索引位置返回字符?
另外,为什么当我试图打印出main方法开头的对象list1时,我只得到了引用地址,而没有得到值本身?
System.out.print("The original string is " + list1);
System.out.println(chars);
我知道我在这个程序上有很多问题,我提前感谢你能给我的任何帮助。
发布于 2018-09-25 09:05:41
解决方案:
在charAt方法中,return语句位于for循环的if条件内。这意味着在for循环中,有时程序控制将处于"else“状态。所以,java认为没有返回语句是很混乱的。一种可能的解决方案是如下所示,这将解决该问题。
public char charAt(int index){
Node current = head;
for(int nodeIndex = 0; nodeIndex < size; nodeIndex++){
if(nodeIndex == index) {
break;
}
else{
current = current.next;
}
}
return current.item;
}
公共值(char[]LinkedString){
if(value.length == 0) return;Node Node =新节点(值);head =节点;size++;node current = head;for(int nodeIndex = 1;nodeIndex < value.length;nodeIndex++){ node =新节点(valuenodeIndex);current.next = node;current = node;size++;}}
@Override公有字符串toString() { char value[];value =新字符大小;Node n=头;
for(int值){i=0;i
}
https://stackoverflow.com/questions/52488936
复制相似问题