我有一个类型为PolyaUrn
的对象,名为myUrn
。我实例化它并使用一些方法来操纵它。然后,我得到名为int[]
的{{1}}的{{1}}字段。我将此myUrn
传递给构造函数,以创建名为ballCounts
的类型int[]
的新对象。但是,当我尝试在SimpleUrn
上调用方法时,会得到su
。我正在使用su
关键字来创建类型为NullPointerException
的对象,并指向变量new
。我不明白为什么我不能使用解引用运算符SimpleUrn
su
以下代码:
su
.
类:
import java.util.Arrays;
class PolyaUrnProg {
public static void main(String[] args) {
PolyaUrn myUrn = new PolyaUrn();
myUrn.draw();
myUrn.draw();
System.out.println(myUrn);
System.out.println();
int numDraws = 100; // we will make another 98 draws from the urn
for (int i = 0 ; i < numDraws-2 ; i++) {
myUrn.draw();
}
System.out.println("...and after 98 more draws:\n");
System.out.println(myUrn);
System.out.println('\n');
System.out.println("Let's construct a SimpleUrn from original PolyaUrn.");
// initialising new int array with the same size as myUrn
int[] a = new int[myUrn.getArray().length];
// copying myUrn into 'a'
a = Arrays.copyOf(myUrn.getArray(), myUrn.getArray().length);
// passing 'a' to constructor in SimpleUrn to initialize it
SimpleUrn su = new SimpleUrn(a);
//ERROR - NullPointException
su.toString();
}
}
PolyaUrn
类:
import java.util.Arrays;
class PolyaUrn {
private int[] ballCounts;
private int lastDraw;
public PolyaUrn() { // Sets up PolyaUrn by rolling it
ballCounts = new int[2]; // initially two possibilities
ballCounts[0] = 1;
ballCounts[1] = 1;
// last draw is not a meaningful value until a ball is drawn.
lastDraw = -1;
}
public int[] getArray() {
return ballCounts;
}
public int getLastDraw() {
return lastDraw;
}
public int[] updateUrn(int b) {
if(b == 0) {
int[] newUrn = Arrays.copyOf(ballCounts, ballCounts.length+1);
newUrn[newUrn.length-1] = 1;
ballCounts = newUrn;
} else {
ballCounts[b] = ballCounts[b]+1;
}
return ballCounts;
}
public void draw() {
int ballDrawn = WeightedSampler.sample(ballCounts);
updateUrn(ballDrawn);
lastDraw = ballDrawn;
}
public String toString() {
String s = "Polya Urn\n=========\n\n";
s += "Ball-counts are as follows:\n\n";
s += " Value\t| Count\n";
s += "----------------\n";
for (int ballValue = 0; ballValue < ballCounts.length ; ballValue++) {
s += " " + ballValue + "\t| " + ballCounts[ballValue] + "\n";
}
return s;
}
}
注意:这些类使用第三类SimpleUrn
,以防有人要编译。此类在这里:
import java.util.Arrays;
class SimpleUrn {
private int[] ballCounts;
private int lastDraw;
public SimpleUrn(int[] ballArray) { // Sets up PolyaUrn by rolling it
int[] ballCounts = ballArray;
// last draw is not a meaningful value until a ball is drawn.
lastDraw = -1;
}
public int getLastDraw() {
return lastDraw;
}
public int[] updateUrn(int b) {
ballCounts[b] = ballCounts[b]-1;
return ballCounts;
}
public void draw() {
int ballDrawn = WeightedSampler.sample(ballCounts);
updateUrn(ballDrawn);
lastDraw = ballDrawn;
}
public String toString() {
String s = "Polya Urn\n=========\n\n";
s += "Ball-counts are as follows:\n\n";
s += " Value\t| Count\n";
s += "----------------\n";
for (int ballValue = 0; ballValue < ballCounts.length ; ballValue++) {
s += " " + ballValue + "\t| " + ballCounts[ballValue] + "\n";
}
return s;
}
}
答案 0 :(得分:4)
您在ballCounts
构造函数中 shadingwing SimpleUrn
。改变这个
public SimpleUrn(int[] ballArray) { // Sets up PolyaUrn by rolling it
int[] ballCounts = ballArray;
// last draw is not a meaningful value until a ball is drawn.
lastDraw = -1;
}
收件人
public SimpleUrn(int[] ballArray) { // Sets up PolyaUrn by rolling it
this.ballCounts = ballArray;
// last draw is not a meaningful value until a ball is drawn.
lastDraw = -1;
}