gpt4 book ai didi

Java - ArrayList 项转换为每个字符串并传递给另一个方法

转载 作者:行者123 更新时间:2023-12-02 03:28:12 24 4
gpt4 key购买 nike

这是我的方法,它有两个带有 arrayList 的参数

private static PriorityInfo getPrioritiesInfo(ArrayList priorityNumber ,ArrayList priorityDate) {
PriorityInfo priorityInfo = new PriorityInfo();
ArrayList temp = new ArrayList();
for(int i=0;i<priorityNumber.size();i++){
priorityInfo.setPriorityNo(toString(priorityNumber.get(i)));
}

//priorityInfo.setPriorityDate(priorityDate);
return priorityInfo;
}

我的预期:

在priorityNumber数组列表中有n个项目(即786975,293AS45),我想将每个项目传递给setPriorityNo方法

上述方法产生错误

method toString in class Object cannot be applied to given types;
required: no arguments
found: Object
reason: actual and formal argument lists differ in length

setPriorityNo 方法来自 PriorityInfo.class:

 public void setPriorityNo(String priorityNo) {
this.priorityNo = priorityNo;
}

让我知道我的错误

最佳答案

您没有正确转换为字符串。您指的是 Object 类中的 toString 方法。

尝试这样的事情:

private static PriorityInfo getPrioritiesInfo(ArrayList priorityNumber, ArrayList priorityDate) {
PriorityInfo priorityInfo = new PriorityInfo(new ArrayList());
ArrayList temp = new ArrayList();
for(int i=0;i<priorityNumber.size();i++){
priorityInfo.setPriorityNumbers(priorityNumber.get(i).toString());
}

//priorityInfo.setPriorityDate(priorityDate);
return priorityInfo;
}

对您传递的 ArrayListpriorityNumber 调用 .toString() 方法。

和您的 PriorityInfo 类:

class PriorityInfo {

private ArrayList priorityNumbers;

public PriorityInfo(ArrayList priorityNumbers) {
this.priorityNumbers = priorityNumbers;
}

public void setPriorityNumbers(String priorityNo) {
this.priorityNumbers.add(priorityNo);
}

public ArrayList getPriorityNumbers() {
return this.priorityNumbers;
}

}

不知道您的 PriorityInfo 类中还有哪些其他方法。

关于Java - ArrayList 项转换为每个字符串并传递给另一个方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38496612/

24 4 0