如何创建起始索引为 1(而不是 0)的 ArrayList

编程入门 行业动态 更新时间:2024-10-25 04:26:51
本文介绍了如何创建起始索引为 1(而不是 0)的 ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

如何在 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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-16 10:49:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/885021.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不是   索引   ArrayList

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!