From JDK1.5, added a new feature foreach, which is a shorthand form for looping through data, using a keyword that is still for, but with a different parameter format. Its detailed usage is:
for (Type e:collection) {
Parameter description:
E: Its type is the type of the element value in the collection or array, which is an element of the collection or array collection.Collections: A collection or array to traverse, or an iterator.
The
In a program to be written today, I want to use foreach and enum to implement a function and find that I have forgotten it. Now I want to sort it out, paste it to your friends to make it easy and prevent you from forgetting it!
I am still a student. If you have any ideas, please advise!
First, paste the code I wrote. (to highlight the key point, only write the code that can respond to your needs !)
Public
PHP is very efficient in development, which is understandable, but at the expense of execution efficiency. PHP Array function is very powerful, but also to consider more, try a few situations, just in case, here, I simply say two encounters of the pit, later if there are found more, then fill it!
foreach provides a simple way to iterate through an array, and it is easy to read the contents of the data or object, but the official document says that be
The YII framework can use foreach to iterate over objects and why objects can be accessed directly using an array formDuring the use of the YII framework, we can use foreach to iterate through the properties of the object returned by the FindAll method, etc.Why is it? In fac
PHP is very efficient in development, this is understandable, but it is at the expense of implementation efficiency. PHP Array function is very powerful, but also need to consider more, try several situations, just in case, here, I simply said that two encountered the pit, later if there are more to find, and then fill it!
foreach provides a simple way to traverse an array to easily read the contents of data or objects, but the official document sa
For the list of these sequential tables, we can use the for instead of foreach when we solve them. But for non-sequential tables, such as dictionary or set, we can extend the methods Foreach,foreachkey and Foreachvalue instead of the original foreach.For extension methods, refer to: https://msdn.microsoft.com/zh-cn/library/bb383977.aspxStatic Class dictionaryex{/
1. Using foreach to traverse the page control and the use of the trinocular operatorUse Div to put a set of checkboxes together for traversing"Form1"runat="Server"> "cb_1"runat="Server"autopostback="true"text="I was automatic."oncheckedchanged="cb_1_checkedchanged"/> "tb_1"runat="Server">"chklist"runat="Server"> "cb_2"runat="Server"text="Reading"/> "Cb_3"runat="Server"text="Listen to the song"/> "Cb_4"runat
Generally, when we create a custom set, we can only implement the ienumerable interface (which may also implement the ienumerator Interface) to support foreach traversal)
However, we can also use the iterator Method built with the yield keyword to implement foreach traversal, And the custom set does not need to implement the ienumerable interface.
Note: although
1 // enable the class to use 2 public class students 3 {4 int I =-1; 5 6 student [] stus; 7 public students () in the FOREACH Loop () {8 stus = new student [2] {new student ("xx"), new student ("wsenmin") }; 9} 10 public IEnumerator GetEnumerator () 11 {12 return (IEnumerator) this; 13} 14 public object Current15 {16 get {17 return stus [I]; 18} 19 20} 21 public bool MoveNext () 22 {23 I ++; 24 return (I
V
In C # , As is the case with Java, there are two ways to create arrays. Only one array is said here. 1 ways to create:Type [] array name ={ element 1, element 2,...};For example:// declare an array of integers int[] array_01={1,2,3,4,5,6};// outputs the contents of an array element for(inti=0; i{Console.WriteLine(Array_01[i]);}Section 2 ways to create: Type [] array name =new type [ number of elements ]; // initialize an array int[] array_02=Newint[6];array_02[0]=1;array_02[1]=2;array_02[2]=3;
If you use foreach to loop the two-dimensional array, the second loop will not be displayed, but if you comment out the first loop, the content of the second loop will be displayed normally, which of your friends knows the reason? thank you very much! PHPcode $ products2array (array (name gt; Apple, quantity gt; 6, unit price gt; 12.2)
The second loop will not be displayed in the second loop. However, if
This article describes how to use the foreach, list, and each functions for Traversing arrays in PHP:
Method 1: foreach$ Sports = array (Football => good,Faster Ming => very well,Running => not good );Foreach ($ sports as $ key => $ value ){Echo $ key. ":". $ value. "?>
Output result:Football: goodFaster Ming: very w
The main use of foreach is in the build in condition, which can iterate a collection in an SQL statement.
The properties of the Foreach element are mainly item,index,collection,open,separator,close.
The item represents the alias of each element in the collection when it iterates, and index specifies a name that represents the position at which each iteration occu
Label:The foreach tag is primarily used to build in conditions, and he can iterate over the collection in SQL. As follows:Delete from the user where ID in#{id}If we say that the parameter is----int[] ids = {1,2,3,4,5}----then the following SQL is printed:Delete form user where ID in (1,2,3,4,5)Interpretation:The value of the Collection:collection property is three list, array, map three, respectively, the corresponding parameter type is: List, array,
To implement a class that can be used with a foreach loop, you must implement Iterable, which overrides a Iterator method, which returns a iteratorThe code is as follows:public class Itertest implements IterableStores the contents of the array private object[] Obj=new object[1];//record the number of elements added to the private int size;Records the current element's subscript private int current=0;//add element public void Add (String str) {Determ
The first format is: for (element Type element variable: traversal Object) { executed code}For example:public class Test {public static void main (string[] Args) { //define an integer array, save score Information int[] scores = {89, 72, 64, 58, 93 }; Sorts the array of arrays classes Arrays.sort (scores); Use foreach to iterate through the elements in the output array for (in
This example applies to. NET Framework 2.0 and later versions.
A simple class
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->
1
Internal
Class
Goodsshelf
2
{
3
//
Cargo List
4
String
[] Goods
=
{
"
First thing
"
,
"
Second thing
"
,
"
Third thing
"
,
"
Fourth thing
"
,
"
Th thing
"
};
5
6
///
7
///
Get iterator
8
///
I. Answer
You can use foreach to traverse the accessed objects.IenumerableInterface or declarationGetenumeratorMethod Type
Note: you do not have to implement the ienumerable interface, but you must implement the getenumrator method.
Ii.. NET 1.0 implementationCode
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->Public class mylist {
Public int count {get {r
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.