Finding all RShape children recursively (Geomerative)

Tried a few different things and came up with this function:

RShape[] getAllChildren(RShape shp) {
  ArrayList<RShape> result = new ArrayList<RShape>();
  int numChildren = shp.countChildren();
  if (numChildren != 0) {
    for (int i = 0; i < numChildren; i++) {
      RShape[] children = getAllChildren(shp.children[i]);
      for (int j = 0; j < children.length; j++) {
        result.add(children[j]);
      }
    }
  } else {
    result.add(shp);
  }
  return result.toArray(new RShape[result.size()]);
}

I did a few tests and it seems working but if anyone has a better solution, please let me know!