This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of Transfer Learning for Natural Language Processing (MEAP) and have Paul Azunre on-line!
See this thread for details.
Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Trying to transfer methods from Array to ArrayList

 
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 7064
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need to use the set() method, you can just use add() which means you can get rid of the "k++".
merge() now returns a List<Integer>.
 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Carey Thanks! That really helped. I do have one final question however.
 
Carey Brown
Bartender
Posts: 7064
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I wanted to post them separately haha. I didn't mean to confuse you.
 
Carey Brown
Bartender
Posts: 7064
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Carey Brown
Bartender
Posts: 7064
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Carey Brown
Bartender
Posts: 7064
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

David Vach wrote: why is the printing of e after the if statements, not before?

It is trivial to know when you are dealing with the first occurrence. If it's not the first then precede the 'e' with a comma.
 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Carey Brown
Bartender
Posts: 7064
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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'.
 
David Vach
Ranch Hand
Posts: 105
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It's just a flesh wound! Or a tiny ad:
Try Free Java/.NET Libraries for Word Excel PowerPoint and PDF
htttp://www.e-iceblue.com/free-apis.html
    Bookmark Topic Watch Topic
  • New Topic