Java关于数组对象赋值与指针

在实现PageRank算法中犯了两个错误,

1:在对double类型赋值时,除法中没有将分母设置为double类型,而是int类型,导致出现商为0的结果错误,导致推迟

2:当定义两个数组时,对数组赋值时,忘记了,数组是对象的特点直接nowRank=resultRank;

其中resultRank又重新赋值,所以导致nowRank中元素值也发生变化,因为数组是对象,赋值意味着指向了同一个对象

代码:

public class TestDouble {

    public static void main(String[] args) {
        int count=2;
        double d=0.25;
        double value=d/count;
        System.out.println(value);
        int[] now=new int[2];
        int[] result=new int[2];
        now[0]=3;
        now[1]=4;
        result[0]=1;
        result[1]=2;
        //
        now=result;
        //
        for (int i = 0; i < now.length; i++) {
            System.out.println(now[i]+" "+result[i]);
        }
        for (int i = 0; i < result.length; i++) {
            now[i]=result[i];
        }
        for (int i = 0; i < result.length; i++) {
            System.out.println("r:"+now[i]);
        }
    }
}
原文地址:https://www.cnblogs.com/csxf/p/3586817.html