Link to home
Start Free TrialLog in
Avatar of Guster
Guster

asked on

How do you reset an array afyer you've used it??

Hi,

I'm reading in a different set of values to a 2D Array continuosly from an image that will then be put into a 1D Array. For each instance of the 1D Array I want to do certain calculations and then I want to reset the array so that the next batch of 2D Array values being read in can be inserted into the 1D Array. How do I reset an array?? Below is the section of code where I'm trying to do this. At the moment when I compile the program I get an error message saying 'ArrayIndexOutOfBoundsException' at the line:
copy[x] = sum;

Here is the section of code:

public void filter(int[][] template) {
 
       
  IntImage src = new IntImage(ImageSize, ImageSize);
  src = imageIn;
       
  int nMaskRows = template.length;
  int nMaskCols = template[0].length;
       
  int rBorder = nMaskRows / 2;  // integer division
  int cBorder = nMaskCols / 2;
  int sum;
  int[] copy = new int[nMaskRows*nMaskCols];//1D Array
  int x = 0;
 
  for (int r = 0; r < (ImageSize - nMaskRows + 1); r++) {
    for (int c = 0; c < (ImageSize - nMaskCols + 1); c++) {
    sum = 0;
    for (int mr = 0; mr < nMaskRows; mr++) {
      for (int mc = 0; mc < nMaskCols; mc++) {
      sum = imageIn.pixels[r+mr][c+mc] * template[mr]    [mc];//getting the result value
      copy[x] = sum;  //<==ArrayIndexOutOfBoundsException
at this line
      ++x;

      }//for mc
    }//for mr
             
   sort(copy); // sorting the 1D Array
   imageOut.pixels[r+rBorder][c+cBorder] = median(copy);
//setting the output pixel to be the median
       THIS IS WHERE I WANT TO RESET THE ARRAY 'copy[]'?
 }//forc
}//forr
       
  display(imageIn, imageOut);
  }//filter
     
Regards,
Guster

ASKER CERTIFIED SOLUTION
Avatar of iDeb
iDeb

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of delphi3
delphi3

Guster,
Insert a System,out.println(x) below
     copy[x] = sum;  //<==ArrayIndexOutOfBoundsException
at this line
     ++x;

as this:

     copy[x] = sum;  //<==ArrayIndexOutOfBoundsException
at this line
     System,out.println(x);
     ++x;



x may be either near zero, or it may be extremely large and exceed the capacity of the array that you set earlier.

Delphi3
oops a typo in that

System.out.println(x);

Delphi3