首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Java:获取存储在链表中的字符串的char index元素

Java:获取存储在链表中的字符串的char index元素
EN

Stack Overflow用户
提问于 2018-09-25 08:18:51
回答 1查看 2.5K关注 0票数 0

我是编程新手,正在做一个我希望能得到一些帮助的项目。项目规格如下:

通过使用字符的链接列表将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类提供了以下内容:

代码语言:javascript
复制
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;
    }
}

这是我的节点类:

代码语言:javascript
复制
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类:

代码语言:javascript
复制
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));

    }

}

这是我在运行测试类时得到的输出:

代码语言:javascript
复制
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时,我只得到了引用地址,而没有得到值本身?

代码语言:javascript
复制
    System.out.print("The original string is " + list1);
    System.out.println(chars);

我知道我在这个程序上有很多问题,我提前感谢你能给我的任何帮助。

EN

回答 1

Stack Overflow用户

发布于 2018-09-25 09:05:41

解决方案:

  1. 问题-缺少返回语句。

在charAt方法中,return语句位于for循环的if条件内。这意味着在for循环中,有时程序控制将处于"else“状态。所以,java认为没有返回语句是很混乱的。一种可能的解决方案是如下所示,这将解决该问题。

代码语言:javascript
复制
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;

}

  1. 在上述更改之后,如果您运行代码,您将得到NullPointerException。原因是LinkedString构造函数中有一个细微的bug。修复方法如下-添加行current.next = node。

公共值(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++;}}

  • 打印"list1“会打印引用,而不是实际值。解决方案是覆盖toString()方法并提供一个自定义实现,如下所示:请注意,如果您在java中打印任何标准对象,它将调用其默认的toString()方法,该方法只打印一个引用(主要)。通过重写toString方法,我们可以传递我们希望java打印的字符串。

@Override公有字符串toString() { char value[];value =新字符大小;Node n=头;

for(int值){i=0;i

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52488936

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档