2

I want to return an array with the index's where the getElem() is equal or higher a certain value.

This is my function:

public static int []findIndex(BufferedImage image,int value)
{
    DataBuffer b = image.getRaster().getDataBuffer();
     int array[] = new int[600];
    for(int i=0;i<76400;i++)
    {
        if(b.getElem(i)>=value)
            array[i]=i;
    }
    return array;
}

but i have an error

"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 27001
    at arraytest.imageMan.findIndex(imageMan.java:139)
    at arraytest.imageMan.main(imageMan.java:183)"
6
  • 3
    Print the index, I bet it must be greater than 599 !!!!! You are initializing the array with 600 , so anything greater than 599 will throw error !!! Commented May 13, 2013 at 15:26
  • Yes the problem is the size of the array.. Commented May 13, 2013 at 15:26
  • Why do you loop to 76400? Since your array is only stores 601 elements this might cause the problem? Commented May 13, 2013 at 15:27
  • @Kuchi The array stores 600 elements Commented May 13, 2013 at 15:38
  • I want search in my DataBuffer(76400 pos), which of then are equal to my value, and store it on array..I want only 600 values, so i declare my array with 600 pos. Commented May 13, 2013 at 15:51

4 Answers 4

2
 int array[] = new int[600];

The array you declare is of size 600.

 for(int i=0;i<76400;i++)

Yet you attempt to reference the array at the 76400'th index.

Why doesn't this work?

Well, when you say new int[600], you are essentially saying, my array can store 600 things, and this means that it has 600 different "slots" to store values.

You access these values by their index, starting from 0.

array[0] // First item
array[599] // Last item.

Your error has occurred because you have exceeded 599.

How to fix this

Well, you can either make your array 76400 long, (which to me is suspicious), or you can change 76400 to array.length (or 600) in your for loop.

Sign up to request clarification or add additional context in comments.

Comments

2

This is the solution i find..

I think that is the best..

 public static int[] findIndex(BufferedImage image, int value) {
    DataBuffer b = image.getRaster().getDataBuffer();
    int array[] = new int[600];
    int j = 0;
    for (int i = 0; i < b.getSize(); i++) {
        if (b.getElem(i) >= value) {
            if (j < 600) {
                array[j] = i;
                j++;
            }
        }
    }
    return array;
}

3 Comments

That's pretty good except what if there are less than 600 matches?
I don't test that possibility, but my image have 76400 pixels, so, i think it's hard don't have 600 matches. This is an solution for that exact problem, for others, it's likely don't working..
Well in that case, it looks like you've found the answer to your own problem. You're allowed to mark it as such if you want.
0

That's because your loop from 0 to 76400, which much much greater than the size of your array.

public static int []findIndex(BufferedImage image,int value)
{
    DataBuffer b = image.getRaster().getDataBuffer();
    int array[] = new int[600];
    for(int i=0;i<array.length;i++)
    {
        if(b.getElem(i)>=value)
            array[i]=i;
    }
    return array;
}

Comments

0

This is my final function. How should we choose a generic programming,i made some changes that made ​​a focused precisely on that. Use now have a list instead of a static array.

   public static int[] findIndex(BufferedImage image, int value) {
    DataBuffer b = image.getRaster().getDataBuffer();
    ArrayList<Integer> a = new ArrayList<>(20);

    int j = 0;
    for (int i = 0; i < b.getSize(); i++) {
        if (b.getElem(i) >= value) {
            a.add(i);
            j++;
        }
    }
    int array[] = new int[a.size()];
    for (int k = 0; k < a.size(); k++) {
        array[k] = a.get(k);
    }
    return array;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.