如何创建起始索引为 1(而不是 0)的 ArrayList [英] How can I create an ArrayList with a starting index of 1 (instead of 0)

查看:28
本文介绍了如何创建起始索引为 1(而不是 0)的 ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 ArrayList 中从 1 而不是 0 开始索引?有没有办法直接在代码中做到这一点?

How can I start the index in an ArrayList at 1 instead of 0? Is there a way to do that directly in code?

(请注意,我要求的是 ArrayList,对于普通数组,请参阅 在 c# 中的任意起始索引上初始化数组)

(Note that I am asking for ArrayList, for plain arrays please see Initializing an array on arbitrary starting index in c#)

推荐答案

显而易见的方法是将 ArrayList 包装在您自己的实现中,并在所有使用该索引的操作中从索引器中减去 1.

The obvious way to do it would be to wrap an ArrayList in your own implementation, and subtract 1 from the indexer in all operations that uses the index.

实际上,您也可以在 .NET 中声明一个数组,该数组具有 任意下限(向下滚动到创建具有非零下限的数组"部分).

You can in fact also declare an array in .NET, that has an arbitrary lower bound (Scroll down to the section "Creating Arrays with a Non-zero Lower Bound").

话虽如此,请不要在生产代码中这样做.保持一致很重要.

With that said, please don't do it in production code. It matters to be consistent.

这篇关于如何创建起始索引为 1(而不是 0)的 ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆