Hi all, so I am trying to transfer my methods from my Array classes to classes using ArrayList. However, I have run into a couple of issues. Firstly, I have run into the issue that in my reverse method, it will print out the first half of my ArrayList as its index positions and the second half as the actual integers that should be printed out. Curiously enough, the compiler still recognizes that this reverse order is not the correct order, and returns false. My other issue is with my merged method. I am not able to check to see if it is correct because I am not sure how to convert the line of code in my main class that should be printing out my merged method into one that can print out array lists. It is still in the format I used to print out my regular Array and I am not sure how to convert it. Here is my current code:
Here are the lines in my main method which I do not know how to convert to print out my merged arrayList:
If needed, I can show the regular array methods for my reverse and merge methods.
How would I convert this method that gets rid of the final comma at the end of the Array into an ArrayList? When I try to convert it, it will not print out the full arrays, so I bet I'm not converting it right. Here is my regular Array method for getting rid of the last comma:
I'm currently just using this:
because I am not sure how to convert it successfully.
There are (at least) two approaches.
Another would be to use StringBuilder.
The advantage to StringBuilder is that you reduce the calls to System.out which can be expensive in large quantities (not really an issue in your case).
Thanks for the help, I really appreciate it. I will use your first one however since that is within what the AP board would be looking for. String builder is not apart of our curriculum, sadly, alongside many other concepts that would make coding so much easier (ex. addAll to merge two arrays). However, looking at your first method, even though it does work, I'm not quite sure I follow the logic. Is it that it will print a comma only as long as the boolean is true? I guess my main two questions are, how does the system determine that the boolean is true or not and why is the printing of e after the if statements, not before? I think I understand but I want to make sure.
David Vach wrote:Ah okay. That makes sense. So it prints out the e statement first, then it proceeds with the if statements? And it knows when its done based off of e right?
Hmmm. Not sure if you have it. The statements are executed in order so for the first 'e' the boolean 'first' is true (because that's how it was initialized) so then we set it to false, skip the 'else' block and then print 'e'. For the next 'e', 'first' is now false so print the comma and then the next 'e'.
Ohhhhh, okay. I think I have it now. So the boolean is set to true. And then the if statement says that if it is set to true, then set it to false. Then it prints the e statement. Then since it is false, it will print a comma the next time it goes through the loop, then if statement. Then it will break the if and print another e statement. Then it will loop until there are no more e statements to print. Thats why no commas are printed after the last e.
Do or do not, there is no try.
It's just a flesh wound! Or a tiny ad:
Try Free Java/.NET Libraries for Word Excel PowerPoint and PDF