简体   繁体   中英

How iterate a Groovy List with Java?

I have the following Groovy domain class:

class A {

  def lotOfBs = []

}

Now, from a Java class I need to iterate that array. These solutions did not work:

for ( B b : a.getLotOfBs() ){
  //COMPILATION ERROR
}

for ( int i = 0 ; i < a.getLotOfBs().length ; i++ ){
  //LENGTH ATTRIBUTE DOES NOT EXIST OR IT IS NOT VISIBLE
}

for ( int i = 0 ; i < a.getLotOfBs().size() ; i++ ){
  //SIZE METHOD DOES NOT EXIST
}

Do you have any suggestions?

Thanks in advance

The array in groovy class is an instance of java.util.ArrayList, so casting to Collection<T> should work:

  Collection<B> bs = (Collection<B>) a.getLotOfBs();

  for (B b : bs) {
    ...
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM