我的以下代码中的方法“ update()”有问题。我试图将数组用作“ hyp”数组的元素,因为各个数组的大小并不相同。
现在在更新功能中,我想比较存储在各个数组的位置0中的元素,例如sky [0],在hyp的第j个位置,以及trainingexample数组中的相应元素。
我的问题是我只能访问hyp数组的第j个位置中每个数组的地址。我试图将第j个位置的数组(即hyp [j])分配给Object []类型的变量,但这不起作用。我应该只使用多维数组,即使其中包含空元素,还是有比我在此尝试的解决方案更好的解决方案?
public class FindS {
static Object[] sky = {"Sunny", "Cloudy", "?", "0", 0};
static Object[] airtemp = {"Warm", "Cold", "?", "0", 0};
static Object[] humidity = {"normal", "high", "?", "0", 0};
static Object[] wind = {"strong", "weak","?", "0",0};
static Object[] water = {"warm", "cold","?", "0", 0};
static Object[] forecast = {"same", "change","?", "0", 0};
static Object[] hyp = {sky,airtemp,humidity,wind, water, forecast};
public static String[] trainingexample = new String[7];
public static int findindex(Object[] a, String s){
int index = 0;
while (index != a.length - 1){
if (a[index] == s)
return index;
index++;
}
return -1;
}
public static void exchange(Object[] a, String s){
int exchindex = findindex(a, s);
try{
Object temp = a[exchindex];
a[exchindex] = a[0];
a[0] = temp;
} catch (ArrayIndexOutOfBoundsException e)
{
System.out.println(s + "not found");
}
}
public void update(){
if (trainingexample[6] == "0");
else{
int j = 0;
while ( j != hyp.length){
Object[] temp = hyp[j];
if (temp[0] == trainingexample[j]);
}
}
}
最佳答案
最好将hyp
定义为多维数组。
static Object[][] hyp = {sky,airtemp,humidity,wind, water, forecast};
然后您的作业将起作用:
Object[] temp = hyp[j];
或者,但不太清楚,您可以将
hyp[j]
强制转换为Object[]
,但我不建议这样做。Object[] temp = (Object[]) hyp[j];