java中的“指针”

  • Post author:
  • Post category:java


java中的“指针”

通常我们说java中没有指针,但是java中的“引用”就相当于指针,只是不称为指针而已。

错误例子

public List<Cluster> getClusters() {
        int num = 3;
        String[] names = {"g1","g2","g3"};
        int[] firstGroupIds = {1,101,201};
        int[] lastGroupIds = {100,200,300}
        List<Cluster> list = new ArrayList<Cluster>();
        Cluster c = new Cluster();
        for (int i = 0; i < num; i++) {
            c.setClusterId(i + 1);
            c.setClusterName(names[i]);
            c.setFirstGroupId(firstGroupIds[i]);
            c.setLastGroupId(lastGroupIds[i]);
            list.add(c);
        }
        return list;
    }

结果list中并没有入预期所料,而是为如下值:

list(0)=list(1)=list(2)={“g3”,201,300}

改正

public List<Cluster> getClusters() {
        int num = 3;
        String[] names = {"g1","g2","g3"};
        int[] firstGroupIds = {1,101,201};
        int[] lastGroupIds = {100,200,300}
        List<Cluster> list = new ArrayList<Cluster>();
        
        for (int i = 0; i < num; i++) {
            Cluster c = new Cluster();
            c.setClusterId(i + 1);
            c.setClusterName(names[i]);
            c.setFirstGroupId(firstGroupIds[i]);
            c.setLastGroupId(lastGroupIds[i]);
            list.add(c);
        }
        return list;
    }

list(0)={“g1”,1,100}

list(1)={“g2”,101,200}

list(2)={“g3”,201,300}

转载于:https://www.cnblogs.com/seven7seven/p/4184041.html