-5

I have tried the approach below,but still keep getting the arrayindexoutofbound exception.

public class Test {
  private Object[] o;
  String type, s[];
  Integer[] i = new Integer[50];
  Object temp = null;

  public <T> Test(T[] obs) {

    this.o = obs;

    if (obs[1] instanceof String) {
      type = "string";
    } else if (obs[1] instanceof Integer) {
      type = "integer";
    }
    if (type == "string") s = (String[]) o;
    else if (type == "integer") i = (Integer[]) o;
    // System.out.println(type);

  }

  public int compare(int one, int two) {
    if (type == "string") {

      int l = s[one].compareTo(s[two]);
      if (l == 0) return 0;
      else if (l > 0) return 1;
      else return -1;

    } else if (type == "integer") {

      System.out.println("integer");
      for (int a : i) System.out.println(a);
      if (i[one] > i[two]) return 1;
      else if (i[one] < i[two]) return -1;
      else return 0;
    }
    return 4;
  }

  public void swap(int i, int j) {
    o[i] = temp;
    o[i] = o[j];
    o[j] = temp;
  }
}

The class where i am implementing the above compare for bubblesort:-

import jsoup.Test;

public class BubbleInt {
  public static void main(String args[]) {
    Integer[] arr = {2, 1, 8, 7};

    Test t = new Test(arr);
    for (int i = 0; i < arr.length - 1; i++) {
      for (int j = 1; j < arr.length - i; j++) {
        int val = t.compare(arr[j - 1], arr[j]);
        System.out.println(val);
        if (val > 1) t.swap(arr[j - 1], arr[j]);
      }
    }
    for (int i : arr) {
      System.out.println(i);
    }
  }
}
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
SKY
  • 7
  • 1
  • 7
  • Take a moment to read through the [editing help](http://stackoverflow.com/editing-help) in the help center. Formatting on Stack Overflow is different than other sites. The better your post looks, the easier it will be for users to help you. – gunr2171 Mar 11 '16 at 15:47
  • You should really learn some more about generics rather than trying to implement it with this `instanceof` stuff. And Comparators. It's just making it harder than necessary! At the very least, get it working for String or Integer first. – Andy Turner Mar 11 '16 at 15:52

2 Answers2

0

consider

for(int j=1;j<arr.length-i;j++)

for i = 0,

int val= t.compare(arr[j-1],arr[j]);

runs out of bounds.

Tobi Nary
  • 4,566
  • 4
  • 30
  • 50
  • hi, while passing the array to the constructor of my test class ,i first used an enhanced for looop to make sure if my array is being passed correctly,but there also i am getting the same error – SKY Mar 11 '16 at 15:49
0

The main problem is these lines:

int val = t.compare(arr[j - 1], arr[j]);
System.out.println(val);
if (val > 1) t.swap(arr[j - 1], arr[j]);

Since your compare and swap methods actually take array indices, these should be:

int val = t.compare(j - 1, j);
System.out.println(val);
if (val > 0) t.swap(j - 1, j);

Otherwise, you are using the array elements as array indices, and the values in the array are greater than the number of elements in the array.

Note the change in the condition on the last line too: the only value of val that you return which is > 1 is 4, and that only occurs for types other than String and Integer.


In your swap method, the first line:

o[i] = temp;

should be:

temp = o[i];
Andy Turner
  • 137,514
  • 11
  • 162
  • 243