Java 使用Arraylist值创建哈希映射数组
所以我正在为我的大学Java II课程做最后一个项目,我有了一个制作游戏开发者物品清单的想法。基本思想是,一个子类,比如说武器超类的枪子类,首先允许开发人员为他们正在制作的游戏制作该类的特定对象。然后,他们将能够获取他们创建的对象并将其存储在ItemInventory类中,该类将保留该特定对象的所有详细信息(即,对于枪对象,其类型、弹匣大小、损坏等),并将其存储在该类型的集合中。我创建ItemInventory的想法是创建一个HashMaps数组。HashMap将分别包含一个字符串weaponType形式的键(比如我制作了一把枪,并将类型字段指定为“狙击步枪”)和一个ArrayList形式的值。这样我就可以遍历数组,找到与我创建的对象匹配的武器类型,并将其存储在该武器类型的特定ArrayList中Java 使用Arraylist值创建哈希映射数组,java,arrays,arraylist,collections,hashmap,Java,Arrays,Arraylist,Collections,Hashmap,所以我正在为我的大学Java II课程做最后一个项目,我有了一个制作游戏开发者物品清单的想法。基本思想是,一个子类,比如说武器超类的枪子类,首先允许开发人员为他们正在制作的游戏制作该类的特定对象。然后,他们将能够获取他们创建的对象并将其存储在ItemInventory类中,该类将保留该特定对象的所有详细信息(即,对于枪对象,其类型、弹匣大小、损坏等),并将其存储在该类型的集合中。我创建ItemInventory的想法是创建一个HashMaps数组。HashMap将分别包含一个字符串weaponT
/**
* A class to make an in-game firearm object
*
* @author Matt
* @version 12/4/2020
*/
public class Gun extends WeaponClass
{
//field declarations
private int rounds;
private String rateOfFire;
private int magazine;
* Constructor
public Gun(String weaponName, String weaponType, double damageAmt, String rarity, int rounds, String rateOfFire, int magazine){
super(weaponName, weaponType, damageAmt, rarity);
this.rounds = rounds;
this.rateOfFire = rateOfFire;
this.magazine = magazine;
}
* Method to assign the number of rounds for the firearm
public void setRounds(int rounds){
this.rounds = rounds;
}
* Method to assign the rate of fire for the fireArm
public void setRateOfFire(String rateOfFire){
this.rateOfFire = rateOfFire;
}
* Method to assign the magazine size of the firearm
public void setMagSize(int magazine){
this.magazine = magazine;
}
* Method to print info about the firearm
public void printGunInfo(){
System.out.println(this);
System.out.println();
}
/*------------------------Accessor Methods-----------------------------*/
* Method to get the number of rounds
public int getRounds(){
return rounds;
}
* Method to get the rate of fire of the weapon
public String getRateFire(){
return rateOfFire;
}
* Method to return the magazine size of the weapon
public int getMagazine(){
return magazine;
}
* Method to override toString()
@Override
public String toString(){
String fireArmString = super.toString() + "\nRounds: " + rounds +
"\nRate of Fire: " + rateOfFire + "\nMagazine Size: " + magazine;
return fireArmString;
}
}
上面显示了gun子类的类代码,这并没有问题
import java.util.HashMap;
import java.util.ArrayList;
import java.util.List;
/**
* An item inventory
*
* @author Matt
* @version 12/14/2020
*/
public class ItemInventory
{
//field Declarations
private HashMap<String, ArrayList<WeaponClass>>[] weapons;
* Constructor
public ItemInventory(){
weapons[7] = new HashMap<>();
}
* Method to set up the weaponArray
public void setWeaponAray(){
weapons[0] = new HashMap<String, ArrayList<WeaponClass>>();
weapons[0].put("Sniper Rifle", ArrayList("SniperList"));
}
}
import java.util.HashMap;
导入java.util.ArrayList;
导入java.util.List;
/**
*物品清单
*
*@作者马特
*@version 12/14/2020
*/
公共类项目清单
{
//字段声明
私人武器;
*建造师
公共物品清单(){
武器[7]=新HashMap();
}
*建立武器阵列的方法
公共武器{
武器[0]=新HashMap();
武器[0]。放置(“狙击步枪”,阵列列表(“狙击手列表”);
}
}
这显示了我的库存情况。现在它不像put语句那样编译,它说它找不到符号方法ArrayList(java.lang.String)。我想知道的是,我在这里使用item inventory类是否正确?还是有更好的方法?如有任何其他建议,将不胜感激。
非常感谢。:)
另外,这是我第一次在这里发帖,如果我出现任何格式或礼仪错误/诸如此类的问题,我深表歉意
编辑:编译好了,但不知道是否能用
/**
* Method to set up the weaponArray so objects can be
* added to the correct list
*/
public void setWeaponAray(){
weapons[0] = new HashMap<String, ArrayList<WeaponClass>>();
weapons[0].put("Sniper Rifle", new ArrayList<WeaponClass>());
weapons[1] = new HashMap<String, ArrayList<WeaponClass>>();
weapons[1].put("Shotgun", new ArrayList<WeaponClass>());
weapons[2] = new HashMap<String, ArrayList<WeaponClass>>();
weapons[2].put("Hand Gun", new ArrayList<WeaponClass>());
weapons[3] = new HashMap<String, ArrayList<WeaponClass>>();
weapons[3].put("Submachine Gun", new ArrayList<WeaponClass>());
weapons[4] = new HashMap<String, ArrayList<WeaponClass>>();
weapons[4].put("Katana", new ArrayList<WeaponClass>());
weapons[5] = new HashMap<String, ArrayList<WeaponClass>>();
weapons[5].put("Club", new ArrayList<WeaponClass>());
weapons[6] = new HashMap<String, ArrayList<WeaponClass>>();
weapons[6].put("Knife", new ArrayList<WeaponClass>());
}
/**
*方法来设置武器阵列,以便可以
*添加到正确的列表中
*/
公共武器{
武器[0]=新HashMap();
武器[0]。放置(“狙击步枪”,新ArrayList());
武器[1]=新HashMap();
武器[1]。放置(“猎枪”,新ArrayList());
武器[2]=新的HashMap();
武器[2]。放置(“手枪”,新ArrayList());
武器[3]=新HashMap();
武器[3]。放置(“冲锋枪”,新ArrayList());
武器[4]=新HashMap();
武器[4]。放置(“Katana”,新ArrayList());
武器[5]=新HashMap();
武器[5]。放置(“俱乐部”,新ArrayList());
武器[6]=新HashMap();
武器[6]。放置(“刀”,新ArrayList());
}
我没有仔细看,我看到了一些不小的细节(你真的需要回到你的书中去),但是你得到的错误“找不到symbol-method-ArrayList(java.lang.String)
”恰恰是,ArrayList
类没有一个包含1个元素的构造函数,你必须执行新建ArrayList()
然后添加元素。。。在这种情况下,它也会失败,因为您正在声明ArrayList
具有WeaponClass
元素,并且您正在尝试添加String
元素/对象/实例(“狙击手列表”
)@AleZalazar,因此它更像是武器[0]。put(“狙击手步枪”,new ArrayList()代码>?此外,如果您不介意指出上述一些非次要的细节,我很乐意返回并修复它们,因为我只是尝试开始工作,现在仍然有点粗糙;添加(新枪(…此处有所有参数));武器[0]。放置(“狙击步枪”,列表)代码>我没有仔细看,我看到了一些不小的细节(你真的需要回到你的书中),但是你得到的错误“找不到符号-方法ArrayList(java.lang.String)
”就是,ArrayList
类没有带1个元素的构造函数,你必须执行新建ArrayList()
然后添加元素。。。在这种情况下,它也会失败,因为您正在声明ArrayList
具有WeaponClass
元素,并且您正在尝试添加String
元素/对象/实例(“狙击手列表”
)@AleZalazar,因此它更像是武器[0]。put(“狙击手步枪”,new ArrayList()代码>?此外,如果您不介意指出上述一些非次要的细节,我很乐意返回并修复它们,因为我只是尝试开始工作,现在仍然有点粗糙;添加(新枪(…此处有所有参数));武器[0]。放置(“狙击步枪”,列表)代码>