改写代码:请问下面的代码 怎么用循环或者遍历的方式改写呀
public TodayExamWidget() {
    self = binder.createAndBindUi(this);

    int row  =1 ;
    gridWidget = new Grid();
    gridWidget.resize(row,3);
    gridWidget.setSize("100","100");
    gridWidget.setText(0,0,"Uncorrected");
    gridWidget.addStyleName("gray_cell");
    gridWidget.setBorderWidth(1);
    gridWidget.getCellFormatter().setWidth(0,0,"160");
    gridWidget.getCellFormatter().setWidth(0,1,"60");
    gridWidget.getCellFormatter().setWidth(0,2,"60");

    visionFlowPanel.add(gridWidget);
}


    private void populateVisualAcuity(Grid gridWidget, VisualAcuityCollection visualAcuityCollection) {
    int rowCount = gridWidget.getRowCount();
    if(!visualAcuityCollection.normal.isEmpty()){

        gridWidget.resize(rowCount +1,3);
        gridWidget.setText(rowCount -2,0,VisualAcuityType.NORMAL.toString());
        gridWidget.setText(rowCount -2,1,visualAcuityCollection.normal.od.distance);
        gridWidget.setText(rowCount -2,2,visualAcuityCollection.normal.os.distance);
    }
    if(!visualAcuityCollection.pinhole.isEmpty()){
        gridWidget.resize(rowCount +1,3);
        gridWidget.setText(rowCount -2,0,VisualAcuityType.PINHOLE.toString());
        gridWidget.setText(rowCount -2,1,visualAcuityCollection.pinhole.od.distance);
        gridWidget.setText(rowCount -2,2,visualAcuityCollection.pinhole.os.distance);
    }
    if(!visualAcuityCollection.binocular.isEmpty()){
        gridWidget.resize(rowCount +1,3);
        gridWidget.setText(rowCount -2,0,VisualAcuityType.BINOCULAR.toString());
        gridWidget.setText(rowCount -2,1,visualAcuityCollection.binocular.od.distance);
        gridWidget.setText(rowCount -2,2,visualAcuityCollection.binocular.os.distance);
    }

    row = rowCount;
}

    主要是上面的那个popluteVisualVison 方法, 又没有什么方法让代码 用loop形式改写? 谢谢啦 各位大神门

3个回答

Java下的Lambda好丑陋。

能否循环,则要看 visualAcuityCollection.normal、visualAcuityCollection.pinhole 和 visualAcuityCollection.binocular 是否是相同的类型?
如果是,则可以考虑用一个变量来代替它们实现循环。循环内的代码也不一样,看看有没有什么规律吧,如: VisualAcuityType。

用lambda表达式可以做到
Function [] funcs = new Function<>[]{visualAcuityCollection.normal::isEmpty, visualAcuityCollection.pinhole::isEmpty, visualAcuityCollection.binacular::isEmpty};

for(Function func : funs) {
....
}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐