gpt4 book ai didi

java - 构造函数中的 String[] 或 ArrayList?

转载 作者:行者123 更新时间:2023-11-29 09:36:51 33 4
gpt4 key购买 nike

我必须为 ArrayList 创建一个构造函数。所以基本上我想要的是这个构造函数生成一个 ArrayList。

    public Recipe(String[] modifications){

The problem is, it should give something like(example):

String[] modifications = [heatup, cooldown, mix, heatup]

构造器会这样工作吗:

    Recipe(heatup, cooldown, mix, heatup)

或者我需要将其更改为类似的东西:

    Recipe(modifications){
this.myArrayList[1] = modifications[1]
.
.
.
}

感谢您,如果我的代码中存在一些大错误,我深表歉意,仍在学习我的 Java 代码。

最佳答案

你可以这样定义它:

public Recipe(String... modifications)

这将为您提供一个可变参数列表,因此您的构造函数将像这样工作:Recipe(heatup, cooldown, mix)

在构造函数内部,您可以像使用数组一样使用它。

关于java - 构造函数中的 String[] 或 ArrayList?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10596289/

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