Thursday, December 9, 2010

How to make list.toArray() return a specific class

debugStringsArray = new String[] {
"productId", "browseNode", "brand", "merchant", "productSource", "priceMetricDAL", "calculatedCPC",
"mexpMerchantIsActive", "yield", "mexpCatId", "relevance", "boost", "ctr", "tags"
if (newGrid) {
// boy it's a PITA to append to an already-initialized array
List debugStringsList = new ArrayList();
for (String debugString : debugStringsArray) {

// here is how you get around the fact that ordinary list.toArray() returns Object[]
debugStringsArray = new String[debugStringsList.size()];
debugStringsArray = debugStringsList.toArray(debugStringsArray);

// ETA: It's actually not that much of a PITA:
ArrayUtils.add(Object[] array, Object element) // convenience method

No comments: