This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
Notice that the variable k get incremented regardless of whether the condition is sucessful or not. This means that when a number get removed from the nums list, one number will get skipped. So... all the numbers after a zero is encountered will not be checked -- including another zero.
ah I see. The incrementing business is the hardest part for me to understand.
OK so let me talk through this and see if I fully understand (correct me plz if I make mistakes):
So we start out with an ArrayList method declared private and named nums. Making it private means that this method is not accessible outside the class... I think.
We know, although it's not stated in this code(it's in a comment), that the nums.size method (am I right in saying method here? I'm assuming everything that has an opening and closed parenthese after it with nothing in it is a method) is always going to be larger than 0. We also know that nums contains all Integer objects.
We now have a public method called numQuest(what is the purpose of making it void? Why isn't it public static void numQuest()? I've never really understood the purpose of static or void, and using them together). We declare k to equal 0, and we declare zero as an integer equal to 0(that's why it's called zero! hehe...).
Now we have a while loop to test for when k is 0 or less (nums.size() is more than 0). If k actually equals 0, then we're going to remove that number. Then we're going to increment k(add one to it? I'm kinda iffy here about how this skips the number after zero in the ArrayList? I'm guessing that's just a behavior of the ArrayList in this case?)
While typing the question out above I think I have thought of the answer. If k equals zero, it will remove that zero. Then it will add 1 to k(0), making k equal to 1 for the next ArrayList number. Since k = 1 even though you see a 0, it will just go on to the next ArrayList number looking for a 0. That is why it skips a 0 after it finds a 0. On the other hand, a trick the collegeboard could use here(if I'm understanding this correctly), is to have an ArrayList of [0, -1, 3, 4, ...] In this case, it would skip the 0 and the -1 because adding 1 to -1 makes a 0. The answer would be [3, 4, ...]
Again, I'm a high school senior in AP Computer Science A reviewing for the AP test. Unfortunately, my teacher doesn't know Java well enough to answer this question and explain it to me. Thankfully I researched up on-line (yay for google!) and stumbled upon these forums, and I think I'll be discussing my Java problems more from now on Thanks guys!
P.S. Even if you guys don't read all of this cause you're lazy (like I am oftentimes), at least writing it out helped me think through the problem in my brain. I definitely understand it a heck of a lot better now than I did just eyeballing the question. Unfortunately, on the AP test I'll have lots and lots of questions like this to do in a timed environment. I don't expect to pass the AP test (no one my teacher taught last year got over a 2, and most got ones), but who knows maybe I'll be the first. I am the first student she's had to take the AP Comp Sci A class without taking the prerequisite Visual BASIC and C++ course (I took it over the summer on my own by answering the questions at the end of the chapters and doing all the programs -- for the first 6 chapters in the Visual BASIC book and C++ book, which is all she covered in her class. And yes I know that's pathetic when the book is like 12-15 chapters for both BASIC and C++!).