Solved

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

Posted on 2002-07-27
3
232 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
ID: 7182180
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
ID: 7182196
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
ID: 7182204
oops a typo in that

System.out.println(x);

Delphi3
   
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

840 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