强力创建类并将其添加到数组中会导致奇怪的结果
我一直被代码的一种行为所困扰,我无法解释自己。我会发布相关代码,然后问问题:
//Uses brute force and existing functions to create and check for equilibria
static void getEquis(List<Integer> values, int depth) {
if (values.size() == depth) {
for(int i = 0; i < numOfAgents; i++) {
agents[i].setVote(values.get(i).intValue());
}
for(int i = 0; i < numOfAgents; i++) {
agents[i].setDoVote(true);
if(isEqui()==true) {
int votes[] = new int[numOfAgents];
boolean doVotes[] = new boolean[numOfAgents];
for(int j = 0; j < numOfAgents; j++) {
votes[j] = 5;
doVotes[j] = true;
//votes[j] = agents[j].getVote();
//doVotes[j] = agents[j].getDoVote();
}
addEqui(new Equi(votes, doVotes));
}
agents[i].setDoVote(false);
if(isEqui()==true) {
int votes[] = new int[numOfAgents];
boolean doVotes[] = new boolean[numOfAgents];
for(int j = 0; j < numOfAgents; j++) {
votes[j] = 5;
doVotes[j] = true;
//votes[j] = agents[j].getVote();
//doVotes[j] = agents[j].getDoVote();
}
addEqui(new Equi(votes, doVotes));
}
}
请注意,我留下了评论(投票[j]=agents[j].getVote();)在那里,正如它应该做的那样,投票[j]=5;下面这一行只是为了测试
private static class Equi
{
private int vote[] = new int[numOfAgents];
private boolean doVote[] = new boolean[numOfAgents];
public Equi(int[] votes, boolean[] doVotes)
{
for(int i = 0; i < numOfAgents; i++) {
this.vote[i] = votes[i];
this.doVote[i] = doVotes[i];
}
}
public int[] getVote() {
return this.vote;
}
public boolean[] getDoVote() {
return this.doVote;
}
public void print() {
for(int i = 0; i < numOfAgents; i++) {
System.out.println("Agent " + (i+1) + ": " + agents[i].doVote + " Wahl: " + agents[i].vote);
}
System.out.println("Solution: " + computeMean());
}
}
//Adds a new equilibrium to the equilibria array
public static void addEqui(Equi equi)
{
Equi store[] = new Equi[equilibria.length + 1];
for(int i = 0; i< equilibria.length; i++) {
store[i] = equilibria[i];
}
store[equilibria.length] = equi;
equilibria = store;
}
现在如果我打印出平衡矩阵的结果
Agent 1: false Wahl: 100 Agent 2: false Wahl: 100 Solution: 0.0 Agent 1: false Wahl: 100 Agent 2: false Wahl: 100 Solution: 0.0 Agent 1: false Wahl: 100 Agent 2: false Wahl: 100 Solution: 0.0 Agent 1: false Wahl: 100 Agent 2: false Wahl: 100 Solution: 0.0
这是通过在main中运行以下命令实现的:
for(int i = 0; i < equilibria.length; i++) {
equilibria[i].print();
}
在我的理解中,它应该总是给我5和真的,但这种行为对我来说是非常奇怪的。你对此有什么解释吗?如果你需要任何其他部分的代码,请评论,我会编辑和添加它
共 (0) 个答案