java中固定长度的StringBuffer

发布于 2024-12-05 07:53:17 字数 171 浏览 1 评论 0原文

在java中保持固定字符串缓冲区长度的最佳实践是什么?也就是说,如果固定值为10并且stringbuffer保存ABCDEFGHIJ,当我们附加K时,将导致A被清除,结果值将是BCDEFGHIJK。我正在考虑使用StringBuffer的reverse()和setLenght()方法组合,但不知道100 K 长度时其性能如何?

what is the best practise to hold a stringbuffer length fixed in java ? That is if the fixed value is 10 and stringbuffer holds ABCDEFGHIJ, when we append K that will cause A to be cleared and resulting value will be BCDEFGHIJK.I am thinking to use StringBuffer's reverse() and and setLenght() method combination but dont know how will its performance be for 100 K length.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

可是我不能没有你 2024-12-12 07:53:17

听起来您正在寻找一个循环缓冲区。您可以创建一个 char[] 并维护大小和逻辑开始。然后,当您需要将其转换为字符串时,只需创建两个字符串(一个从缓冲区末尾开始,一个从开头开始)并将它们连接在一起。不过,这将相对昂贵 - 尝试尽可能长时间地将其保留为循环缓冲区。

确保在每个操作中您还考虑缓冲区已满的可能性。操作示例:

public void append(char c)
{
    buffer[(size + start) % maxLength] = c;
    if (size == maxLength)
    {
        start = (start + 1) % maxLength;
    }
    else
    {
        size++;
    }
}

It sounds like you're after a circular buffer. You could create a char[] and maintain a size as well as the logical start. Then when you need to convert it into a string, you can just create two strings (one from the end of the buffer and one from the start) and concatenate them together. That will be relatively expensive though - try to keep it just as the circular buffer for as much of the time as you can.

Make sure that on each operation you also consider the possibility of the buffer not being full though. Sample operation:

public void append(char c)
{
    buffer[(size + start) % maxLength] = c;
    if (size == maxLength)
    {
        start = (start + 1) % maxLength;
    }
    else
    {
        size++;
    }
}
献世佛 2024-12-12 07:53:17

您可以使用 删除

void append(String s) {
    buffer.append(s);
    if(buffer.length() > MAX_LENGTH){
        buffer.delete(0, buffer.length() - MAX_LENGTH);
    }
}

更新:如果参数是长字符串,则会导致不必要的 StringBuffer 分配。为了避免这种情况,您可以首先缩短缓冲区,然后仅根据需要附加字符串中的字符:

void append(String s) {
    if (buffer.length() + s.length() > MAX_LENGTH) {
        buffer.delete(0, buffer.length() + s.length() - MAX_LENGTH);
    }
    buffer.append(s, Math.max(0, s.length() - MAX_LENGTH), s.length());
}

You could use delete:

void append(String s) {
    buffer.append(s);
    if(buffer.length() > MAX_LENGTH){
        buffer.delete(0, buffer.length() - MAX_LENGTH);
    }
}

Update: If the parameter is a long string this results in unnecessary StringBuffer allocations. To avoid this, you could shorten the buffer first, and then append only as much characters of the string as needed:

void append(String s) {
    if (buffer.length() + s.length() > MAX_LENGTH) {
        buffer.delete(0, buffer.length() + s.length() - MAX_LENGTH);
    }
    buffer.append(s, Math.max(0, s.length() - MAX_LENGTH), s.length());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文