[英]Using arrays as elements of arrays
我在以下代码中对方法“ update()”存在问题。 我试图将数组用作“ hyp”数组的元素,因为各个数组的大小并不相同。 现在,在更新函数中,我想将存储在各个数组的位置0中的元素(例如,在hyp的第j个位置的sky [0])与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];
特定索引j的hyp将返回Object。
static Object[] hyp = {sky,airtemp,humidity,wind, water, forecast};
因此,更改Object[] temp = hyp[j];
到Object temp = hyp[j];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.