1

I have a set: Set<String> tmpSet = FastSet.newInstance();

When i follow this question: How to convert Set to String[]?

and i do the same: String[] strArrStrings = includeFeatureIds.toArray(new String[0]);

and i have this exception:

Exception: java.lang.IllegalArgumentException
Message: Error running script at location [component://order/webapp/ordermgr/WEB-INF/actions/entry/catalog/KeywordSearch.groovy]: java.lang.UnsupportedOperationException: Destination array too small
---- cause ---------------------------------------------------------------------
Exception: java.lang.UnsupportedOperationException
Message: Destination array too small
---- stack trace ---------------------------------------------------------------
java.lang.UnsupportedOperationException: Destination array too small
javolution.util.FastCollection.toArray(FastCollection.java:351)

So now, i have to code as:

for (FastSet.Record r = tmpSet.head(), end = tmpSet.tail(); (r = r.getNext()) != end;) {
         // copy one by one element to String[]  
     }

My question: Is there anyway(or utility) to convert FastSet to String[]?

Thank :-)

See also:

Community
  • 1
  • 1
Rong Nguyen
  • 4,143
  • 5
  • 27
  • 53

1 Answers1

2

See FastCollection#toArray(T[])

Unlike standard Collection, this method does not try to resize the array

so you have to make the array the right size. Use

includeFeatureIds.toArray(new String[includeFeatureIds.size()])

instead of

includeFeatureIds.toArray(new String[0])
Pshemo
  • 122,468
  • 25
  • 185
  • 269
vandale
  • 3,600
  • 3
  • 22
  • 39