-1

I'm Java beginner, I have taken piece of code from online and trying the program on permutation. Im getting error as

Exception in thread "main"java.lang.ArrayIndexOutOfBoundsException: -1.

Can anybody help to resolve this problem. Thank you.

// Permute.java -- A class generating all permutations

  import java.lang.reflect.Array;
import java.util.Iterator;
import java.util.NoSuchElementException;

   public class Permute implements Iterator {

      private final int size;
     private final Object [] elements;  // copy of original 0 .. size-1
     private final Object ar;           // array for output,  0 .. size-1
     private final int [] permutation;  // perm of nums 1..size, perm[0]=0

     private boolean next = true;

     // int[], double[] array won't work :-(
     public Permute (Object [] e) {
        size = e.length;
        elements = new Object [size];    // not suitable for primitives
        System.arraycopy (e, 0, elements, 0, size);
        ar = Array.newInstance (e.getClass().getComponentType(), size);
        System.arraycopy (e, 0, ar, 0, size);
        permutation = new int [size+1];
        for (int i=0; i<size+1; i++) {
          permutation [i]=i;
        }
     }

    private void formNextPermutation () {
        for (int i=0; i<size; i++) {
           // i+1 because perm[0] always = 0
           // perm[]-1 because the numbers 1..size are being permuted
           Array.set (ar, i, elements[permutation[i+1]-1]);
        }
     }

     public boolean hasNext() {
        return next;
     }

     public void remove() throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
     }

     private void swap (final int i, final int j) {
        final int x = permutation[i];
        permutation[i] = permutation [j];
        permutation[j] = x;
    }

     // does not throw NoSuchElement; it wraps around!
     public Object next() throws NoSuchElementException {

       formNextPermutation ();  // copy original elements

        int i = size-1;
        while (permutation[i]>permutation[i+1]) 
            i--;


        if (i==0) {
           next = false;
          for (int j=0; j<size+1; j++) {
              permutation [j]=j;
           }
          return ar;
       }

        int j = size;

      while (permutation[i]>permutation[j]) j--;
        swap (i,j);
        int r = size;
        int s = i+1;
        while (r>s) { swap(r,s); r--; s++; }

        return ar;
     }

     public String toString () {
        final int n = Array.getLength(ar);
        final StringBuffer sb = new StringBuffer ("[");
        for (int j=0; j<n; j++) {
           sb.append (Array.get(ar,j).toString());
           if (j<n-1) sb.append (",");
        }
        sb.append("]");
        return new String (sb);
     }

     public static void main (String [] args) {
        for (Iterator i = new Permute(args); i.hasNext(); ) {
           final String [] a = (String []) i.next();
           System.out.println (i);
        }
     }
  }
Moritz Petersen
  • 12,902
  • 3
  • 38
  • 45
lyra
  • 31
  • 3
  • 5
    Post the stacktrace, please. – Moritz Petersen Sep 02 '13 at 09:53
  • 6
    Why can't you post the full Stack trace ?? That Stack trace it self contains the 90% of solution. – Suresh Atta Sep 02 '13 at 09:56
  • The most overlooked element to 99% of all bugs/issues when people post questions here. –  Sep 02 '13 at 09:56
  • 3
    This question appears to be off-topic because it does not describe the specific problem sufficiently, especially due to lack of a stacktrace. – nanofarad Sep 02 '13 at 09:59
  • Thank u for the response ...im getting error in these two lines.1.> while (permutation[i]>permutation[i+1]) i--; 2.> final String [] a = (String []) i.next(); – lyra Sep 03 '13 at 04:42

1 Answers1

1

ArrayIndexOutOfBoundsException Thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.
As you have not posted any stack trace. Problem is unclear. But these links may help you.

  1. oracle docs
  2. stackoverflow
Community
  • 1
  • 1
Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
  • thank u for the response.im getting error in this two lines 1.>while (permutation[i]>permutation[i+1]) i--; if (i==0) { next = false; for (int j=0; jfinal String [] a = (String []) i.next(); – lyra Sep 03 '13 at 05:01
  • @ShilpaChidaravalli: if answer is useful to you then please mark as answer. Thanks. – Aniket Kulkarni Sep 03 '13 at 05:28