Foreach循环输出索引值

循环输邮索引值,使用for是没有任何问题:

 class Bh
    {
        public string[] str { get; set; }

        public void TestFor()
        {
            for (int i = 0; i < str.Length; i++)
            {
                Console.WriteLine("index:{0},Value:{1}", i, str[i]);
            }
        }
    }

Source Code

运行程序:

但是,某一情况之下,你在程序中,是使用foreach方法进行循环的,但又想输出索引的话,那怎样实现呢?

class Bh
    {
        public string[] str { get; set; }

        public void TestFor()
        {
            for (int i = 0; i < str.Length; i++)
            {
                Console.WriteLine("index:{0},Value:{1}", i, str[i]);
            }
        }

        public void TestForeach()
        {
            int idx = 0;
            foreach (string s in str)
            {
                Console.WriteLine("index:{0},Value:{1}", idx, s);
                idx++;
            }
        }
    }

Source Code

再次运行,结果可见:

原文地址:https://www.cnblogs.com/insus/p/8124407.html

时间: 2024-08-10 23:17:23

Foreach循环输出索引值的相关文章

PHP中的&amp;传值引用的问题,在foreach循环的结果能帮解释下输出的结果原理是什么?

PHP中的&传值引用的问题,在foreach循环的结果能帮解释下输出的结果原理是什么? 代码如下: <?php $arr = array('one','two','three'); foreach ($arr as &$value){ echo 'Value:'.$value.'<br />'; } foreach ($arr as $value){ echo 'Value:'.$value.'<br />'; } ?>输出结果: Value:one V

*使用while循环遍历数组创建索引和自增索引值

package com.chongrui.test; /* *使用while循环遍历数组 *  *  * */ public class test { public static void main(String[] args) {        // TODO Auto-generated method stub           String[] aves = new String[]{"白路","丹顶鹤","百灵"};//创建鸟类数组  

javascript使用for循环批量注册的事件不能正确获取索引值的解决方法

今天遇到一个问题,那就是当使用for循环批量注册事件处理函数,然后最后通过事件处理函数获取当前元素的索引值的时候会失败,先看一段代码实例: <script type="text/javascript"> window.onload=function(){ var oLis=document.getElementsByTagName("li"); var oshow=document.getElementById("show"); fo

C#不允许在foreach循环中改变数组或集合中元素的值(注:成员的值不受影响)

C#不允许在foreach循环中改变数组或集合中元素的值(注:成员的值不受影响),如以下代码将无法通过编译. foreach (int x in myArray) { x++; //错误代码,因为改变了元素的值 Console.WriteLine(x); } 如果要让自定义的数据类型支持foreach循环,则该类型必须实现IEnumerable<T>接口,且存在对应此列表的IEnumerator<T>实现. 实际上,在.Net的底层(IL语言层面)而言, foreach (var

12 在Foreach循环中如何获得当前迭代的索引

static void ForEachIndex() { int curIndex = 0; Dictionary<string, string> diction = new Dictionary<string,string>(); int counter = 0; diction.Add("One", "One"); diction.Add("Two", "Two"); diction.Add(&qu

利用js输出ul下li的index索引值

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>打印索引</title> </head> <style type="text/css"> li{background: pink;margin-bottom: 10px;height: 40px;} </style> <body>

使用for循环批量注册的事件不能正确获取索引值

使用for循环批量注册的事件不能正确获取索引值:可能不少朋友会遇到一个问题,那就是当使用for循环批量注册事件处理函数,然后最后通过事件处理函数获取当前元素的索引值的时候会失败,先看一段代码实例: 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset=" utf-8"> 5 <meta name="author" content="http://www.

js循环给li绑定事件实现 点击li弹出其索引值 和内容

代码如下: html代码 <ul> <li>房产</li> <li>家居</li> <li>二手房</li> </ul> 方法一: var itemli = document.getElementsByTagName("li"); for(var i = 0; i<itemli.length; i++){ itemli[i].index = i; //给每个li定义一个属性索引值,赋

PHP中foreach循环详解

首先要说的是,其实我对foreach循环的用法并不是很精通,说详解,其实也只是我自己的理解,希望对你能有点帮助 . 先来看一下foreach的语法: foreach ($array as $key=>$value) { …… } 为了便于理解,我们假定这里的$array是一个一维的相关数组,$key是数组的索引,$value是这个索引的值,它们的名字可以随意,之所以叫$key和$value是为了便于理解.为了能让你更好的理解foreach的工作过程,我们来创建一个数组: $array = arr