From: James Carroll <jim@mi...> - 2003-06-11 13:26:03
I'm trying to do a slice on an ArrayList... (see below) What's the best
way to get just the first few elements of the list?
Why is it giving the error 'only integer keys accepted?'
And finally, what's your favorite Jython book so far for understanding
>>> al =3D java.util.ArrayList()
[one, two, three]
Traceback (innermost last):
File "<console>", line 1, in ?
TypeError: only integer keys accepted
From: brian zimmer <bzimmer@zi...> - 2003-06-11 14:02:16
> I'm trying to do a slice on an ArrayList... (see below) What's the best
> way to get just the first few elements of the list?
> Why is it giving the error 'only integer keys accepted?'
When a slice is requested of a sequence an instance of PySlice is passed to
the object's __finditem__ method. In the case of ListProxy, the wrapper for
ArrayList, it does not handle slices and as such you get a TypeError .
This should probably be reported as a bug to make it more compatible with
Given this deficiency, you can either turn the ArrayList instance into a
>>> al = list(al)
>>> print al[0:2]
or get the items one by one:
>>> for i in range(0, 2):
... print al[i]
Depending on your needs either one should work.