问题:
How do I convert an array to a list in Java? 如何在Java中将数组转换为列表?
I used the Arrays.asList()
but the behavior (and signature) somehow changed from Java SE 1.4.2 (docs now in archive) to 8 and most snippets I found on the web use the 1.4.2 behaviour. 我使用了Arrays.asList()
但是其行为(和签名)从Java SE 1.4.2 (现在已存档的文档)以某种方式更改为8,并且我在网络上发现的大多数代码片段都使用1.4.2行为。
For example: 例如:
int[] spam = new int[] { 1, 2, 3 };
Arrays.asList(spam)
- on 1.4.2 returns a list containing the elements 1, 2, 3 在1.4.2上返回包含元素1,2,3的列表
- on 1.5.0+ returns a list containing the array spam 在1.5.0+上返回包含数组垃圾邮件的列表
In many cases it should be easy to detect, but sometimes it can slip unnoticed: 在许多情况下,它应该很容易检测到,但是有时它可能会被忽略而不会被注意到:
Assert.assertTrue(Arrays.asList(spam).indexOf(4) == -1);
解决方案:
参考一: https://stackoom.com/question/AwH3/将数组转换为Java中的列表参考二: https://oldbug.net/q/AwH3/Converting-array-to-list-in-Java
来源:oschina
链接:https://my.oschina.net/u/3797416/blog/4317153