Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.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 理解作业中的一些代码_Java - Fatal编程技术网

Java 理解作业中的一些代码

Java 理解作业中的一些代码,java,Java,我正在学习java,我有一个练习,当我需要找到数组中每个数字的显示次数时,数字是0-100,这需要在O(n)中完成。现在我不知道怎么做,所以我看到了解决方案,并没有真正理解它,对解决方案的解释很差,下面是代码: public static void count (int [] data){ final int N = 100; int [] temp = new int[N]; int i; for (i=0; i < data.length; i++){

我正在学习java,我有一个练习,当我需要找到数组中每个数字的显示次数时,数字是0-100,这需要在O(n)中完成。现在我不知道怎么做,所以我看到了解决方案,并没有真正理解它,对解决方案的解释很差,下面是代码:

public static void count (int [] data){
    final int N = 100;
    int [] temp = new int[N];
    int i;
    for (i=0; i < data.length; i++){
        temp[data[i]]+=1;  
    }
    for (i=0; i < N; i++){
        if ( temp[i] != 0)
            System.out.println(i + ":" +temp[i]);
    }

}
如果有人能解释代码中每一行的意思,
我真的很感谢你的帮助。谢谢

基本上,该代码是这样做的: 1) 用100个位置初始化临时数组(全部设置为0); 2) 迭代数据数组,并相对于正在处理的数据数组值的索引增加值。举个例子:

int[]data={23,11,10,23,5};
临时[数据[0]]+=1
温度[23]+=1
温度[23]=温度[23]+1=0+1=1
温度[数据[1]]+=1
温度[11]+=1
温度[11]=温度[11]+1=0+1=1
温度[数据[2]]+=1
温度[10]+=1
温度[10]=温度[10]+1=0+1=1
温度[数据[3]]+=1
温度[23]+=1
温度[23]=温度[23]+1=1+1=2
温度[数据[4]]+=1
温度[5]+=1
温度[5]=温度[5]+1=0+1=1
在本例中,您会注意到值23在数据数组中出现两次,而其他值只出现一次。这样,您就可以计算O(n)中的出现次数,也就是说,只需遍历一次数组。

/*方法签名
/* method signature
static - method can be used without instantiation of the class
public - can be accessed by other classes
void - the return of the function count.
int [] data - parameter of the function. This function receives an array of int
*/

public static void count (int [] data){
    final int N = 100;
    int [] temp = new int[N];
    int i;

 //for i = 0 until i < size of the array passed as parameter, increment 1 in i

    for (i=0; i < data.length; i++){

/*
each iteration of this for increments 1 in the array temp at the position informed by the value of parameter data in position i.
caution: this can bring serious problems of segmentation fault, in other words, you can try to store in a position of the array temp that doesn't exist.
This will happen because you cannot guarantee the number that will be store in data[i]. If data[i] > 99 (because N = 100), you will face a segmentation fault.
*/

        temp[data[i]]+=1;  
    }

//here you will print the value stored at temp array in position i. From 0 to 99   

    for (i=0; i < N; i++)
    {
            if ( temp[i] != 0)
                System.out.println(i + ":" +temp[i]);
    }

}

HOPE IT HELPS.

You need to study more. Your question is so naive.
静态-方法可以在不实例化类的情况下使用 公共-可由其他类访问 void-返回函数计数。 int[]data—函数的参数。此函数接收一个int数组 */ 公共静态无效计数(int[]数据){ 最终整数N=100; int[]temp=新的int[N]; int i; //对于i=0,直到i<作为参数传递的数组的大小,在i中增加1 对于(i=0;i99(因为N=100),您将面临分段错误。 */ 温度[数据[i]]+=1; } //在这里,您将打印存储在位置i的临时数组中的值,从0到99 对于(i=0;i
解释是什么?你从中不明白什么?有道理。非常感谢。
/* method signature
static - method can be used without instantiation of the class
public - can be accessed by other classes
void - the return of the function count.
int [] data - parameter of the function. This function receives an array of int
*/

public static void count (int [] data){
    final int N = 100;
    int [] temp = new int[N];
    int i;

 //for i = 0 until i < size of the array passed as parameter, increment 1 in i

    for (i=0; i < data.length; i++){

/*
each iteration of this for increments 1 in the array temp at the position informed by the value of parameter data in position i.
caution: this can bring serious problems of segmentation fault, in other words, you can try to store in a position of the array temp that doesn't exist.
This will happen because you cannot guarantee the number that will be store in data[i]. If data[i] > 99 (because N = 100), you will face a segmentation fault.
*/

        temp[data[i]]+=1;  
    }

//here you will print the value stored at temp array in position i. From 0 to 99   

    for (i=0; i < N; i++)
    {
            if ( temp[i] != 0)
                System.out.println(i + ":" +temp[i]);
    }

}

HOPE IT HELPS.

You need to study more. Your question is so naive.