foreach

Iterator for array

How to get an iterator for an array in java? int[] arr={1,2,3}; for(int i:arr) System.out.println(i); How does the above for-each loop work ? Is the array converted to a list to get the iterator ? ...

get the sub index in foreach loop in simpleXMLElement

<?xml version="1.0" encoding="utf-8"?> <items> <item> <title>This is title1</title> <desc>This is desc1</desc> <image></image> <tudou></tudou> </item> <item> <title>This is title2</title> <desc>This is desc2</desc> <tudou>55362137</tudou> </item> <item> <title>This is title3</title> <desc>Thi...

Reading an XML in C#

Hello, I'm using System.Xml to read a xml file in C#. First I open the file (locally)... and use foreach to get the values, like this: XmlNodeList titles = xmlDoc.GetElementsByTagName("title"); foreach (XmlNode title in titles) { rowNews = new ListViewItem(); rowNews.Text = (title.ChildNodes[0].Value); listView1.Items.Add(rowNews); } ...

foreach(Derived obj in new List<Base>())

What does the following code do? class Base { } class Derived : Base { } class Test { void Foo(List<Base> list) { foreach (Derived obj in list) { // ... } } } I didn't expect it to even compile, but it does. ...

Java iterators and for-each-loop. any way to access the underlying iterator?

I very much like the for-each-loop construction (for(T e : iterable)) in Java which works on any Iterable<T> because it makes in many cases very easy to read and to write code. I wonder though if there is any way that I can access the underlying iterator from such a loop. This can be mandatory if I want to use the remove() from the iter...

Can't assign value to an object's property in a foreach loop?

I have an object defined as public class EmailData { public string TemplateId { get; set;} public IEnumerable<Recipient> To { get; set; } public IEnumerable<Recipient> CC { get; set; } public IEnumerable<Recipient> BCC { get; set; } } public class Recipient { public string Key { get; set; } ...

EF4 - Foreach local variable with static data

Hi guys.. I'm using EF4. I've found a very annoying issue. I have a db view that I've dragged into my Entity Model class. When I try to iterate over the hole collection, the data displayed, it's almost the same that the first objects... I've tried enabling Lazy Loading, but nothing works.. it's a really simple snippet.. why doesn't work...

How to rewrite this foreach looping using linq ?

Supposing I have two classes, Customer and Order, where one Customer can have one-or-many Orders associated to it. class Customer { Order[] Orders; } class Order { int OrderId; } If for any given Customer, I want to find all the associated OrderId's, is there an easy way to do that using linq ? Something that gives the same resul...

Multi-line foreach loop in linq / lambda

I am looking for a way to change the following code: foreach (Contact _contact in contacts) { _contact.ID = 0; _contact.GroupID = 0; _contact.CompanyID = 0; } I would like to change this using LINQ / lambda into something similar to: contacts.ForEach(c => c.ID = 0; c.GroupID = 0; c.CompanyID = 0); However that doesn't w...

PHP: cannot foreach simple associatie array

I wrote a code to parse through something, dynamically making an array out of the array keys of one array. This is from a form, so the odd key has a value, and that is somehow the problem. My code: //array values are not needed in my code, just junk rather $array = array('one_a'=>2, 'three_b', 'four_c', 'five_d'=>12); $number = array(...

foreach code in PHP

I would like to iterate through an xml document to get its values. See the given code foreach ($xml->children() as $key1=>$value1 /*($xml->children() as $second_gen)*/ ) { echo ' 1 ' .$key1.' '.$value1.'<br>'; foreach ($second_gen as $key2=>$value2) { echo ' ___2 ' .$key2.' '.$value2.'<br>'; } } So what I want to...

PHP - Grab the first element using a foreach

Wondering what would be a good method to get the first iteration on a foreach loop. I want to do something different on the first iteration. Is a conditional our best option on these cases? ...

C# - Can you loop through types separately in a generic list?

I have 3 different classes, a generic "entity" class, and then two classes that inherit this, a bullet class and an enemy class. I then have a list of the entity class, with the bullets and enemies in the list, and a lot of the places I treat these the same. However, some times, I want to loop through just one of the classes, and not th...

[Smarty, and maybe doctrine]

Hi everyone, I'hv got a problem when I am using foreach in smarty, an array with 2 item was loop in a foreach, but the result is it loop 3 time. I use doctrine to get a list of review by a user from database Doctrine_Core::getTable('review')->findByUser($userId); then I assign it to smarty and loop in foreach: {foreach from=$r...

PHP PDO fetch results

I have the following script which runs fine but I think a) it is ugly; b) it doesn't do what I need after the script is run. Utlimately, the script should work as follows: 1) SQL selects all agents from a given table where the enrollment date is within the last month, grouped together by agent number where the count is greater than ...

each_with_index desired column output Rails

<% source.strains.each_with_index do |strain, j| %> <% if (j == (source.strains.size - 1)) %> <font size="3">Strain <%= strain[0].appendix %> </font> <% end %> <% end %> I need to get output as If j value is last one of a loop, then i need to print first value of loop (j[0]). Kindly suggest me or correct abo...

Very basic numerical comparison program

For some reason, I can't understand the logic of a program that: Takes in a list of numbers Goes through them (via a for each loop) to find the least number displays the least number via writeline If leastNumber must be initialized to 0, then won't the leastNumber ALWAYS be 0? (I've provided a basic txt file containing a list of var...

Can I access the index from within a foreach?

In PHP I can do this: $list = array("element1", "element2"); foreach ($list as $index => $value) { // do stuff } In C# i can write: var list = new List<string>(){ "element1", "element2" }; foreach (var value in list) { // do stuff () } But how can I access the index-value in the C# version? ...

Order for ForEach on View Model

In MVC 2 I have a user control - Partial page as below. Model has four records id Dtext Dtext1 1 A, A1 2 B B1 3 C C1 4 D D1 On My machine - Output is as above in the ID Order which is expected. But after deployment output is totally bizarre something like below. D D1 B B1 A, A1 C C1 Would like to know ...

Is there an is_iterable or similar function for PHP?

Hello, I am passing a JSON'd feed from Twitter into my application, and sometimes Twitters API returns empty or incomplete feeds. This is causing a problem when I try to iterate the feeds using foreach - does anyone know of a suitable test to find out if an object or array is iterable? Could this be done with isarray($obj) || $obj inst...