Solved

for loop

Posted on 2007-11-20
17
222 Views
Last Modified: 2010-03-30
code shown below..

index[0] and index[1] have values 0 and 6 resp.

but the programs comes out of loop after checking 6 value

am missing something??
for(int m=0; m<12; m++) {
   if(m==index[0] || m==index[1]) {
	log.info("keeping "+m+" instance");
   } else {
	log.info("delete "+m);
	startingData.delete(m);
}
}

Open in new window

0
Comment
Question by:max_dub
  • 6
  • 6
  • 4
  • +1
17 Comments
 
LVL 7

Expert Comment

by:UrosVidojevic
ID: 20324269
Can you post the rest of code, please.
It seems to me that for loop must have 12 iterations.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20324275
Make sure you're not ignoring exceptions
0
 
LVL 92

Expert Comment

by:objects
ID: 20324278
whats the log show?
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

Author Comment

by:max_dub
ID: 20324309
that is pretty much the code where runtime error comes

log shown below

as per exceptions, the only thing i need to check is if the counter equals either if index value .. keep data .. otherwise delete data

but somehow it doesn't loop entirely upto 12
2007-11-20 23:23:16,406 INFO  SwingWorker-pool-1-thread-9 BuildClassifierHelper [] - image 1 to delete: 0
2007-11-20 23:23:16,406 INFO  SwingWorker-pool-1-thread-9 BuildClassifierHelper [] - image 2 to delete: 6
 
2007-11-20 23:23:16,406 INFO  SwingWorker-pool-1-thread-9 BuildClassifierHelper [] - Starting data: 12
2007-11-20 23:23:16,406 INFO  SwingWorker-pool-1-thread-9 BuildClassifierHelper [] - keeping 0 instance
2007-11-20 23:23:16,406 INFO  SwingWorker-pool-1-thread-9 BuildClassifierHelper [] - delete 1
2007-11-20 23:23:16,406 INFO  SwingWorker-pool-1-thread-9 BuildClassifierHelper [] - delete 2
2007-11-20 23:23:16,406 INFO  SwingWorker-pool-1-thread-9 BuildClassifierHelper [] - delete 3
2007-11-20 23:23:16,406 INFO  SwingWorker-pool-1-thread-9 BuildClassifierHelper [] - delete 4
2007-11-20 23:23:16,406 INFO  SwingWorker-pool-1-thread-9 BuildClassifierHelper [] - delete 5
2007-11-20 23:23:16,406 INFO  SwingWorker-pool-1-thread-9 BuildClassifierHelper [] - keeping 6 instance
2007-11-20 23:23:16,406 INFO  SwingWorker-pool-1-thread-9 BuildClassifierHelper [] - Starting data: 7
2007-11-20 23:23:16,406 INFO  SwingWorker-pool-1-thread-9 BuildClassifierHelper [] - Query Data: 10
2007-11-20 23:23:16,406 WARN  SwingWorker-pool-1-thread-9 BuildClassifierHelper [] - All Loaders Spawned

Open in new window

0
 

Author Comment

by:max_dub
ID: 20324351
ok, it seems to give arrayoutofbounds exception for no. 7

debugging now
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20324375
>>as per exceptions, the only thing i need to check is if the counter equals either if index value .. keep data .. otherwise delete data

It's doesn't really matter what you're doing. You can get an exception almost anywhere - often terminating the thread
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20324385
>>ok, it seems to give arrayoutofbounds exception for no. 7

(A RuntimeException )
0
 
LVL 92

Expert Comment

by:objects
ID: 20324404
get a stack trace f the exception, ando stop it breaking yo looppp use;

for(int m=0; m<12; m++) {
   if(m==index[0] || m==index[1]) {
        log.info("keeping "+m+" instance");
   } else {
        log.info("delete "+m);
        try {
           startingData.delete(m);
        } catch (Exception ex) {
           ex.printStackTrace();
        }
}
}
0
 

Author Comment

by:max_dub
ID: 20324460
yeah, after 6

it throws array out of bounds

java.lang.ArrayIndexOutOfBoundsException
      at java.lang.System.arraycopy(Native Method)
      at weka.core.FastVector.removeElementAt(Unknown Source)
      at weka.core.Instances.delete(Unknown Source)

wondering as the object does contains 12 instances?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20324534
You need to do some more debug to find out why
0
 

Author Comment

by:max_dub
ID: 20324541
yeah, when i am individually deleting the values it works fine

but not as a whole, i suppose its shifting the values in between

thanks for help!!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20324551
No problem
0
 
LVL 92

Accepted Solution

by:
objects earned 200 total points
ID: 20324571
It won't after you start removing them :)

try this:

for(int m=11; m>=0; m--) {
   if(m==index[0] || m==index[1]) {
        log.info("keeping "+m+" instance");
   } else {
        log.info("delete "+m);
        try {
           startingData.delete(m);
        } catch (Exception ex) {
           ex.printStackTrace();
        }
}
}

0
 

Author Comment

by:max_dub
ID: 20324601
wow, i was thinking of doing same, but then i thought probably it won't work.. its just the otherway round.. but its does work :)

so actually i was debugging it and each time i was delete an instance, it was shifting it upwards and when it reached 7, it was already out of that reach .. hence the array out of bounds

now i wonder, why wud from 11 to 0 work and not 0 to 11??
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20324653
?
0
 

Author Comment

by:max_dub
ID: 20324724
i don't knw ??

somehow it works otherway around - from 11 to 0 .. mebbe experts can say why

thou what i was trying to do was to delete each instance individually and once it deletes an instance, i updated the count so that it deletes the right index and no exception then

but objects sol. worked too.. i still need to figure it out why?
0
 
LVL 92

Expert Comment

by:objects
ID: 20324841
as i mentioned above you won't have 12 after you start removing them.
0

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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 about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
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.

832 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