gpt4 book ai didi

java - Java 中的 Arraylist 和 List

转载 作者:行者123 更新时间:2023-11-30 06:15:44 25 4
gpt4 key购买 nike

谁能解释一下下面代码的含义,尤其是 ArrayList 和 List 以及为什么我们需要同时使用两者?

String [] forecastArray =
{
"Today - Sunny - 35/30",
"Tomorrow - Foggy - 33/28",
"Wednesday - Cloudy - 33/26",
"Thursday - Sleepy - 30/24",
"Friday - Bunking - 36/34",
"Saturday - Trapped - 38/35",
"Sunday - Heavy Rain - 32/28"
};
List<String> myList = Arrays.asList(forecastArray);

List<String> weekForecast = new ArrayList<>(myList);

最佳答案

List是一个接口(interface)。 ArrayList是该接口(interface)的实现。一般建议对接口(interface)进行编程。这样你就可以切换到 LinkedList以后不要更改返回类型或其他代码。这是 polymorphism 的示例.

关于java - Java 中的 Arraylist 和 List,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28359456/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com