class Program
{
static void Main(string[] args)
{
IndexClass names = new IndexClass();
names[] = "Zara";
names[] = "Riz";
names[] = "Nuha";
names[] = "Asif";
names[] = "Davinder";
names[] = "Sunil";
names[] = "Rubic";
// 使用带有 int 参数的第一个索引器
for (int i = ; i < IndexClass.size; i++)
{
Console.WriteLine(names[i]);
}
// 使用带有 string 参数的第二个索引器
Console.WriteLine(names["Nuha"]);
Console.ReadKey(); }
}
class IndexClass
{
private string[] namelist = new string[size];
public static int size = ;
//// --构造函数赋值
public IndexClass()
{
for (int i = ; i < size; i++)
{
namelist[i] = "N. A.";
} }
//重载索引
public string this[int index]
{
get
{
string tmp; if (index >= && index <= size - )
{
tmp = namelist[index];
}
else
{
tmp = "";
} return (tmp);
}
set
{
if (index >= && index <= size - )
{
namelist[index] = value;
}
}
}
public int this[string name]
{
get
{
int index = ;
while (index < size)
{
if (namelist[index] == name)
{
return index;
}
index++;
}
return index;
} } }

索引的重载 str["name"] str[i]的更多相关文章

  1. 算法:Manacher,给定一个字符串str,返回str中最长回文子串的长度。

    [题目] 给定一个字符串str,返回str中最长回文子串的长度 [举例] str="123", 1 str="abc1234321ab" 7 [暴力破解] 从左 ...

  2. 为什么字符串String是不可变字符串&&"".equals(str)与str.equals("")的区别

    为什么字符串String是不可变字符串 实际上String类的实现是char类型的数组 虽然说源码中设置的是private final char[] value; final关键词表示不可变动 但是只 ...

  3. 003_python的str切片,str常用操作方法,for循环,集合,深浅copy

    基础数据类型 基础数据类型,有7种类型,存在即合理. 1.int 整数 主要是做运算的 .比如加减乘除,幂,取余  + - * / ** %... 2.bool布尔值 判断真假以及作为条件变量 3.s ...

  4. "".equals(str)和str.equals("")的区别

    如果当str为null的话 "".equals(str)不会报空指针异常,而str.equals("")会报异常.这种方式主要用来防止空指针异常

  5. Python: 字符串开头或结尾匹配str.startswith(),str.endswith()

    问题 需要通过指定的文本模式去检查字符串的开头或者结尾,比如文件名后缀,URLScheme 等等. 解决方案 1.检查字符串开头或结尾的一个简单方法是使用str.startswith() 或者是str ...

  6. PyCharm启动报错 TypeError: unsupported operand type(s) for /: ‘str’ and ‘str’ 解决

    这个提示大概是说:"类型错误:不支持操作类型为字符串和字符串",直接把两个字符串(BASE_DIR = os.path.dirname(os.path.dirname(os.pat ...

  7. TypeError: unsupported operand type(s) for |: 'str' and 'str'

    问题描述:

  8. python unicode to str and str to unicode

    @staticmethod def unicode2str(p_unicode): v = p_unicode.encode('unicode-escape').decode('string_esca ...

  9. string str将str转字符数组以及字符数组初始化

    c和指针(<pointers on c>) 8.1.11 一个字符串,如"hello",一般为字符串常量,可以用它对字符指针赋值,或初始化,相当于把这个字符串常量的首地 ...

随机推荐

  1. httpd-2.2 配置及用法完全攻略

    导读 apache是一款稳定的流行的web软件,是linux操作系统中默认的web管理软件.在RHEL/Centos系列中可以用rpm直接进行安装,服务名为httpd.apache有很多设置和调优 的 ...

  2. Android 数据存储之 SharedPreferences储存

    ------------------------------------------SharedPreferences存储--------------------------------------- ...

  3. Android架构:用消息机制获取网络数据

    网络请求,不管是什么协议,是长连接还是短连接,总是一个异步的请求,过程包括:加请求参数->发起请求->接收响应->解析数据->获得业务数据. 最挫的做法是,业务代码包揽所有这些 ...

  4. 架构Android App总结

    历时两个多月,自己架构的一个App快要完成了,有很多可以总结的地方: 1, 各个模块尽可能独立,不要直接调用,用消息机制解耦.包括页面跳转不要直接startActivity,而是用消息跳转:业务模块请 ...

  5. gcc编译时对'xxxx'未定义的引用问题

    gcc编译时对’xxxx’未定义的引用问题 gcc编译时对’xxxx’未定义的引用问题 原因 解决办法 gcc 依赖顺序问题 在使用gcc编译的时候有时候会碰到这样的问题,编译为.o(obj) 文件没 ...

  6. Java读写文件通用格式

    String path = "I:\\"; File file = new File(path + "user_id_pair.txt"); FileReade ...

  7. Javaweb Servlet出现Class xxx is not a servlet错误原因

  8. 交叉编译alsa声卡驱动

    變異成靜態 ./configure --target=arm-linux --enable-shared=no --enable-static=yes 編譯成動態 ./configure --targ ...

  9. 网页百度地图API相关资料

    百度地图API——网页URI接口.手机网页点击直接导航:js生成一个地图网页 或 直接跳转到百度导航界面 http://developer.baidu.com/map/index.php?title= ...

  10. maven package

    maven package test包下执行test 的配置文件 生成target目录,编译.测试代码,生成测试报告,生成jar/war文件 maven 配置文件详解 http://blog.csdn ...