Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 152075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:33:31+00:00 2026-05-11T09:33:31+00:00

I want a simple class that implements a fixed-size circular buffer . It should

  • 0

I want a simple class that implements a fixed-size circular buffer. It should be efficient, easy on the eyes, generically typed.

For now it need not be MT-capable. I can always add a lock later, it won’t be high-concurrency in any case.

Methods should be: .Add() and I guess .List(), where I retrieve all the entries. On second thought, Retrieval I think should be done via an indexer. At any moment I will want to be able to retrieve any element in the buffer by index. But keep in mind that from one moment to the next Element[n] may be different, as the circular buffer fills up and rolls over. This isn’t a stack, it’s a circular buffer.

Regarding ‘overflow‘: I would expect internally there would be an array holding the items, and over time the head and tail of the buffer will rotate around that fixed array. But that should be invisible from the user. There should be no externally-detectable ‘overflow’ event or behavior.

This is not a school assignment – it is most commonly going to be used for a MRU cache or a fixed-size transaction or event log.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T09:33:32+00:00Added an answer on May 11, 2026 at 9:33 am

    I would use an array of T, a head and tail pointer, and add and get methods.

    Like: (Bug hunting is left to the user)

    // Hijack these for simplicity import java.nio.BufferOverflowException; import java.nio.BufferUnderflowException;  public class CircularBuffer<T> {    private T[] buffer;    private int tail;    private int head;    @SuppressWarnings('unchecked')   public CircularBuffer(int n) {     buffer = (T[]) new Object[n];     tail = 0;     head = 0;   }    public void add(T toAdd) {     if (head != (tail - 1)) {         buffer[head++] = toAdd;     } else {         throw new BufferOverflowException();     }     head = head % buffer.length;   }    public T get() {     T t = null;     int adjTail = tail > head ? tail - buffer.length : tail;     if (adjTail < head) {         t = (T) buffer[tail++];         tail = tail % buffer.length;     } else {         throw new BufferUnderflowException();     }     return t;   }    public String toString() {     return 'CircularBuffer(size=' + buffer.length + ', head=' + head + ', tail=' + tail + ')';   }    public static void main(String[] args) {     CircularBuffer<String> b = new CircularBuffer<String>(3);     for (int i = 0; i < 10; i++) {         System.out.println('Start: ' + b);         b.add('One');         System.out.println('One: ' + b);         b.add('Two');         System.out.println('Two: ' + b);         System.out.println('Got '' + b.get() + '', now ' + b);          b.add('Three');         System.out.println('Three: ' + b);         // Test Overflow         // b.add('Four');         // System.out.println('Four: ' + b);          System.out.println('Got '' + b.get() + '', now ' + b);         System.out.println('Got '' + b.get() + '', now ' + b);         // Test Underflow         // System.out.println('Got '' + b.get() + '', now ' + b);          // Back to start, let's shift on one         b.add('Foo');         b.get();     }   } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 226k
  • Answers 226k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Here's the algo, step by step, including finding d. m… May 13, 2026 at 1:07 am
  • Editorial Team
    Editorial Team added an answer Hyperlink myLink = new Hyperlink("foo"); myLink.addClickHandler(new ClickHandler() { @Override public… May 13, 2026 at 1:07 am
  • Editorial Team
    Editorial Team added an answer You can't, really. Java doesn't have any unsigned data types,… May 13, 2026 at 1:07 am

Related Questions

I want to use the new and delete operators for creating and destroying my
I am trying to do a very simple command line library for interactive Java
I have a simple question, but I'm about 80% sure that the answer to
I thought I'd use some (what I thought was) simple generics to enforce CRUD

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.