我有一个整数数组列表。我的要求是确定arraylist是否在指定索引处存在一个元素。如果是,则应将值设置为该索引(使用set方法),否则应将值添加到该索引位置(使用add方法)
在我的Java代码中发现很难处理上述条件。请提供帮助。
这是我到目前为止的内容:
ArrayList<Integer> tempArray = new ArrayList<Integer>();
int counter = 0;
int tempValue = 0;
For LOOP -
if (//certain conditions are satisfied){
tempValue = calculateNewValue();
tempArray.add(counter, tempValue); //Need some logic here to determine if its a set or add method to be used
}
if (//some other conditions are satisfied){
counter++;
}
end For LOOP
Run Code Online (Sandbox Code Playgroud)