Java 如何计算ArrayList中介于特定数字之间的项目数

Java 如何计算ArrayList中介于特定数字之间的项目数,java,sorting,arraylist,Java,Sorting,Arraylist,标题几乎说明了一切。我创建了一个ArrayList,里面的变量都是0到100之间的整数。我需要告诉你这些数字中有多少在0-49、50-59、60-69、70-79和80-100之间int count=0; int count = 0; for(int i = 0; i < myList.size(); i++){ if(myList.get(i) > val1 && myList.get(i) < val2) count ++ } 对于

标题几乎说明了一切。我创建了一个ArrayList,里面的变量都是0到100之间的整数。我需要告诉你这些数字中有多少在0-49、50-59、60-69、70-79和80-100之间

int count=0;
int count = 0;
for(int i = 0; i < myList.size(); i++){
    if(myList.get(i) > val1 && myList.get(i) < val2)
        count ++
}
对于(int i=0;ival1&&myList.get(i)
以下是一个示例,它应该会让您走上正确的道路

int count80to100=0; //Keeping track of other ranges is left as an exercise
for(Integer value:list) //use an enhanced for loop (for convenience)
{
    int i = value.intValue(); //get the value
    if(i>=80 && i<=100) //check if it's in the right range
        count80to100++; //increment the counter
}
int count80to100=0//跟踪其他范围是一项练习
for(整数值:list)//使用增强的for循环(为了方便)
{
int i=value.intValue();//获取值

如果(i>=80&&i我假设您开始学习Java,这不是您的家庭作业问题。如果这是真的,那么您可以使用以下方法获取您的值:

public int count(List<Integer> nums, int startValue, int endValue) {
    int counter = 0;
    for(Integer num: nums) {
        if((num.intValue()>=startValue) && (num.intValue()<=endValue))
            counter++;
    }
    return counter;
}
public int count(列出num、int startValue、int endValue){
int计数器=0;
for(整数num:nums){

如果((num.intValue()>=startValue)&&(num.intValue())这可能行得通,我该怎么打印出来?例如,JTextBox.setText(xxxxxx+“”)?如果使用JFrames,这可能行得通。否则,一个简单的
System.out.println(count80to100);
就足够了。