将List从一种方法用于另一种方法

时间:2017-05-30 09:33:39

标签: java robotframework

private List<String> add() {
    List<String> strlist = new ArrayList<String>();
    return strList;
}

public void methodOne() {
    List<String> strList = this.add();
}

public void methodtwo() {
    // need to use the list in methodOne.
}

我有一个私有方法返回List。我通过在add()中调用methodOne()方法并存储了List值来执行该methodTwo()方法。现在,我需要在add()方法中使用该列表而不执行methodOne()方法或List。 方法只是RobotFrame工作中的关键字

是否可以在Ride中创建Method One()变量并存储来自Method Two()的列表并在`<div class="col-xs-12" *ngFor="let data of homeData; let i = index"> <div class="row"> <div class="col-xs-12"> <form (ngSubmit)= "updateAddress()"> <div class="form-group"> <input type="text" class="form-control" value="{{data.companyAddress.address}}" name="address{{i}}" [(ngModel)]="address"/> </div> <div class="form-group"> <input type="text" class="form-control" value="{{data.companyAddress.city}}" name="city{{i}}" [(ngModel)]="city"/> </div> <div class="form-group"> <input type="text" class="form-control" value="{{ data.companyAddress.companyName }}" name="companyName{{i}}" [(ngModel)]="companyName"/> </div> <div class="form-group"> <input type="text" class="form-control" value="{{data.companyAddress.country}}" name="country{{i}}" [(ngModel)]="country"/> </div> <div class="form-group"> <input type="text" class="form-control" value="{{data.companyAddress.zipCode}}" name="zipCode{{i}}" [(ngModel)]="zipCode"/> </div> <button type="submit" class="btn btn-default">Submit</button> </form> </div> </div> 中使用?

4 个答案:

答案 0 :(得分:2)

创建类级别列表,以便当方法一执行它时,它会填充类级别变量,以便您可以在methodTwo()中使用它

答案 1 :(得分:1)

public class YourClass{

    private List<String> yourList;

    private List<String> add(){
        List<String> strlist=new ArrayList<String>();
        return strList;
    }

    public void methodOne(){
        yourList=this.add();
    }

    public void methodtwo(){
        // here go with yourList variable.
    }

}

答案 2 :(得分:0)

修改methodOne()以便它返回该列表而不是void

public List<String> methodOne() {
    List<String> strList = this.add(); 
    .... something else....
    return strList;
}

public void methodtwo() {
    methodOne(); // here you get a ref to strList...
}

答案 3 :(得分:0)

public void methodOne(){
    List<String> strList=this.add();
}

使用上面的代码,你不能在methodOne的任何其他位置使用strList ,因为它是一个本地列表,其范围不在此方法之外。

你还剩2个选项,

Option1:
public List<String> methodOne(){
    List<String> strList=this.add();
    // do SomeOperation on strList because that's why methodOne is there else you can directly call add method from methodTwo()
    return strList;
}

public void methodtwo(){
  List<String> myLocalList = methodOne(); // Now you have the list :)
}


Option2: Use Instance level list but it's not a good practice