索引的重载 str["name"] str[i]
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]的更多相关文章
- 算法:Manacher,给定一个字符串str,返回str中最长回文子串的长度。
[题目] 给定一个字符串str,返回str中最长回文子串的长度 [举例] str="123", 1 str="abc1234321ab" 7 [暴力破解] 从左 ...
- 为什么字符串String是不可变字符串&&"".equals(str)与str.equals("")的区别
为什么字符串String是不可变字符串 实际上String类的实现是char类型的数组 虽然说源码中设置的是private final char[] value; final关键词表示不可变动 但是只 ...
- 003_python的str切片,str常用操作方法,for循环,集合,深浅copy
基础数据类型 基础数据类型,有7种类型,存在即合理. 1.int 整数 主要是做运算的 .比如加减乘除,幂,取余 + - * / ** %... 2.bool布尔值 判断真假以及作为条件变量 3.s ...
- "".equals(str)和str.equals("")的区别
如果当str为null的话 "".equals(str)不会报空指针异常,而str.equals("")会报异常.这种方式主要用来防止空指针异常
- Python: 字符串开头或结尾匹配str.startswith(),str.endswith()
问题 需要通过指定的文本模式去检查字符串的开头或者结尾,比如文件名后缀,URLScheme 等等. 解决方案 1.检查字符串开头或结尾的一个简单方法是使用str.startswith() 或者是str ...
- PyCharm启动报错 TypeError: unsupported operand type(s) for /: ‘str’ and ‘str’ 解决
这个提示大概是说:"类型错误:不支持操作类型为字符串和字符串",直接把两个字符串(BASE_DIR = os.path.dirname(os.path.dirname(os.pat ...
- TypeError: unsupported operand type(s) for |: 'str' and 'str'
问题描述:
- python unicode to str and str to unicode
@staticmethod def unicode2str(p_unicode): v = p_unicode.encode('unicode-escape').decode('string_esca ...
- string str将str转字符数组以及字符数组初始化
c和指针(<pointers on c>) 8.1.11 一个字符串,如"hello",一般为字符串常量,可以用它对字符指针赋值,或初始化,相当于把这个字符串常量的首地 ...
随机推荐
- python 函数式编程工具
有三个内置函数与列表一起使用时非常有用:filter().map()和reduce(). 1. filter(function, sequence)返回的序列由function(item)结果为真的元 ...
- OpenCV图像轮廓检测
轮廓检测: 轮廓检测的原理通俗的说就是掏空内部点,比如原图中有3*3的矩形点.那么就可以将中间的那一点去掉. 一.关键函数1.1 cvFindContours函数功能:对图像进行轮廓检测,这个函数将 ...
- (转)android中颜色矩阵colormatrix
原文地址:http://www.cnblogs.com/menlsh/archive/2013/02/03/2890888.html 在Android中,对图像进行颜色方面的处理,如黑白老照片.泛黄旧 ...
- [NOIP2015] 子串substring 题解
[题目描述] 有两个仅包含小写英文字母的字符串A和B.现在要从字符串A中取出k个互不重叠的非空子串,然后把这k个子串按照其在字符串A中出现的顺序依次连接起来得到一个新的字符串,请问有多少种方案可以使得 ...
- Flask 模板语言
Flask使用的是Jinja2模板引擎 举个例子: from flask import Flask, render_template app = Flask(__name__) @app.route( ...
- 1.6---旋转二维数组,旋转图像像素,旋转矩阵,90度(CC150)
import java.util.*; public class Transform { public int[][] transformImage(int[][] matrix, int n) { ...
- [转]Python的ASCII, GB2312, Unicode , UTF-8
2007-12-13 10:50:47| 分类: Python实用软件编|举报|字号 订阅 ASCII 是一种字符集,包括大小写的英文字母.数字.控制字符等,它用一个字节表示,范围是 0-1 ...
- linux系统vsftpd登陆慢卡怎么办
linux系统vsftpd登陆慢卡怎么办 浏览:145 | 更新:2013-12-31 00:50 vsftpd是linux系统中的一款ftp软件,用它可以实现文件,数据上传与下载,但有些用户会发现v ...
- Sublime Text编辑工具带有 PEP 8 格式检测插件
Sublime Text编辑工具带有 PEP 8 格式检测插件
- Linux下安装Django
Django是基于Python开发的免费的开源网站框架,也是python web开发中重量级的web框架,可以用于快速搭建高性能并且优雅的网站! 下面以Fedora为例安装Django,最新Fedor ...