Click here to Skip to main content
15,889,867 members
Home / Discussions / Java
   

Java

 
QuestionFIX Day Changed Problem Pin
techGaurav8-Oct-12 21:42
techGaurav8-Oct-12 21:42 
AnswerRe: FIX Day Changed Problem Pin
TorstenH.8-Oct-12 22:00
TorstenH.8-Oct-12 22:00 
AnswerRe: FIX Day Changed Problem Pin
Nagy Vilmos11-Oct-12 20:12
professionalNagy Vilmos11-Oct-12 20:12 
QuestionThreads in details Pin
Neo101016-Oct-12 0:16
Neo101016-Oct-12 0:16 
AnswerRe: Threads in details Pin
Nagy Vilmos8-Oct-12 0:54
professionalNagy Vilmos8-Oct-12 0:54 
Questionjava applet security issues Pin
Ponnampi5-Oct-12 18:01
Ponnampi5-Oct-12 18:01 
AnswerRe: java applet security issues Pin
Ponnampi5-Oct-12 18:12
Ponnampi5-Oct-12 18:12 
QuestionData Structures - ArrayDeque <T> extends AbstractList <T> Pin
Member 94884535-Oct-12 16:29
Member 94884535-Oct-12 16:29 
I'm trying to implement a Data structure in Java that is an ArrayDeque without using modular arithmetic. When testing my code I get an error:

java.lang.ArrayIndexOutOfBoundsException: 16
at a2checker.SimpleTest.test(SimpleTest.java:70)
at checker.AtomicTest$RunnableTest.run(AtomicTest.java:70)
at java.lang.Thread.run(Thread.java:636)

So it would seem that part of my code manipulating arrays a[X] is trying to do something with an index 'X' that is out of bounds. I have been staring at my code and cant see what is wrong with it. Any input would be greatly appreciated.

Here is my code.
---------------------------------

C#
import java.util.AbstractList;

/**
* An implementation of the List interface that allows for fast modifications
* at both the head and tail.
*
* @param <T> the type of objects stored in this list
*/
public class ArrayDeque2<T> extends AbstractList<T> {
    /**
     * The class of elements stored in this queue
     */
    protected Factory<T> f;

    /**
     * Array used to store elements
     */
    public T[] a;

    /**
     * Index of next element to de-queue
     */
    public int j;

    /**
     * Number of elements in the queue
     */
    public int n;

    /**
     * Grow the internal array
     */
    protected void resize() {
        T[] b = f.newArray(Utils.max(2*n,1));
        for (int k = 0; k < n; k++)
            b[k] = a[(j+k >= a.length) ? j+k-a.length : j+k];
        a = b;
        j = 0;
    }

    /**
     * Constructor
     */
    public ArrayDeque2(Class<T> t) {
        f = new Factory<T>(t);
        a = f.newArray(1);
        j = 0;
        n = 0;
    }

    public int size() {
        return n;
    }

    public T get(int i) {
        if (i < 0 || i > n-1) throw new IndexOutOfBoundsException();
        return a[(j+i >= a.length) ? j+i-a.length : j+i];
    }

    public T set(int i, T x) {
        if (i < 0 || i > n-1) throw new IndexOutOfBoundsException();
        T y = a[(j+i >= a.length) ? j+i-a.length : j+i];
        a[(j+i >= a.length) ? j+i-a.length : j+i] = x;
        return y;
    }

    public void add(int i, T x) {
        if (i < 0 || i > n) throw new IndexOutOfBoundsException();
        if (n+1 > a.length) resize();
        if (i < n/2) {
            // shift elements 0,...,i-1 left in a
            j = (j == 0) ? a.length - 1 : j - 1;
            for (int k = 0; k < i-1; k++)
                a[(j+k >= a.length) ? j+k-a.length : j+k] = a[(j+k+1 >= a.length) ? j+k+1-a.length : j+k+1];
        } else {
            // shift elements i,...,n-1 right in a
            for (int k = n; k > i; k--)
                a[(j+k >= a.length) ? j+k-a.length : j+k] = a[(j+k-1 >= a.length) ? j+k-1-a.length : j+k-1];
        }
        a[(j+i >= a.length) ? j+i-a.length : j+i] = x;
        n++;
    }

    public T remove(int i) {
        if (i < 0 || i > n - 1) throw new IndexOutOfBoundsException();

        T x = a[(j+i >= a.length) ? j+i-a.length : j+i];
        if (i < n/2) {
            for (int k = i; k > 0; k--)
                a[(j+k >= a.length) ? j+k-a.length : j+k] = a[(j+k-1 >= a.length) ? j+k-1-a.length : j+k-1];
            j = (j+1 >= a.length) ? j+1-a.length : j+1;
        } else {
            for (int k = i; k < n-1; k++)
                a[(j+k >= a.length) ? j+k-a.length : j+k] = a[(j+k+1 >= a.length) ? j+k+1-a.length : j+k+1];
        }
        n--;
        return x;
    }

    public void clear() {
        a = f.newArray(1);
        n = 0;
        resize();
    }
}

AnswerRe: Data Structures - ArrayDeque extends AbstractList Pin
TorstenH.5-Oct-12 21:36
TorstenH.5-Oct-12 21:36 
QuestionCreating a class that extends more than one class. Pin
Bill.Moo4-Oct-12 2:43
Bill.Moo4-Oct-12 2:43 
AnswerRe: Creating a class that extends more than one class. Pin
Nagy Vilmos4-Oct-12 3:00
professionalNagy Vilmos4-Oct-12 3:00 
GeneralRe: Creating a class that extends more than one class. Pin
Bill.Moo4-Oct-12 3:48
Bill.Moo4-Oct-12 3:48 
AnswerRe: Creating a class that extends more than one class. Pin
Peter_in_27804-Oct-12 3:03
professionalPeter_in_27804-Oct-12 3:03 
GeneralRe: Creating a class that extends more than one class. Pin
Bill.Moo4-Oct-12 3:59
Bill.Moo4-Oct-12 3:59 
GeneralRe: Creating a class that extends more than one class. Pin
Peter_in_27804-Oct-12 11:36
professionalPeter_in_27804-Oct-12 11:36 
AnswerRe: Creating a class that extends more than one class. Pin
pasztorpisti4-Oct-12 12:14
pasztorpisti4-Oct-12 12:14 
QuestionRe: Creating a class that extends more than one class. Pin
Monster Maker21-Oct-12 21:08
Monster Maker21-Oct-12 21:08 
AnswerRe: Creating a class that extends more than one class. Pin
Gowtham Gutha15-Nov-12 6:59
Gowtham Gutha15-Nov-12 6:59 
QuestionThread names Pin
Neo101014-Oct-12 1:19
Neo101014-Oct-12 1:19 
AnswerRe: Thread names Pin
Nagy Vilmos4-Oct-12 2:54
professionalNagy Vilmos4-Oct-12 2:54 
AnswerRe: Thread names Pin
Gowtham Gutha15-Nov-12 7:11
Gowtham Gutha15-Nov-12 7:11 
Questionget ethernet IP address Pin
hari3013-Oct-12 19:30
hari3013-Oct-12 19:30 
AnswerRe: get ethernet IP address Pin
TorstenH.3-Oct-12 19:58
TorstenH.3-Oct-12 19:58 
AnswerRe: get ethernet IP address Pin
jschell4-Oct-12 11:44
jschell4-Oct-12 11:44 
QuestionJava Based Websevice Pin
ashish121693-Oct-12 1:55
ashish121693-Oct-12 1:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.