【问题标题】:toString method to remove too many whitespaces [closed]删除太多空格的 toString 方法[关闭]
【发布时间】:2016-03-20 02:43:58
【问题描述】:

我有一个带有 2 个堆栈(leftright)的文本编辑器缓冲区硬件分配。在大多数情况下,一切都按预期的方式工作。但我遇到的麻烦是它返回了太多的空白。我专门尝试填写toString() 方法以返回文本。 例如返回文本打印:

 T h e r e i s g r a n d e u r i n t h i s v i e w o f l i f e ,

字母之间有一个空格,每个单词之间有两个空格。如何删除字母之间的空格,同时只删除单词之间的 1 个空格,以便我的字符串返回:

There is grandeur in this view of life,

public class Buffer {
    private Stack<Character> left;  // chars left of cursor
    private Stack<Character> right; // chars right of cursor

    // Create an empty buffer.
    public Buffer() {
        left = new Stack<Character>();
        right = new Stack<Character>();
    }

    // Insert c at the cursor position.
    public void insert(char c) {
        left.push(c);
    }

    // Delete and return the character at the cursor.
    public char delete() {
        if (!right.isEmpty()){
            return right.pop();
        }else return 0;
    }

    // Move the cursor k positions to the left.
    public void left(int k) {
        while (!left.isEmpty() && --k >= 0){
            right.push(left.pop());
        }
    }

    // Move the cursor k positions to the right.
    public void right(int k) {
        while (!right.isEmpty() && --k >=0){
            left.push(right.pop());
        }
    }

    // Return the number of characters in the buffer.
    public int size() {
        return left.size()+right.size();
    }

    // Return a string representation of the buffer with a "|" character (not 
    // part of the buffer) at the cursor position.
    public String toString() {

        String a = (left+"|"+right);


        return a;
    }

    // Test client (DO NOT EDIT).
    public static void main(String[] args) {
        Buffer buf = new Buffer();
        String s = "There is grandeur in this view of life, with its " 
            + "several powers, having been originally breathed into a few " 
            + "forms or into one; and that, whilst this planet has gone " 
            + "cycling on according to the fixed law of gravity, from so " 
            + "simple a beginning endless forms most beautiful and most " 
            + "wonderful have been, and are being, evolved. ~ " 
            + "Charles Darwin, The Origin of Species";
        for (int i = 0; i < s.length(); i++) {
            buf.insert(s.charAt(i));
        }
        buf.left(buf.size());
        buf.right(97);
        s = "by the Creator ";
        for (int i = 0; i < s.length(); i++) {
            buf.insert(s.charAt(i));
        }
        buf.right(228);
        buf.delete();
        buf.insert('-');
        buf.insert('-');
        buf.left(342);
        StdOut.println(buf);
    }
}

【问题讨论】:

  • 您应该尽最大努力按照本网站的规则解决您的问题。另请查看How do I ask and answer Homework questions。无论问题是针对家庭作业还是家庭作业(自学),此信息均有效。
  • toString() 方法的代码是什么?
  • 提示:识别是两个空格还是一个空格。 java.lang.String.toCharArray() (注意:有更好的方法,例如正则表达式!但对于初学者来说可能太多了)。然后用 1 个空格替换 2 个空格,用空字符串替换 1 个空格 ""
  • @PinTacular 好吧,在这种情况下,要么 github 上的代码不是最新版本,要么 StdOut-class 中有一些奇怪的错误。顺便说一句,那个班级不见了……

标签: java stack tostring


【解决方案1】:

您的代码依赖于StacktoString() 方法。除非格式定义明确,否则您不应该这样做,并且对于 java.util.Stack 没有明确定义,尽管它的输出方式与其他集合类相同,即 [value1, value2, value3]

有关输出示例,请参阅 this IDEONE(一旦我将 StdOut 替换为 System.out):

[]|[s, e, i, c, e, p, S,  , f, o,  , n, i, g, i, r, O,  , e, h, T,  , ,, n, i, w, r, a, D,  , s, e, l, r, a, h, C,  , -, -,  , ., d, e, v, l, o, v, e,  , ,, g, n, i, e, b,  , e, r, a,  , d, n, a,  , ,, n, e, e, b,  , e, v, a, h,  , l, u, f, r, e, d, n, o, w,  , t, s, o, m,  , d, n, a,  , l, u, f, i, t, u, a, e, b,  , t, s, o, m,  , s, m, r, o, f,  , s, s, e, l, d, n, e,  , g, n, i, n, n, i, g, e, b,  , a,  , e, l, p, m, i, s,  , o, s,  , m, o, r, f,  , ,, y, t, i, v, a, r, g,  , f, o,  , w, a, l,  , d, e, x, i, f,  , e, h, t,  , o, t,  , g, n, i, d, r, o, c, c, a,  , n, o,  , g, n, i, l, c, y, c,  , e, n, o, g,  , s, a, h,  , t, e, n, a, l, p,  , s, i, h, t,  , t, s, l, i, h, w,  , ,, t, a, h, t,  , d, n, a,  , ;, e, n, o,  , o, t, n, i,  , r, o,  , s, m, r, o, f,  , w, e, f,  , a,  , o, t, n, i,  , r, o, t, a, e, r, C,  , e, h, t,  , y, b,  , d, e, h, t, a, e, r, b,  , y, l, l, a, n, i, g, i, r, o,  , n, e, e, b,  , g, n, i, v, a, h,  , ,, s, r, e, w, o, p,  , l, a, r, e, v, e, s,  , s, t, i,  , h, t, i, w,  , ,, e, f, i, l,  , f, o,  , w, e, i, v,  , s, i, h, t,  , n, i,  , r, u, e, d, n, a, r, g,  , s, i,  , e, r, e, h, T]

正如人们一直告诉你的那样,整个字符串是颠倒的,有括号 ([]) 和逗号分隔符 (,)。

如果您没有看到,那么您可能没有使用 java.util.Stack,而是使用了一些本地实现。

无论如何,解决方案是修复您的toString 实现,使其不依赖StacktoString(),因为即使它不输出空格,如果“光标”出现,您仍然会得到不好的结果在你的文字中间。

如果你使用java.util.Stack,你应该创建一个StringBuilder(),然后迭代left并附加每个字符,附加|,然后使用right.listIterator(right.size())迭代right并使用@987654342向后迭代@ 和 previous() 追加字符。

【讨论】:

  • 你是对的,它没有使用标准的 java.util.stack。但是普林斯顿版本的堆栈(如果有区别的话)。我得到的输出看起来像imgur.com/A2lopuW 我必须填写 toString 方法,因为这是教授要求我们的。
  • “我必须填写 toString 方法” 是的,我也是这么说的。您必须将 your toString() 方法更改为有效的方法,这意味着不要使用 Stack.toString() 方法,因为您现在正在使用表达式 left+"|"+right 隐式执行。
  • 啊,好的,我明白了。你有没有可以指出我如何实现这一目标的方向?同样,不是在寻找答案,而是寻找从这里出发的方向。
  • @PinTacular 是的,我可以指导你。阅读我回答的最后一段。
  • 所以我会遍历堆栈并将其放入一个字符串(newString)中。 new for 循环遍历新字符串{ if newString.contains " " (
【解决方案2】:

要么修复你的算法(首选),要么这是一个简单的“快速而肮脏”的方法:

String fixed = str.replaceAll("(?<! ) ", "");

匹配正则表达式的意思是“前面没有空格字符的空格字符”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-15
    • 2015-02-12
    相关资源
    最近更新 更多