Java 无法从零件[]转换为零件
首先,让我解释一下我想做什么。在java中,我正在创建一个与我高中的一个cca程序相关的游戏,我正在用vb6制作(已经很好地使用了),但我只能在其中做些什么,所以我正在使用java。名为Parts的类的代码如下所示:Java 无法从零件[]转换为零件,java,type-mismatch,Java,Type Mismatch,首先,让我解释一下我想做什么。在java中,我正在创建一个与我高中的一个cca程序相关的游戏,我正在用vb6制作(已经很好地使用了),但我只能在其中做些什么,所以我正在使用java。名为Parts的类的代码如下所示: public class Parts implements Cloneable{ public static Parts[] PartsList = new Parts[4096]; public static HashMap<Integer,Boolean&
public class Parts implements Cloneable{
public static Parts[] PartsList = new Parts[4096];
public static HashMap<Integer,Boolean> ArrayList = new HashMap<Integer,Boolean>();
protected boolean PartConstructerCalled = false;
public int partTextureIndex;
public final int partID;
protected float partStat;
protected String partConversion;
private String partName;
/**
* To get everything started.
*/
public static void Init_Parts(){}
public Object clone() throws CloneNotSupportedException{
return super.clone();
}
/**
* Call this to create a new part.
* Game crashes if an id you give is already used.
* @param id Id to give to the part.
*/
public Parts(int id, int textureid)
{
this.PartConstructerCalled = true;
if(PartsList[id] != null){
Variables.HackLog.severe("Part id of " + id + " is already used.");
String partname = PartsList[id].getPartName();
if(partname != "Un-named"){
throw new IllegalArgumentException("id " + String.valueOf(id) + " was trying to take up part id of " + id + " which is already taken up by "+PartsList[id].getPartName() + ".");
}else{
throw new IllegalArgumentException("id " + String.valueOf(id) + " was trying to take up part id of " + id + " which is already used.");
}
}else{
this.partID = id;
this.partName = "Un-named";
this.partTextureIndex = textureid;
PartsList[id] = this;
ArrayList.put(id, false);
}
}
}
rarray
属于Parts[]
类型,它是Parts对象数组。您正在尝试将其转换为单个部分对象。你认为这是怎么做到的?您可以从数组中获取某些元素,并将其分配给某些部分
对象,例如部分列表[id]=rarray[0]
。但这只是一个例子,我真的不知道你想在这里分配什么。你有一个数组,你想要一个元素。。。那么,您想要哪个元素,以及您希望转换如何工作?为什么array
是一个数组?(这段代码确实不清楚您想要实现什么。)除了给出错误的代码外,这两段代码片段似乎是相同的。区别是什么?他的Parts[]rarray不是在if-else语句中吗?因此,当它存在时,该语句被销毁,因此rarray[0]不再存在了?哎呀,我的坏,好久没见了。Nevermind:)+1PartsList[id]=(Parts)rarray代码>也在else
中,比如Parts[]rarray=…
Nvm为了满足我的需要,我不得不将Parts[4096]的定义更改为Parts[4096][],或者是Parts.java中的Parts[4096][4096]
public class MainParts extends Parts{
public static Parts[] CPU = CreatePartArray(1,1,4,"CPU");
public static final float[] CPUStats = {1.28f,2.0f,3.36f,5.0f};
public static final Parts Memory = new MainParts(2,2).setPartName("Memory").setPartStat(10.0f).setPartConversion("GB");
public static final Parts Fan = new MainParts(3,3).setPartName("Fan").setPartStat(2.0f).setPartConversion("CPUs/Fan");
public static void Init_MainParts(){
Parts.Init_Parts();
setPartArrayStat(CPUStats,CPU);
setPartArrayConversion("GHz",CPU);
Variables.HackLog.info("Parts initialized.");
}
public MainParts(int id, int textureid) {
super(id, textureid);
}
/**
* This creates an array of a new part with the same id, still crashes if you create a single part or an array part with same id later on.
* @param id The id to give to new part
* @param texture The texture index to assign to part
* @param size The size of the array
* @param name To give to the array
* @return
*/
public static Parts[] CreatePartArray(int id, int texture,int size,String name)
{
if(PartsList[id] != null){
Variables.HackLog.severe("Part id of " + id + " is already used.");
String partname = PartsList[id].getPartName();
if(partname != "Un-named"){
throw new IllegalArgumentException("id " + String.valueOf(id) + " was trying to take up part id of " + id + " which is already taken up by "+PartsList[id].getPartName() + ".");
}else{
throw new IllegalArgumentException("id " + String.valueOf(id) + " was trying to take up part id of " + id + " which is already used.");
}
}else{
Parts temp = new MainParts(id,texture);
Parts[] array = new Parts[1];
Parts[] rarray = Arrays.copyOf(array, array.length + size);
rarray[1] = temp;
for(int i=2;i<rarray.length;i++){
try{
rarray[i]=(MainParts)temp.clone();
}catch(Exception e){
e.printStackTrace();
}
}
PartsList[id] = rarray[1]; //Here is what works but not what I wanted.
ArrayList.put(id, true);
setPartArrayName(name,rarray);
return rarray;
}
}
}
public class MainParts extends Parts{
public static Parts[] CPU = CreatePartArray(1,1,4,"CPU");
public static final float[] CPUStats = {1.28f,2.0f,3.36f,5.0f};
public static final Parts Memory = new MainParts(2,2).setPartName("Memory").setPartStat(10.0f).setPartConversion("GB");
public static final Parts Fan = new MainParts(3,3).setPartName("Fan").setPartStat(2.0f).setPartConversion("CPUs/Fan");
public static void Init_MainParts(){
Parts.Init_Parts();
setPartArrayStat(CPUStats,CPU);
setPartArrayConversion("GHz",CPU);
Variables.HackLog.info("Parts initialized.");
}
public MainParts(int id, int textureid) {
super(id, textureid);
}
/**
* This creates an array of a new part with the same id, still crashes if you create a single part or an array part with same id later on.
* @param id The id to give to new part
* @param texture The texture index to assign to part
* @param size The size of the array
* @param name To give to the array
* @return
*/
public static Parts[] CreatePartArray(int id, int texture,int size,String name)
{
if(PartsList[id] != null){
Variables.HackLog.severe("Part id of " + id + " is already used.");
String partname = PartsList[id].getPartName();
if(partname != "Un-named"){
throw new IllegalArgumentException("id " + String.valueOf(id) + " was trying to take up part id of " + id + " which is already taken up by "+PartsList[id].getPartName() + ".");
}else{
throw new IllegalArgumentException("id " + String.valueOf(id) + " was trying to take up part id of " + id + " which is already used.");
}
}else{
Parts temp = new MainParts(id,texture);
Parts[] array = new Parts[1];
Parts[] rarray = Arrays.copyOf(array, array.length + size);
rarray[1] = temp;
for(int i=2;i<rarray.length;i++){
try{
rarray[i]=(MainParts)temp.clone();
}catch(Exception e){
e.printStackTrace();
}
}
PartsList[id] = (Parts)rarray; //This is what I want it to look like but sadly, errors :(
ArrayList.put(id, true);
setPartArrayName(name,rarray);
return rarray;
}
}
}
Type mismatch: cannot convert from Parts[] to Parts