I've been scratching my head at this for hours now, but I can't seem to figure this one out.
I'm writing a program that has a constant loop going in a Thread, which occasionally sends back data to another part of the program using an Arraylist.
I only want to send this data back after I have gathered 11 items in my arraylist. So I used this code:
//The rest of the loop in which i gather the values for key and velocity.
notenbuffer.add(new Noot(key,velocity));
if (notenbuffer.size() > 10){
System.out.println("Notenbuffer > 10, verstuur data");
if (notenbuffer.isEmpty()){
System.out.println("buffer is empty!");
}
else {
usbRead.writeNotes(notenbuffer);
System.out.println("emptied the buffer!");
notenbuffer.clear();
}
}
Now for some weird reason the program never empties the Arraylist, and just keeps on adding items to it. I've checked and it does in fact reach the usbRead.writeNotes(notenbuffer) part because this function gets called correctly. When I use the debugger it simply skips to the beginning of the loop after this function was called.
Is there a way in which I can empty an Arraylist once it reaches a certain size?
Edit: I made a logic error by writing if (notenbuffer.isEmpty()) this will always be false because I am already in an if statement which requires this to be false.
Did you put your second if inside your first by accident? (The missing indentation seems to suggest so). Having if
(notenbuffer.isEmpty()) inside the if (notenbuffer.size() > 10) block
makes no sense at all logically. After all if your List size is > 10
then the list is obviously not empty. So if (notenbuffer.isEmpty())
can never be true at all. – OH GOD SPIDERS 15 mins ago
This was indeed the problem. Removing this exposed an NoClassDefFoundError that needed resolving.
Related
cvShowImage("SMART", cropped);
cvWaitKey(10);
cvShowImage("SMART", cropped);
cvWaitKey(0);
What is the difference between these two functions and when i use this in infinite for loop cvWaitKey(10) works but cvWaitKey(0) or cvWaitKey(30) do not work. What is the reason?
The difference can be found in the documentation of OpenCV of the waitKey function.
Basically, the function waitKey waits for a key to be pressed, and the argument is the amount of time it will wait. So, when you use 10. It will wait 10 milliseconds and then continue with the program.
The documentation says:
0 is the special value that means “forever”
So, when you use 0. The program will wait for key to be pressed forever... Just pressing any key will continue the program... (also closing the window will do it)
I recommend to use 10 when you are doing a stream of pictures (maybe from a camera). And use 0, when you are expecting human interaction for the program to continue. And a bigger value if you want to see it for enough time, but continue the program without any interaction.
Hey guys, i have this while loop for variable 'i' and i have created a board[][] which is 2x2 (so sizeX for the board is 2 in this example). The problem is i can't work out why it isn't leaving the while loop if it goes through one of the 'if' statements starting with 'i=1' and then it then sets 'i=i+1' (=2) but doesn't leave the while statement and tries to see what board[2][x] is equal to when obviously that is out of the bounds of the board. Sorry for confusing explanation i hope you understand...
I am assuming that you are new to programming because you are using BlueJ.
When your second loop is running, I'm guessing that your line x=x is keeping x within range and hence the loop never exits.
One good thing to help is to use a debugger or print statements to show what the values of x and i are at each stage. That will quickly let you know what is going wrong.
I'm using the enhance for loop in my android application to iterate over a list of Status Objects.
for (Status stat : status)
timelineItems.add(new TimelineItem(stat));
This is generating the following IndexOutOfBoundsException:
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
I realized that the this is happening is because the size of the ArrayList is 0 which means its empty. My question is why did it even enter the loop in the first place isn't the enhanced for created to guarantee that these problems don't occur and how to I stop it.
First of all: Don't use enhanced for loop on Android if you're using an ArrayList. It's slower than the regular one and uses more memory. For all other collections, it's OK.
What you're experiencing probably relates to non-synchronized modification of the collection. If the size suddenly changes while the loop is running, it'll cause errors like that one.
The only way this can happen is if somebody else (another thread) clears the collection while the enhanced for loop was executing. It identified that there was a zeroth element, but when it came to access it it had been deleted.
I'm doing some java programming in Netbeans 7.3 and I have written a program that contains a complex while loop. It reads a file in, does some calculations, then prints an output file with some changes.
Something goes slightly wrong at the point where the while loop goes through the 158th line of the text (158th cycle of the while loop). Nothing appears to be wrong with the file itself at this point.
Am I being stupid when putting a breakpoint on the while loop and continuously clicking "Step Out" 158 times to see what's going on? Is there an easier way to do this with java? Like running the loop until a variable reaches a certain value?
adding debugcode like
if (iterationCount == 158) {
int dummy = 13; // <= put breakpoint here
}
is a practical alternative to "conditional breakpoints".
I'm working on a Java class in an Android project that summarizes array entries saved in previous classes, with each entry itself being an array with multiple elements.
I've have created methods to move forwards and backwards through the entries, but given there can be over 100 entries I would like to create another method that cycles through them instead of pressing the "Next" button over and over again.
Is there a way to do this?
I've found that loops will only show the last entry, but below is the best example I can think of, of what I need.
for (int i = Selection; i<=Qty; i++){
Num.setText(Integer.toString(i));
loadNext();
try{
Thread.sleep(1500);
}catch(InterruptedException e){}
if (Brk=true){
break;
}
}
The solution that would be closest to your original answer would be to create a background thread that does the loop, loading each item inside an Activity.runOnUiThread(). You can also do a similar thing with AsyncTask and progress updates. See this article for more information on both of these:
http://developer.android.com/resources/articles/painless-threading.html
However, a better solution is to not have a loop at all - just have a timer, and increment your loop variable each time the timer runs.
It may work. However, it will cause your UI to freeze during each time you call the sleep method. In general, when you are dealing with UI stuff, never use Thread class. Instead, use the Handler class. There are a lot of documentation but if, after you have search exhaustively, you can't find a good example just let me know.
Your break condition seems wrong, and causes the loop breaks at the first iteration:
if (Brk=true){
break;
}
Brk=true is an assigment exception, not a comparation exception. It will return always true. The expresion should be Brk==trueto check if the variable value is true. But again, it is a boolean variable, so you don't need to compare, but just reference it at the if statement:
if (Brk){
break;
}