[英]Java: Getting the char index element of a String that's stored in a Linked List
我是编程的新手,正在从事一个项目,希望能得到一些帮助。 项目规格如下:
通过使用字符的链接列表,将ADT字符串实现为类LinkedString。 包括以下LinkedString构造函数和方法:
LinkedString(char []值)
分配一个新的字符链接列表,以便它表示字符数组参数中当前包含的字符序列。
LinkedString(原始字符串)
初始化一个新的字符链接列表,以便它表示与参数相同的字符序列。
char charAt(int index)
返回指定索引处的字符值。 链接字符串中的第一个字符的位置为零。
LinkedString concat(LinkedString str)
将指定的链接字符串连接到此链接字符串的末尾。
boolean isEmpty()
当且仅当length()为0时返回true。
int length()
返回此链接字符串的长度。
LinkedString子字符串(int beginIndex,int endIndex)
返回一个新的链接字符串,该字符串是此链接字符串的子字符串。
实现LinkedString,使其模仿Java String类。 例如,字符位置应从零开始。 同样,使用一个名为size的变量来跟踪字符串中的字符数。 确定长度时,无需遍历链表和对节点进行计数。 请记住要包括一个Test类,该类创建一个或多个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()方法出现了什么问题,使它无法在请求的索引位置返回char?
另外,为什么当我尝试在main方法开头附近打印对象list1时,我仅获得引用地址而不是值本身?
System.out.print("The original string is " + list1);
System.out.println(chars);
我知道该程序存在很多问题,在此先感谢您为我提供的任何帮助。
解:
在charAt方法中,return语句位于for循环的if条件内。 这意味着在for循环中,有时程序控制将处于“ else”状态。 因此,java感到困惑,认为没有return语句。 如下是一种可能的解决方案,它将解决此问题。
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;
}
完成上述更改后,如果运行代码,则将获得NullPointerException。 原因是LinkedString构造函数存在细微的错误。 修复方法如下-添加行current.next =节点。
公共LinkedString(char []值){
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; current = node; size++; } }
打印“ list1”打印参考而不是实际值。 解决方案是重写toString()方法并提供如下的自定义实现:请注意,如果您在java中打印任何标准对象,它将调用其默认的toString()方法,该方法仅打印引用(大部分情况下)。 通过重写toString方法,我们可以传递希望java打印的字符串。
@Override public String toString(){char value []; 值=新字符[大小]; 节点n =头;
for(int i=0;i<size;i++){ value[i] = n.item; n = n.next; } String str = String.copyValueOf(value); return str;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.