Solved

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

Posted on 2002-07-27
3
229 Views
Last Modified: 2012-05-05
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

0
Comment
Question by:Guster
  • 2
3 Comments
 
LVL 2

Accepted Solution

by:
iDeb earned 50 total points
Comment Utility
1. The ArrayOutOfBounds Exception is happening becuause you are not setting x =0 after the loop
 for (int mr = 0; mr < nMaskRows; mr++) {
     for (int mc = 0; mc < nMaskCols; mc++) {
          ...........
          ...........
     }
  }

  x = 0;

2. You need not reset the array. If you insist, use a double for loop to initialize the individual element s of the array to 0;
0
 
LVL 4

Expert Comment

by:delphi3
Comment Utility
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
0
 
LVL 4

Expert Comment

by:delphi3
Comment Utility
oops a typo in that

System.out.println(x);

Delphi3
   
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now