How can I create an ArrayList with a starting index of 1 (instead of 0)

后端 未结 8 738
春和景丽
春和景丽 2021-01-04 09:47

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

(Note that I am asking for ArrayList

8条回答
  •  盖世英雄少女心
    2021-01-04 10:16

    Well, you could make your own:

    public class MyArrayList extends ArrayList {
        public T get(int index) {
            super.get(index - 1);
        }
    
        public void set(int index, T value) {
            super.set(index - 1, value);
        }
    }
    

    But it begs the question: why on earth would you bother?

提交回复
热议问题