Java 算法,第四版:不理解关于别名/引用的示例
类别代码链接:Java 算法,第四版:不理解关于别名/引用的示例,java,reference,aliasing,Java,Reference,Aliasing,类别代码链接: 公共类计数器实现可比较的{ 私有最终字符串名称;//计数器名称 私有final int maxCount;//最大值 私有整数计数;//当前值 //使用给定参数创建一个新计数器 公共计数器(字符串id,int max){ 姓名=身份证; 最大计数=最大值; 计数=0; } //将计数器增加1 公共空间增量(){ 如果(计数
公共类计数器实现可比较的{
私有最终字符串名称;//计数器名称
私有final int maxCount;//最大值
私有整数计数;//当前值
//使用给定参数创建一个新计数器
公共计数器(字符串id,int max){
姓名=身份证;
最大计数=最大值;
计数=0;
}
//将计数器增加1
公共空间增量(){
如果(计数<最大计数)计数++;
}
//返回当前计数
公共int值(){
返回计数;
}
//返回此计数器的字符串表示形式
公共字符串toString(){
返回名称+“:”+计数;
}
//根据计数比较两个计数器对象
公共整数比较(计数器){
if(this.countthat.count)返回+1;
否则返回0;
}
//测试客户端
公共静态void main(字符串[]args){
int n=Integer.parseInt(args[0]);
int trials=Integer.parseInt(args[1]);
//创建n个计数器
计数器[]点击次数=新计数器[n];
对于(int i=0;i
这本书说,它将打印“2个一”,流程如上图所示。
但是我不能得到它。在我看来,c1加起来,它的对象加起来,所以我们得到“2”;然后将c1复制到c2,c2也得到“2”。随着c2的添加,对象将转向未知的下一个网格。
打印c1时,我认为我们应该得到“2”而不是“2”。那么,我的流程有什么问题?
提前谢谢
public class Counter implements Comparable<Counter> {
private final String name; // counter name
private final int maxCount; // maximum value
private int count; // current value
// create a new counter with the given parameters
public Counter(String id, int max) {
name = id;
maxCount = max;
count = 0;
}
// increment the counter by 1
public void increment() {
if (count < maxCount) count++;
}
// return the current count
public int value() {
return count;
}
// return a string representation of this counter
public String toString() {
return name + ": " + count;
}
// compare two Counter objects based on their count
public int compareTo(Counter that) {
if (this.count < that.count) return -1;
else if (this.count > that.count) return +1;
else return 0;
}
// test client
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
int trials = Integer.parseInt(args[1]);
// create n counters
Counter[] hits = new Counter[n];
for (int i = 0; i < n; i++) {
hits[i] = new Counter(i + "", trials);
}
// increment trials counters at random
for (int t = 0; t < trials; t++) {
int index = StdRandom.uniform(n);
hits[index].increment();
}
// print results
for (int i = 0; i < n; i++) {
StdOut.println(hits[i]);
}
}
}
我认为这个演示应该只显示引用。
因为您只创建了一个计数器类型的对象。
将c1的值赋给变量(计数器)c2,然后使用方法。变量c2上的增量()将改变c1。
因为c2和c1都引用内存中的同一对象。
因此,对c1和c2的更改都会影响同一个对象。StdOut不是Java。这是什么书?@Michael它可能是为了演示而创建的一个助手类。无论如何,我们不能在不看到类的实现的情况下告诉您输出应该是什么。你会希望这本书是对的。谢谢,我会更新类码链接。@Michael“Algorithms,4th Edition”,这本书的网站:衷心感谢你的回答,我已经在问题中发布了类码。我明白你的意思,但我仍然无法理解结果。
public class Counter implements Comparable<Counter> {
private final String name; // counter name
private final int maxCount; // maximum value
private int count; // current value
// create a new counter with the given parameters
public Counter(String id, int max) {
name = id;
maxCount = max;
count = 0;
}
// increment the counter by 1
public void increment() {
if (count < maxCount) count++;
}
// return the current count
public int value() {
return count;
}
// return a string representation of this counter
public String toString() {
return name + ": " + count;
}
// compare two Counter objects based on their count
public int compareTo(Counter that) {
if (this.count < that.count) return -1;
else if (this.count > that.count) return +1;
else return 0;
}
// test client
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
int trials = Integer.parseInt(args[1]);
// create n counters
Counter[] hits = new Counter[n];
for (int i = 0; i < n; i++) {
hits[i] = new Counter(i + "", trials);
}
// increment trials counters at random
for (int t = 0; t < trials; t++) {
int index = StdRandom.uniform(n);
hits[index].increment();
}
// print results
for (int i = 0; i < n; i++) {
StdOut.println(hits[i]);
}
}
}
Counter c1 = new Counter("ones");
c1.increment();
Counter c2 = c1;
c2.increment();
StdOut.println(c1);