在Java中是否有一种方法可以迭代具有类似名称的对象?

huangapple go评论75阅读模式
英文:

Is there a way to iterate through objects with similar names in Java?

问题

SeekBar[] burpeeSelectors = new SeekBar[5];
Boolean[] burpeeSwitchStatus = new Boolean[5];
int[] pushupValues = new int[5];

// Initialize the SeekBars and switch statuses here

public void updateTotals() {
    for (int i = 0; i < 5; i++) {
        if (burpeeSwitchStatus[i]) {
            pushupValues[i] = burpeeSelectors[i].getProgress();
        } else {
            burpeeTotal -= setsSeekBar.getProgress() * burpeePrimer[i].getProgress();
        }
    }
    
    //...
}

在这段代码中,我使用了数组来替代了之前的多个变量,这样可以使代码更加简洁和可维护。通过使用循环,你可以在一个代码块中处理所有的 SeekBar 和开关状态。这种方法避免了重复的代码块,使得代码更加优雅和易于理解。

英文:

I have some code that works perfectly, but it seems like there's a more elegant way to do the same thing. Here's the code:

SeekBar burpeeSelector1;
SeekBar burpeeSelector2;
SeekBar burpeeSelector3;
SeekBar burpeeSelector4;
SeekBar burpeeSelector5;

Boolean burpeeSwitch1Status = true;
Boolean burpeeSwitch2Status = true;
Boolean burpeeSwitch3Status = true;
Boolean burpeeSwitch4Status = true;
Boolean burpeeSwitch5Status = true;

public void updateTotals(){

	int pushupValue1 = 0;
    int pushupValue2 = 0;
    int pushupValue3 = 0;
    int pushupValue4 = 0;
    int pushupValue5 = 0;
	
	if(burpeeSwitch1Status){
        pushupValue1 = burpeeSelector1.getProgress();
    } else{
        burpeeTotal-=setsSeekBar.getProgress()*burpeePrimer1.getProgress();
    }

    if(burpeeSwitch2Status){
        pushupValue2 = burpeeSelector2.getProgress();
    } else{
        burpeeTotal-=setsSeekBar.getProgress()*burpeePrimer2.getProgress();
    }

    if(burpeeSwitch3Status){
        pushupValue3 = burpeeSelector3.getProgress();
    } else{
        burpeeTotal-=setsSeekBar.getProgress()*burpeePrimer3.getProgress();
    }

    if(burpeeSwitch4Status){
        pushupValue4 = burpeeSelector4.getProgress();
    } else{
        burpeeTotal-=setsSeekBar.getProgress()*burpeePrimer4.getProgress();
    }

    if(burpeeSwitch5Status){
        pushupValue5 = burpeeSelector5.getProgress();
    } else{
        burpeeTotal-=setsSeekBar.getProgress()*burpeePrimer5.getProgress();
    }
	
	//...
	
}

How would you make this more elegant?

My idea to simplify the code is to replace the booleans with an array and replace the ints with an array, then use a for loop to run the algorithm that gets repeated on each boolean. However, though each chunk of code does the same thing, each chunk also references different objects (burpeeSelector1, burpeeSelector2, etc.) Is there some syntax trick that will allow me to abstract this out so I can use my for loop idea?

答案1

得分: 1

可以创建一个类

class burpeeSelector{
public boolean status;
public int value;
public SeekBar seekBar;

burpeeSelector(int value,boolean status,SeekBar seekBar){
this.status = status;
this.value= value;
this.seekBar= seekBar;
}
}


然后创建一个ArrayList并像这样初始化它:

ArrayList burpeeSelectorList = new ArrayList<>();
burpeeSelectorList.add(new burpeeSelector(0,true,burpeeSelector1));
burpeeSelectorList.add(new burpeeSelector(0,true,burpeeSelector2));
.
.
.


然后使用for循环访问这些值。

<details>
<summary>英文:</summary>

you can create class : 

class burpeeSelector{
public boolean status;
public int value;
public SeekBar seekBar;

burpeeSelector(int value,boolean status,SeekBar seekBar){
this.status = status;
this.value= value;
this.seekBar= seekBar;
}
}


then create ArrayList and initialize it like this;

ArrayList<burpeeSelector> burpeeSelectorList = new ArrayList<>();
burpeeSelectorList.add(new burpeeSelector(0,true,burpeeSelector1));
burpeeSelectorList.add(new burpeeSelector(0,true,burpeeSelector2));
.
.
.


then use forLoop to access to values

</details>



huangapple
  • 本文由 发表于 2020年10月19日 04:43:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/64418162.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定