# Arrays in ruby and java

**URL:** <https://www.ruby-forum.com/t/arrays-in-ruby-and-java/138285>\
**Category:** Ruby\
**Created:** [May 21, 2008, 5:21pm UTC](https://www.ruby-forum.com/t/arrays-in-ruby-and-java/138285 "2008-05-21T17:21:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zzhang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/b9e5f3/32.png) [@zzhang](https://www.ruby-forum.com/u/zzhang)\
**Post date:** [May 21, 2008, 5:21pm UTC](https://www.ruby-forum.com/t/arrays-in-ruby-and-java/138285/1 "2008-05-21T17:21:27Z")

</div>

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):

def self.reproduce(a, b)  
data\_size = @@costs[0].length  
available = []  
0.upto(data\_size-1) { |n| available \<\< n }  
token = a.data[0]  
spawn = [token]  
available.delete(token)  
while available.length \> 0 do  
#Select next  
if token != b.data.last &&  
available.include?(b.data[b.data.index(token)+1])  
next\_token = b.data[b.data.index(token)+1]  
elsif token != a.data.last &&  
available.include?(a.data[a.data.index(token)+1])  
next\_token = a.data[a.data.index(token)+1]  
else  
next\_token = available[rand(available.length)]  
end  
#Add to spawn  
token = next\_token  
available.delete(token)  
spawn \<\< next\_token  
a, b = b, a if rand \< 0.4  
end  
return Chromosome.new(spawn)  
end

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.

thanks in advance

here is my java code:

public void reproduce(int even, int uneven){  
int dataSize = DataConverter.costs.length;  
LinkedList available = new LinkedList();  
for(int i=0; i\<dataSize; i++){  
available.add(i);  
}  
int token = toBreed.get(even).seed.get(0);  
ArrayList spawn = new ArrayList();// TODO correct  
spawn?  
int counter = 0;  
/_token =_/ available.remove(token);// TODO correct?  
int last = toBreed.get(uneven).seed.size()-1;// TODO whats that?  
while(available.size()\>0){  
int nextToken;  
int hold = toBreed.get(uneven).seed.indexOf(token)+1;  
System.out.println("hold "+hold);

```
  int containU = toBreed.get(uneven).seed.get(hold);
  int containE =

```

toBreed.get(even).seed.get(toBreed.get(even).seed.indexOf(token)+1);  
if(token != toBreed.get(uneven).seed.get(last) &&  
available.contains(containU)){  
nextToken = containU;  
}else{  
if(token!= toBreed.get(even).seed.get(last) &&  
available.contains(containE)){  
nextToken = containE;  
}else{  
nextToken =  
available.get((int)Math.round(Math.random()\*available.size()));  
}  
}  
System.out.println(counter+": “+token+”; "+ nextToken );  
// add to spawn  
token = nextToken;  
Integer rToken = new Integer(token);

```
  available.remove(rToken);
  spawn.add(nextToken);
  counter +=1;
  if(Math.random()<0.4){
    int holder = even;
    even = uneven;
    uneven = holder;
  }
}
Chromosome spawnCh = new Chromosome(spawn);
offspring.add(spawnCh);

```

}

---

<div class="post-metadata">

**Author:** ![zzhang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/b9e5f3/32.png) [@zzhang](https://www.ruby-forum.com/u/zzhang)\
**Post date:** [May 21, 2008, 5:42pm UTC](https://www.ruby-forum.com/t/arrays-in-ruby-and-java/138285/2 "2008-05-21T17:42:39Z")

</div>

2008/5/21 Zhe Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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

---

<div class="post-metadata">

**Author:** ![zzhang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/b9e5f3/32.png) [@zzhang](https://www.ruby-forum.com/u/zzhang)\
**Post date:** [May 21, 2008, 7:08pm UTC](https://www.ruby-forum.com/t/arrays-in-ruby-and-java/138285/3 "2008-05-21T19:08:46Z")

</div>

thank you for the reply.

if i got u right, an array in ruby doesnt throw an exception, but just  
gives back a null/nil?

---

<div class="post-metadata">

**Author:** ![zzhang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/b9e5f3/32.png) [@zzhang](https://www.ruby-forum.com/u/zzhang)\
**Post date:** [May 22, 2008, 6:59am UTC](https://www.ruby-forum.com/t/arrays-in-ruby-and-java/138285/4 "2008-05-22T06:59:51Z")

</div>

Zhe Z. wrote:

> thank you for the reply.
> 
> if i got u right, an array in ruby doesnt throw an exception, but just  
> gives back a null/nil?

Yep
