如何替换数组中的静态值? 我希望用户的第一个输入放置在第一个索引处。
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/