[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2002-07-27
3
Medium Priority
?
237 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 2

Accepted Solution

by:
iDeb earned 150 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

650 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