This is your problem
for ( int i : file1 ){
change it to
for ( int i = 0; i< file1.size() ; i++ ){
This syntax
for ( int i : file1 ){
is giving you the iterated value of file1, meaning that if file1 = List([4,5,6])
then in the first iteration of the loop i == 4 not 0
.
Obviously this would apply to the second loop as well.
Alternatively you could change
for ( int i : file1 ){
int x = file1.get(i);
to
for ( int i : file1 ){
int x = i;
And it would fix your problem. Hope that helps.