【发布时间】:2021-01-03 17:50:13
【问题描述】:
我使用了其他一些答案来解决我的问题。但我想知道是否有办法进一步改进?
// Copy the masterList ArrayList and then sort in ascending order and then make a third
// ArrayList and loop through to add the 8 lowest values to this list.
ArrayList<Integer> sortedList = new ArrayList<>(Calculator.masterList);
Collections.sort(sortedList);
ArrayList<Integer> lowEight = new ArrayList<>();
for (int i = 0; i < 8; i++) {
lowEight.add(sortedList.get(i));
}
// Set TextView as the value of index 0 in masterList ArrayList, check if lowEight
// ArrayList contains the element that is the same as masterList index 0 and if
// so highlight s1 textview green.
s1.setText("Score 1 is " + String.format("%d", Calculator.masterList.get(0)));
if (lowEight.contains(Calculator.masterList.get(0))) {
s1.setBackgroundColor(Color.GREEN);
}
这在一定程度上通过突出显示masterList 和lowEight 中的值来起作用,但例如,如果数字7 在lowEight 中并且在masterList 中出现9 次,它将突出显示所有9 个出现。有没有办法将确切的对象从masterList 移动到sortedList,然后到lowEight,然后有一种方法来检查对象而不仅仅是值?
【问题讨论】:
标签: java android-studio arraylist