-3

CODE:

for (int i =0; i<=10; i++) {
System.out.println(movieasArrayList.get(i).getID() + " " + movieasArrayList.get(i).getAction()); 
  }

QUESTION:

Change this for-loop to a for-each loop to perform the same task for all Movies (not just first 11).

Any help will be appreciated!!

Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
Andrea
  • 1
  • 3
    Firstly you must have an collection to use foreach. Please look at this; http://stackoverflow.com/questions/85190/how-does-the-java-for-each-loop-work and also this; http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html – Semih Eker Dec 17 '14 at 03:25
  • 6
    Come on, you've got to try to solve it yourself first, else you're just cheating yourself out of what matters the most -- the opportunity to learn something, to challenge yourself. So please let's see a post of your best attempt before someone spoon feeds you an answer. – Hovercraft Full Of Eels Dec 17 '14 at 03:26
  • @SemihEker-He is having a collection,namely,`moviesArrayList`! He just wants the answer without any effort of his own. – Am_I_Helpful Dec 17 '14 at 03:28
  • You need to try it out first. Then let us know when you get caught somewhere. – Vagabond Dec 17 '14 at 03:31
  • 2
    Seriously, it would have taken less time to look this up in the docs than it took to post this question. – Dawood ibn Kareem Dec 17 '14 at 03:33
  • Before you ask a simple question like this, try to figure it out yourself by looking in the docs, it's to your own benefit – deepmindz Dec 17 '14 at 03:36
  • wow you guys are so rude (and I am a SHE btw) ! I don't want any answers without effort programming is not easy for me I am struggling a lot have no idea where to even start..The teacher didn't really talk about how to convert it and the tutorials are not helping..I am not looking for the answer but can you guys at least give me a hint on where to start.. I am not a good programmer this is my first programming class and I feel lost.. – Andrea Dec 17 '14 at 03:37
  • wow...That's all I can say.. thanks guys! have a good night! – Andrea Dec 17 '14 at 03:38
  • @AndreaDuarte-Sorry for commenting as (he) for you instead of she. Please see the answer below. Also,starting of everything causes slight/more pain,it's the flow which gives them the ability to handle it! – Am_I_Helpful Dec 17 '14 at 03:39
  • @shekharsuman thanks for the apology it's okay and thanks for the help.. I really wasn't looking for just the answer so thank you for also posting the structure I really appreciate it I will make sure I understand it from here! – Andrea Dec 17 '14 at 03:50
  • Andrea, the best thing you could read would be the [second link that Semih posted in his/her first comment](http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html). Or maybe [this one](https://blogs.oracle.com/CoreJavaTechTips/entry/using_enhanced_for_loops_with). I'm sorry if I came across as rude earlier, but please understand that this site is really designed for a higher level of question than this. There are many web sites around, including the excellent online tutorials made by Oracle, that will give you help with Java that is more appropriate to your level. – Dawood ibn Kareem Dec 17 '14 at 04:21
  • David I understand what you are saying and sorry for asking such basic questions I guess when the tutorials are not making sense is nice to have someone that knows help out (specially if it takes someone as experienced as you 1 min to answer my questions) with all due respect I have seen answers to WAY simpler questions than the one I had in this site, so I think I can still post my questions an I guess I'll risk it and see if someone has a min of their precious time to help out but I appreciate your suggestion! – Andrea Dec 18 '14 at 21:03

1 Answers1

1

The for-each loop structure in java goes as follows:

// works with collections and arrays, everything
// that has an iterator
Collection<Object> c = new Collection<Object>();
for (Object i : c) {
    // do code
}

For you, this would look like this:

// I don't know what your movieArrayList contains
for (Movie i : movieArrayList) {
    System.out.println(i.getId() + " " +  i.getAction());
}

Hope this helps!

August
  • 12,410
  • 3
  • 35
  • 51
deepmindz
  • 598
  • 1
  • 6
  • 14