This site requires JavaScript, please enable it in your browser!
Greenfoot back
Mux
Mux wrote ...

2012/10/10

opperating with lists

Mux Mux

2012/10/10

#
I want to get the object taht is in one of the tow lists:
java.util.ArrayList hight;
        if(list == 1)
        {
            hight = hight1;
        }
        else if( list == 2)
        {
            hight = hight2;
        }
        
        Informationsträger intStart = hight.get(X);
hight1 and 2 are lists with objects with a listsice from arround 800. list is a variable with the number 1 or 2 to detect wich list should be used. This hole thing gives an compile error in the last line that says wrong type for X. X is an int-typ. what could be wrong ??
SPower SPower

2012/10/10

#
How is X declared? Are there other variables called X?
Mux Mux

2012/10/10

#
oh it's really miss leading. X is actually a variable like
int X = 0;
if( thing 1 happens)
{
      X = 1;
}
else if( thing 2 happens)
{
      X = 2;
}
else
{
      X = 3;
}
just like this. this all in the method in front of the first part of code i've posted. EDIT: so its a defined X but it's not what the list.get() thing needs but why ??? EDIT 2: all so the object in the list is a Informationsträger.class object
SPower SPower

2012/10/10

#
Can you post the specific error?
Mux Mux

2012/10/10

#
No ?? I don't know how. The error is just made by the compiler that marks the X red and tell nothing more than it's the wrong typ: exactly: incompatible types There was an expression of a certain type required here. You provide an expression of a different type that is not compatible. (E.g. you wrote a String where an int was expected.) EDIT: even when i put a number like 3 in there it doesn't work same problem thats it. And even if i write list.get( (int) X ) it doesn't work out. must there be something else instead ??
danpost danpost

2012/10/10

#
It might be the way you are trying to copy the arrays to another variable. Try the following, instead of all the code you initially posted (replace all 11 lines with this 1 line). If it works, then I was probably right.
Informationsträger intStart = list == 1 ? hight1.get(X) : height2.get(X);
Mux Mux

2012/10/10

#
i put your line of code in but it gives me the same mistake. also i don't understand anything of your line ^^ but that isn't the question. Same mistake. incompatible types EDIT: the following is red marked : height1.get(X) : height2.get(X)
danpost danpost

2012/10/10

#
Try this then
Informationsträger intStart = (Informationsträger) (list == 1 ? hight1.get(X) : hight2.get(X));
Mux Mux

2012/10/10

#
Got it and i know the mistake ^^:
Informationsträger I = (Informationsträger) ...
I was missing the second one. It's needed to declare the object that comes out of the list to be sure that it is what you want to save it in. Thanks for all the help. Just by the way i use my code for it again cause i don't understand anything of your line. But anyway like before thx for the really fast and nice answers.
danpost danpost

2012/10/10

#
The one liner I gave does exactly the same thing as your 11-liner at the beginning of the discussion; except that it does not reassign another variable to get the data from. The format condition ? opt1 : opt2 will return opt1 if the condition is true and opt2 if the condition is not true (false). So, in your case, if list is one, hight1.getX() will be returned, otherwise hight2.getX() will be returned; which is just what you wanted.
Mux Mux

2012/10/10

#
thanks for the information its much better to do the thing in your one line code thx there for
You need to login to post a reply.