Solved

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

Posted on 2002-07-27
3
230 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Understanding websocket example in spring 1 57
VB Script to add site to Java Exception List 4 54
custom annotations 9 33
jdbc error in jsp application 20 21
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…
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 will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
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.

863 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

21 Experts available now in Live!

Get 1:1 Help Now