gpt4 book ai didi

java - Java 中的内存问题/错误

转载 作者:太空宇宙 更新时间:2023-11-04 13:01:38 24 4
gpt4 key购买 nike

下面是我用 Java 制作的冒险游戏的方法:

public static void characterDisperse(){

int wRand = (int)(Math.random())*wModifiers.length;
Item[] inv = new Item[1];
inv[0]=new Item(wModifiers[wRand] + " " + wNames[(int)(Math.random()*wNames.length)],(int)(Math.random()*wModifiers.length)*2,1);
for(int a=0;a<10;a++){
for(int b=0;b<10;b++){
if(Math.random()>0.5){ //density of characters
charTest[a][b] = new Character(names[(int)(Math.random()*names.length)],(int)(Math.random()*5),(int)(Math.random()*3),(int)(Math.random()*15+5)*10,2,inv);

map[a][b].chars[1] = charTest[a][b];
System.out.println(map[a][b].chars[1]);

}
else{
map[a][b].passable = false;
}


}
}
for(int d=0;d<10;d++){
for(int e=0;e<10;e++){
System.out.println(map[d][e].chars[1]);
}
}

}

我遇到的问题是,在第一个双“for”循环中,打印的每个字符都是具有各种不同属性的不同字符。但是,当第二个双“for”循环运行并打印出我刚刚创建的所有字符时,它会打印出所有相同的字符,如下所示:

NAME: Moriah    RACE: 4 ALIGNMENT: 0    HEALTH: 130 SKILL: 2    INVENTORY: 1
NAME: Marge RACE: 1 ALIGNMENT: 0 HEALTH: 160 SKILL: 2 INVENTORY: 1
NAME: Faith RACE: 0 ALIGNMENT: 1 HEALTH: 50 SKILL: 2 INVENTORY: 1
NAME: Morton RACE: 3 ALIGNMENT: 2 HEALTH: 60 SKILL: 2 INVENTORY: 1
NAME: Sherwood RACE: 1 ALIGNMENT: 2 HEALTH: 50 SKILL: 2 INVENTORY: 1
NAME: Ezequiel RACE: 2 ALIGNMENT: 1 HEALTH: 150 SKILL: 2 INVENTORY: 1
NAME: Herschel RACE: 2 ALIGNMENT: 2 HEALTH: 70 SKILL: 2 INVENTORY: 1
NAME: Lester RACE: 3 ALIGNMENT: 2 HEALTH: 80 SKILL: 2 INVENTORY: 1
NAME: Corinna RACE: 2 ALIGNMENT: 0 HEALTH: 190 SKILL: 2 INVENTORY: 1

FIRST FOR LOOP ENDS HERE
SECOND FOR LOOP BEGINS

NAME: Corinna RACE: 2 ALIGNMENT: 0 HEALTH: 190 SKILL: 2 INVENTORY: 1
NAME: Corinna RACE: 2 ALIGNMENT: 0 HEALTH: 190 SKILL: 2 INVENTORY: 1
NAME: Corinna RACE: 2 ALIGNMENT: 0 HEALTH: 190 SKILL: 2 INVENTORY: 1
NAME: Corinna RACE: 2 ALIGNMENT: 0 HEALTH: 190 SKILL: 2 INVENTORY: 1
NAME: Corinna RACE: 2 ALIGNMENT: 0 HEALTH: 190 SKILL: 2 INVENTORY: 1
NAME: Corinna RACE: 2 ALIGNMENT: 0 HEALTH: 190 SKILL: 2 INVENTORY: 1
NAME: Corinna RACE: 2 ALIGNMENT: 0 HEALTH: 190 SKILL: 2 INVENTORY: 1
NAME: Corinna RACE: 2 ALIGNMENT: 0 HEALTH: 190 SKILL: 2 INVENTORY: 1

我相信这是某种内存问题,我将map[a][b](位置对象)处的字符设置为charTest[a][b]中的字符。 charTest 已在代码的前面实例化。

map[][]是一个10x10的Location对象数组,charTest[][]是一个10x10的Character对象数组:

位置.java:

import java.awt.*;
public class Location{

Character[] chars;
Feature[] features;
boolean passable;

public Location(Character[] chars, Feature[] features, boolean passable){
this.chars = chars;
this.features = features;
this.passable = passable;
}

public String toString(){
return "test";
}

public boolean containsChars(Location l){
if(l.chars.length>0){
return true;
}
else{
return false;
}
}

}

字符.java:

public class Character{
String name;
int race;
int alignment;

int health;

int skill;

Item[] inventory;

public Character(String name,int race,int alignment,int health,int skill,Item[] inventory){
this.name = name;
this.race = race;
this.alignment = alignment;
this.health = health;
this.skill = skill;
this.inventory = inventory;
}


public String toString(){
String printOut = "NAME: " + name + "\tRACE: " + race + "\tALIGNMENT: " + alignment + "\tHEALTH: " + health + "\tSKILL: " + skill + "\tINVENTORY: " + "1";
return printOut;
}
}

最佳答案

在此行中,您必须将末尾的 inv 更改为 new Item[1]

charTest[a][b] = new Character(names[(int)(Math.random()*names.length)],(int)(Math.random()*5),(int)(Math.random()*3),(int)(Math.random()*15+5)*10,2,inv); 

所以这一行可以完成这项工作:

charTest[a][b] = new Character(names[(int)(Math.random()*names.length)],(int)(Math.random()*5),(int)(Math.random()*3),(int)(Math.random()*15+5)*10,2,new Item[1]);

原因是,当您使用此 Item[] inv = new Item[1]; 创建数组时,对此数组的引用将分配给变量 inv。然后,如果您将值 inv 分配给另一个变量,则您复制引用

这意味着在您的情况下,所有 Character 对象都具有相同的对象。

<小时/>

您显然也有同样的问题

map[a][b].chars[1]

当您创建map[a][b]时,您将相同的chars数组分配给map中的所有字段。

然后,每次创建新的 Character 时,都将其放入同一个 chars 数组中,该数组替换旧的并打印它。这就是为什么您在生成新字符时会看到新字符,这就是为什么所有 map 变量中的最后一个字符都是相同的。

<小时/>

当您为 map 生成初始数据时,您必须为每个位置创建新数组,如下所示:

for(int a=0;a<10;a++){
for(int b=0;b<10;b++){
map[a][b] = new Location(new Character[5], new Feature[5], true);
}
}

PS:我强烈建议使用 ArrayList,而不是数组。

关于java - Java 中的内存问题/错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34882467/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com