将数组转换为Java中的列表

断了今生、忘了曾经 提交于 2020-08-09 10:19:08

问题:

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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!