Reputation: 83
void testK(ArrayList list) {
for (int y= list.size() ; y > 0 ; y-- ) {
Kostka kst = list.get(y -1);
}}
when I try to compile this code, it says that the (y -1)
(3rd line) is incompatible
list.size()
method should return an integer, so whats the problem ?Upvotes: 1
Views: 915
Reputation: 1264
Tip for enhancement : put the -1
a the beginning of the loop, so you avoid list.size()
substractions in the loop. If possible, keep Koska ksk
final.
for (int y = list.size() - 1; y > 0; y--) {
final Kostka kst = list.get(y);
}
Upvotes: 0
Reputation: 17622
First I suggest you to use generics for ArrayList
.
like
void testK(ArrayList<Kostka> list) {
for (int y= list.size() ; y > 0 ; y-- ) {
Kostka kst = list.get(y -1);
}}
or you need to cast the object got from list to your type Kostka
void testK(ArrayList list) {
for (int y= list.size() ; y > 0 ; y-- ) {
Kostka kst = (Kostka)list.get(y -1);
}}
Upvotes: 1
Reputation: 62864
In the way you have written your code, the get(y - 1)
will return an Object
instance.
You have to cast it:
Kostka kst = (Kostka) list.get(y -1);
Also, avoid using raw types like ArrayList
. Instead, use Generic collections (ArrayList<Kostka>
)
Upvotes: 1
Reputation: 9954
You either have to cast the result of list.get()
to your type
Kostka kst = (Kostka)list.get(y -1);
or work with generics and supply a generic list to your method
void testK(ArrayList<Kostka> list)
Upvotes: 7