gpt4 book ai didi

java - 如何替换数组中的静态值

转载 作者:行者123 更新时间:2023-12-02 09:39:25 25 4
gpt4 key购买 nike

如何替换数组中的静态值?我希望用户的第一个输入放置在第一个索引处。

Scanner input = new Scanner(System.in);

int[] arr = {0,0,0,0,0};

int seat = input.nextInt();

if(seat >=1 || seat <=5)

{

//how can i assign first input to index 0 permanently?

}

最佳答案

根据定义,数组是可变的。您始终可以为任何索引 arr[index] = newValue 分配新值。您不能将数组设置为只读。

但是,有些只读列表不允许在初始化后更改它们,例如参见 java.util.Collections.unmodifiableList(<list>) .

我不知道有任何默认实现来锁定列表的特定索引。为此,您可能需要自己实现 List界面。

关于java - 如何替换数组中的静态值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57220919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com