簡體   English   中英

使用構造函數反轉字符串的字符順序並放入 LinkedList

[英]Reverse String order of characters and place into LinkedList using a constructor

我正在制作一個程序,它接受一個字符串並將其以相反的順序放入一個 LinkedList。

這段代碼似乎不起作用(輸入錯誤),我不知道為什么。 任何解決方案?

public LargeInteger(String input) 
{
     TODO
    size=size+input.length();
    LLNode<Integer> curNode=new LLNode<Integer>();
    for(int curPos=input.length()-1;curPos>=0;curPos--)
    {
        if(curPos==input.length()-1)
        {
            head=new LLNode<Integer>();
            head.data=input.charAt(curPos)+'0';
            curNode=head;
        }
        else
        {
            curNode.link=new LLNode<Integer>();
            curNode=curNode.link;
            curNode.data=input.charAt(curPos)+'0';
        }
    }
}

歡迎使用堆棧溢出。

如何使用 List 接口和 String 類的方法,而不僅僅是 for 循環? 檢查這個例子:

String phrase = "this is the phrase"; // this is the input
List<String> list = new LinkedList<>();

// iterates the input in decreasing index order
for(int i=phrase.length()-1; i >= 0; i--) { 
    // gets the character from the input and add to the LinkedList
    list.add(Character.toString(phrase.charAt(i))); 
}

如果不想加空格,加if(isEmpty(phrase.charAt(i))) continue; 在添加字符之前。

現場示例在這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM