Reputation: 1488
I have a problem with the method "update()" in the following code. I tried to to use arrays as elements of the array "hyp", since the individual arrays do not all have the same size. Now in the update function I want to compare the elements that are stored in position 0 of the individual arrays, e.g. sky[0], in the j-th position of hyp with the corresponding elements in the trainingexample array. The problem I have is that I can only access the address for each array in the j-th position of the hyp array. I tried to assign the array in the j-th position, i.e. hyp[j], to a variable of type Object[], but this did not work. Should I just use multidimensional arrays, even though there would be empty elements in it or is there a better solution than what I'm trying to do here?
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]);
}
}
}
Upvotes: 1
Views: 74
Reputation: 178263
It would be best to define hyp
as a multidimensional array.
static Object[][] hyp = {sky,airtemp,humidity,wind, water, forecast};
Then your assignment would work:
Object[] temp = hyp[j];
Alternatively, but less clearly, you could just cast hyp[j]
as Object[]
, but I wouldn't recommend it.
Object[] temp = (Object[]) hyp[j];
Upvotes: 2
Reputation: 4923
The hyp at particular index j will return Object.
static Object[] hyp = {sky,airtemp,humidity,wind, water, forecast};
So change Object[] temp = hyp[j];
to Object temp = hyp[j];
Upvotes: 0