Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:如何使用方法计算ArrayList中的出现次数_Java_Arraylist - Fatal编程技术网

Java:如何使用方法计算ArrayList中的出现次数

Java:如何使用方法计算ArrayList中的出现次数,java,arraylist,Java,Arraylist,方法examples()将一个数字和一个整数数组作为其输入,并返回该数字在数组中出现的次数 例如: int[] arr = { 7, 3, 3, 6, 3, 3, 1, 5 }; System.out.println(appearances(3, arr)); // should print 4 since there are 4 3s 我首先要写的是我能够做到的。现在,我正试图找出如何使用ArrayList执行相同的操作 这就是我到目前为止所做的: import java.u

方法examples()将一个数字和一个整数数组作为其输入,并返回该数字在数组中出现的次数

例如:

    int[] arr = { 7, 3, 3, 6, 3, 3, 1, 5 };
    System.out.println(appearances(3, arr)); // should print 4 since there are 4 3s
我首先要写的是我能够做到的。现在,我正试图找出如何使用ArrayList执行相同的操作

这就是我到目前为止所做的:

import java.util.ArrayList;

public class Main {
    
    public static void main(String[] args) {
        int[] arr = { 7, 3, 3, 6, 3, 3, 1, 5 };
        ArrayList<Integer> arrList = new ArrayList<Integer>();
            for (int num : arr) { 
//this add the numbers from arr to arrList so pretty much making an ArrayList with the same numbers as the array
            arrList.add(num);
        }   
        System.out.println(appearances(3, arr)); 
        System.out.println(appearances(3, arrList)); 

        
    }


    public static int appearances(int datum, int[] nums) { 
//this is the method for find the occurrences for the array
          int count = 0;
            for (int i = 0; i < nums.length; i++) {
                if (datum == nums[i])
                  count++;
            }
            return count;

    }   
    
    

    public static int appearances(int datum, ArrayList<Integer> nums) {
//this is the method for finding the occurrences for ArrayList
//this part is what I am asking help for
        int count = 0;
        for (int i = 0; i < nums.size(); i++) {
            if(nums.contains(datum)); 
                count++;
            }
        
        return count;
        


    }

}


import java.util.ArrayList;
公共班机{
公共静态void main(字符串[]args){
int[]arr={7,3,3,6,3,3,1,5};
ArrayList arrList=新的ArrayList();
对于(int num:arr){
//这会将arr中的数字添加到arrList中,使ArrayList中的数字与数组中的数字相同
arrList.add(num);
}   
系统输出打印LN(外观(3,arr));
系统输出打印LN(外观(3,arrList));
}
公共静态整数外观(整数基准,整数[]nums){
//这是查找阵列引用的方法
整数计数=0;
对于(int i=0;i
对于ArrayList的方法,我得到的是8而不是4,所以我猜它是打印出ArrayList的长度。我缺少什么?

nums.contains(datum)
如果列表包含的元素等于
datum
,则返回
true
。不过,您需要检查每个元素

更改:

if(nums.contains(datum));  // Note: ";" should be deleted too!
致:

请注意,您有一个尾随的
这意味着您的
if
在其条件为
true
时不执行任何操作

但是,使用foreach循环更容易、更惯用:

for (int i : nums) {
    if (datum == i)
      count++;
}
当然还有基于流的单行版本:

return (int)nums.stream().filter(i -> i == datum).count();

正如前面的回答中提到的,您的
if
声明中有语法错误,这就是代码不能正常工作的原因。除此之外,我还建议使用Java 8
Stream
来完成在一行中计算
列表中出现的数字的任务:

public void main(字符串[]args){
int[]arr={7,3,3,6,3,3,1,5};
ArrayList arrList=新的ArrayList();
for(int num:arr){
arrList.add(num);
}
系统输出打印LN(外观(3,arrList));
}
专用静态长外观(int-datum、ArrayList nums){
返回nums.stream().filter(i->i.equals(datum)).count();
}

您可以使用collections静态方法,也可以使用stream API

public static int appearances(int datum, ArrayList<Integer> nums) {
int c1 = Collections.frequency(nums, datum);
int c2 = nums.stream().filter(x -> x==datum).count();
}
公共静态整数外观(整数基准、数组列表nums){
int c1=收集频率(nums,基准);
int c2=nums.stream().filter(x->x==datum.count();
}

我没听清你的问题,你能用length函数找不到吗不是语法错误:代码编译和运行时没有错误。它是一个没有主体的
if
,这意味着当
if
条件为true时,不会运行额外的代码。很难找,很有趣,我不知道。因此,当
if
语句返回
true
?否,不跳过时,将跳过方法中的其余代码。一切正常。想象缩进是错误的:
count++和下一个结束<代码>}
都应该超出1级以反映现实。
public static int appearances(int datum, ArrayList<Integer> nums) {
int c1 = Collections.frequency(nums, datum);
int c2 = nums.stream().filter(x -> x==datum).count();
}