当前位置:网站首页>Arrays Aslist uses bug

Arrays Aslist uses bug

2022-06-22 17:12:00 Seven days at night

After using the code scanning tool , Found code

obj.andIn("id", Arrays.asList(arr));

Prompt here BUG,BUG The message is :

Bug: int[] passed to varargs method java.util.Arrays.asList(Object[]) in com.vs.api.impl.VsMpExaminationPaperServiceImpl.changePublish(Integer, Integer)

This code passes a primitive array to a function that takes a variable number of object arguments. This creates an array of length one to hold the primitive array and passes it to the function.

Rank: Scary (7), confidence: High
Pattern: VA_PRIMITIVE_ARRAY_PASSED_TO_OBJECT_VARARG
Type: VA, Category: CORRECTNESS (Correctness)

After in-depth understanding, we found that :

Use new ArrayList<>(Arrays.asList(arr)) The way ,Arrays.asList() The output value of is passed to ArrayList Constructor , Then a reference will be created arr Of elements in the array ArrayList, So the results of the disruption will not be synchronized to arr Array to . If used directly Arrays.asList(arr) Result , The results will be synchronized to arr Array . Lead to arr The elements in the array will also be scrambled .

原网站

版权声明
本文为[Seven days at night]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221522489576.html