ArrayList转linkedList存取
时间: 2023-08-19 18:03:59 浏览: 62
将 ArrayList 转换为 LinkedList 的方法是使用 LinkedList 的构造函数,它接受一个 Collection 类型的参数。可以将 ArrayList 作为参数传递给 LinkedList 的构造函数,从而创建一个新的 LinkedList 对象。
例如,以下代码演示了如何将 ArrayList 转换为 LinkedList:
```
import java.util.ArrayList;
import java.util.LinkedList;
public class Example {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("One");
arrayList.add("Two");
arrayList.add("Three");
LinkedList<String> linkedList = new LinkedList<String>(arrayList);
// 访问 LinkedList 中的元素
System.out.println(linkedList.get(0));
System.out.println(linkedList.get(1));
System.out.println(linkedList.get(2));
}
}
```
在上面的示例中,我们首先创建了一个名为 `arrayList` 的 ArrayList,并将三个字符串添加到该列表中。然后,我们使用 LinkedList 的构造函数将 ArrayList 转换为 LinkedList。最后,我们通过调用 `get()` 方法访问 LinkedList 中的元素。
反过来,如果要将 LinkedList 转换为 ArrayList,则可以使用 ArrayList 的构造函数,它接受一个 Collection 类型的参数。可以将 LinkedList 作为参数传递给 ArrayList 的构造函数,从而创建一个新的 ArrayList 对象。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)