# java.lang.NegativeArraySizeException: -1

- **ID:** `java/negative-array-size-exception`
- **领域:** java
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

尝试创建大小为负数的数组，通常由整数溢出、大小计算错误或格式错误的输入数据引起。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Java 8 | active | — | — |
| Java 11 | active | — | — |
| Java 17 | active | — | — |
| Java 21 | active | — | — |

## 解决方案

1. ```
   Validate the array size before creation: if (size < 0) throw new IllegalArgumentException("Size must be non-negative: " + size);
   ```
2. ```
   Use Math.max(0, size) to clamp the size to a non-negative value as a temporary fix, but investigate the root cause.
   ```

## 无效尝试

- **** — The issue is not about memory limits but a negative value; increasing heap does not fix the logic error. (90% 失败率)
- **** — This masks the underlying bug, potentially leading to data corruption or silent failures. (80% 失败率)
