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

时间:2016-07-21 06:34:22

标签: java arraylist

这是我的方法,它有两个带有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 arraylist中有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;
    }

让我知道我的错误

2 个答案:

答案 0 :(得分:2)

您没有正确转换为字符串。你指的是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;
}

在你传递的ArrayList priorityNumber上调用.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类中有哪些其他方法。

答案 1 :(得分:0)

我正在使用带有StringUtils的Apache Commons库

 private static PriorityInfo getPrioritiesInfo(ArrayList priorityNumber ,ArrayList priorityDate) {
        PriorityInfo priorityInfo = new PriorityInfo();
        String temp="";

        for (int i = 0; i < priorityNumber.size(); i++) {
            String value = StringUtils.join(priorityNumber.get(i), '|');
            temp = temp.concat(value);
        }
 priorityInfo.setPriorityNo(temp);       
        return priorityInfo;
    }