如何将Collection转换为ArrayList
将Collection转换为ArrayList,可以使用ArrayList的构造函数进行转换。代码示例如下:
Collection<String> collection = new ArrayList<>();
collection.add('Item 1');
collection.add('Item 2');
collection.add('Item 3');
ArrayList<String> arrayList = new ArrayList<>(collection);
在上述代码中,首先创建一个Collection对象,并向其中添加了几个元素。然后,通过ArrayList的构造函数将Collection转换为ArrayList。最终得到的arrayList就是一个ArrayList对象,包含了与原始collection相同的元素。
原文地址: https://www.cveoy.top/t/topic/oJk6 著作权归作者所有。请勿转载和采集!