java - ArrayIndexOutOfBoundsException when trying to implement circular queue using arrays -


visualization of queue

above diagram assignment page

for homework assignment have implement circular queue using arrays. have conditions working except case in strings "i", "j", "k", "l" appended. result of action, of new values in array supposed loop around beginning. however, arrayindexoutofboundsexception when appending 2nd letter.

the debugger further traces issue tostring method, can't understand problem is.

any advice?

public class circulararrayqueueapp {     public static void main(string[] args) {          circulararrayqueue queue = new circulararrayqueue(10);          string[] data1 = {"a", "b", "c", "d", "e", "f", "g", "h"};         string[] data2 = {"i", "j", "k", "l"};          (string adata1 : data1) queue.enqueue(adata1);          system.out.println(queue.first());         system.out.println(queue);          (int = 0; < 4; i++) {             int index = queue.getread();             system.out.println("\"" + queue.dequeue() + "\" has been dequeued @ index " + index);         }          system.out.println(queue.first());         system.out.println(queue);          (int = 0; < 4; i++) {             queue.enqueue(data2[i]);         }          system.out.println(queue.first());         system.out.println(queue);          }      static class circulararrayqueue {         private final int length;         private int read;         private int write;         private final object[] data;          public circulararrayqueue(int size) {             data = new object[size];             length = data.length;             read = 0;             write = 0;         }          object read() {             object o;             o = data[read];             read = (read + 1) % length;              return o;         }          object first() {             if (isempty()) {                 system.out.println("**empty queue**");                 return null;             }             return data[read];         }          void write(object o) {             data[write] = o;             write = (write + 1) % length;         }          boolean enqueue(object o) {             if (isfull()) {                 system.out.println("**full queue**");                 return false;             }             write(o);             return true;         }          public object[] getdata() {             return data;         }          object dequeue() {             if (isempty()) {                 system.out.println("**empty queue**");                 return null;             }             return read();         }          boolean isfull() {             return read == (write + 1) % length;         }          boolean isempty() {             return read == length;         }          public string tostring() {             string output = "[";              (int = read; ((i) % length) != write; i++)                 output += data[i] + " ";              return output.substring(0, output.length() - 1) + "]";         }          public int getread() {             return read;         }          public int getsize() {             return length;         }          public int getwrite() {             return write;         }     } } 

you aren't modding value of you're array lookup. try using string joiner simplify things

stringjoiner sj = new stringjoiner(" ", "[", "]"); (int = read; % length != write; i++) {     sj.add(data[i % length].tostring()); } return sj.tostring(); 

or alternatively mod @ same time increment

for (int = read; != write; = (i + 1) % length) {     sj.add(data[i].tostring()); } 

Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -