From: Robert Klemme Date: 2008-05-22T00:42:01+09:00 Subject: Re: arrays in ruby and java 2008/5/21 Zhe Zen : > hi, > > i have been translating an algorithm from ruby to java. unfortunately i > stuck with one line of code and would like to ask for some help. > > My problem is the following line: > > b.data[b.data.index(token)+1] > > and here is the context(which i not relevant in my opinion): > my problem is the following: if the array b.data has 10 elements, then > the "+1" could set it to position 10, so that b.data[10] would give back > an error, like the java.lang.IndexOutOfBoundsException, which I get in > Java, when translating. in ruby the code is working, in java obviously > not. could somebody give me a hint, where my error in reasoning is. I cannot see an error in your reasoning. Ruby's Array and Java's List just behave differently. As far as I can see you have at least these options: 1. create an implementation of java.util.List that does not throw but simply returns nil. 2. do a check in your code and if the index is out of bounds use null instead of list.get(n) 3. write a static helper method that implements option 2 Kind regards robert -- use.inject do |as, often| as.you_can - without end