-2

I am trying to implement a ring that remembers the last N elements. It adds element and changes the pointer correctly. The get() method must return the newest element added to the ring. I tried to find the logic in the get method with pen and paper and eventually, I managed to do it. However, when I run my code it does not seem so. Thanks for the help in advance.

[1][2][3][4][5] <- In the following example, get(0) has to return 5 and get(1) - 4

Iteration and print

[1][2][3][4][5]

Using the get method - get(0), get(1) ....

[1] [5] [4] [3] [2] - here [1] must be on the right side of [2]

import java.util.AbstractCollection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class CircularArrayRing<E> extends AbstractCollection<E> implements  Ring<E>
{

    private int elements;
    private int front;
    private E[] ring;

    @SuppressWarnings("unchecked")
    public CircularArrayRing()
    {
        ring = (E[]) new Object[10];
        front = 0;
    }

    @SuppressWarnings("unchecked")
    public CircularArrayRing(int size)
    {
        ring = (E[]) new Object[size];
        front = 0;
    }

    @Override
    public boolean add(E e) 
    {

        ring[front] = e;
        front++;

        if(front == ring.length)
        {
            front = 0;
        }

        if(elements < ring.length)
        {
            elements++;
        }

        return false;
    }

    @Override
    public Iterator<E> iterator() 
    {

        return null;
    }

    @Override
    public int size() 
    {
       return elements;
    }

   @Override
   public E get(int index) throws IndexOutOfBoundsException 
   {
       if(index > elements - 1 || index > ring.length - 1) 
       {    
           throw new IndexOutOfBoundsException();
       }
       else
       {    
            if (index > front)
            {
                return ring[ring.length + front -index];
            }
            else
            {
                return ring[front - index];
            }

       }

    }

}
  • 1
    Debugging didn't help? See: [What is a debugger and how can it help me diagnose problems?](http://stackoverflow.com/q/25385173/5221149) – Andreas Mar 07 '17 at 22:06

3 Answers3

0

So, after looking a bit better at your implementation, I tried rewriting it.
No need to initialize size and effectiveSize, as their default is 0 as class members.
I think this still does what you want.

public class CircularArrayRing<E> extends AbstractCollection<E>
{
   private final E[] ring;
   private int size;
   private int effectiveSize;

   @SuppressWarnings("unchecked")
   public CircularArrayRing() {
      ring = (E[]) new Object[10];
   }

   @SuppressWarnings("unchecked")
   public CircularArrayRing(final int size) {
      ring = (E[]) new Object[size];
   }

   @Override
   public boolean add(final E e) {
      if (effectiveSize < ring.length) {
         effectiveSize++;
      }

      if (size >= ring.length) {
         size = 0;
      }

      ring[size++] = e;
      return true;
   }

   @Override
   public Iterator<E> iterator() {
      return null;
   }

   @Override
   public int size() {
      return effectiveSize;
   }

   public E get(final int index) throws IndexOutOfBoundsException {
      if (index < 0 || index > effectiveSize - 1) {
         throw new IndexOutOfBoundsException();
      }

      return ring[effectiveSize - index - 1];
   }
}
LppEdd
  • 20,274
  • 11
  • 84
  • 139
0

There are some mistakes with the array index handling.

For example if we look at the base case with just 1 element, the logic calling get(0) is:

front - index = 1 - 0 = 1 -> ArrayIndexOutOfBounds

From this we can see that front should be decreased by 1 to reach the correct index.

Further testing will show that the same fix should be applied also to the other branch of the if and that the condition itself should be >= instead of >.

The correct code inside get would then be:

if (index >= front) {
    return ring[ring.length + front - 1 - index];
}
else {
    return ring[front - 1 - index];
}
Loris Securo
  • 7,538
  • 2
  • 17
  • 28
0

Java ring?

I thought you guys meant one of these:

https://www.slideshare.net/abhishekabhi1023/java-ring-new

I'm looking for information on how to program this ;(

Eamorr
  • 9,872
  • 34
  • 125
  • 209