feat: 实现数组自动扩容功能
- 在 SyscallCommand 类中的列表操作中增加了自动扩容逻辑 - 当访问的索引超过当前列表大小时,自动将列表大小扩展到该索引 - 这种实现允许在 append操作时直接使用 set 方法,而不需要额外判断
This commit is contained in:
parent
0c2a888e86
commit
35fdb25d27
@ -400,9 +400,20 @@ public class SyscallCommand implements Command {
|
|||||||
// 必须是可变 List
|
// 必须是可变 List
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
java.util.List<Object> mlist = (java.util.List<Object>) list;
|
java.util.List<Object> mlist = (java.util.List<Object>) list;
|
||||||
if (idx < 0 || idx >= mlist.size())
|
if (idx < 0) {
|
||||||
throw new IndexOutOfBoundsException("数组下标越界: " + idx + " (长度 " + mlist.size() + ")");
|
throw new IndexOutOfBoundsException("数组下标越界: " + idx + " (长度 " + mlist.size() + ")");
|
||||||
|
}
|
||||||
|
// 允许自动扩容:当 idx == size 时等价于 append;当 idx > size 时以 null 填充到 idx-1,再 set(idx, value)
|
||||||
|
if (idx >= mlist.size()) {
|
||||||
|
// 以 null 填充直到 size == idx
|
||||||
|
while (mlist.size() < idx) {
|
||||||
|
mlist.add(null);
|
||||||
|
}
|
||||||
|
// 此时 size == idx,直接追加
|
||||||
|
mlist.add(value);
|
||||||
|
} else {
|
||||||
mlist.set(idx, value);
|
mlist.set(idx, value);
|
||||||
|
}
|
||||||
} else if (arrObj != null && arrObj.getClass().isArray()) {
|
} else if (arrObj != null && arrObj.getClass().isArray()) {
|
||||||
int len = java.lang.reflect.Array.getLength(arrObj);
|
int len = java.lang.reflect.Array.getLength(arrObj);
|
||||||
if (idx < 0 || idx >= len)
|
if (idx < 0 || idx >= len)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user