我有一个数组,我想检查数组中的所有值是否都等于我要使用edittext

时间:2019-03-08 11:14:44

标签: java android arraylist

这是我的数组的一个示例

List<Integer> sampleList = new ArrayList<>();

此数组内的所有值均为:

sampleList = [1,2,3,4,5,6]

基本上,我使用for来循环访问此数组中的每个值

for (int i = 0; i < sampleList.size(); i++) {
     int to = sampleList.get(i);
     if (norooms == to) { //norooms = 1
         //display a toast 
      }
  }

我想检查位置0-5是否都等于1,但我无法实现。任何帮助

4 个答案:

答案 0 :(得分:1)

您可以尝试这样的事情。

int editTextValue = 3; // Your edit text value
boolean isEqual = true;//Flag to check if all the values are equal or not.
 for (int i=0; i<sampleList.size(); i++){
        if (sampleList.get(i) != editTextValue){
            isEqual = false;
            break;
        } 
    }

然后在循环结束后检查 isEqual 的条件并进行相应处理。

答案 1 :(得分:0)

Arraylist<String> matchedValues = new Arraylist<>();
String editTextValue = edittext.getText();
String arrayValue;
boolean isEqual ;
boolean isEveryElementPresent = false;
for (int i = 0 ; i < sampleList.size() ; i++){
    arrayValue = sampleList.get(i);
    if(arrayValue.equals(editTextValue)){
        isEqual = true;
        matchedValues.add(arrayValue);
    }else{
        isEqual =false;
    }
}

if(matchedValues.size() == sampleList.size()){
        isEveryElementPresent = true;
}

答案 2 :(得分:0)

public Check {
  int value;
  int index;
  boolean match;
  // constructor, setters and getters
}

有关所需的所有信息:

public List<Check> checkValues(List<Integer> originalList, Integer testValue) {
  List<Check> result = new ArrayList<>();
  for ( int i = 0; i< originalList.size(); i++ ) {
    result.add(new Check(i, originalList.get(i), originalList.get(i) == testValue);
  }
  return result;
}

这是一种“方法”。您将获得有关有多少个匹配项,有哪些其他值以及在列表中找到它们的索引的信息。

答案 3 :(得分:0)

我将使用Java Streams,有关更多信息,请访问:Streams

基本上将列表变成元素流,然后就可以处理数据了。

//To get only the elements equal to 1
sampleList.stream()
  .filter((num) -> num == 1);

//to get a list of booleans whose value depends on the element being equal to 1.
List<Boolean> listBooleans = sampleList.stream()
  .map((num) -> num == 1)
  .collect(Collectors.toList())
  • 过滤器使用谓词确定元素是否符合特定条件,在本例中为number ==1。
  • map将整数流转换为布尔流,其结果为number ==1。
  • collect终止流,使用Collectors.toList将其转换为List。

希望这会有所帮助。