Get int array from database Cursor I find this code quite ugly, but in fact, I can't find a finer way of achieving the goal.

private static final String TABLE_NAME = "table_name";
private static final String[] allNeededColumns = {"col_id","col_one","col_two"};

public int[] getItemIds(int category_id) {
    ArrayList&lt;Integer&gt; ids = new ArrayList&lt;Integer&gt;();
    Cursor c = getDatabase().query(TABLE_NAME, allNeededColumns, CATEGORY_ID + "=" + category_id, null, null, null, null);
    c.moveToFirst();
    while(!c.isAfterLast()){
        ids.add(Integer.valueOf(c.getInt(2)));
        c.moveToNext();
    }
    return convertIntegers(ids);
}

public static int[] convertIntegers(ArrayList&lt;Integer&gt; integers)
{
    int[] ret = new int[integers.size()];
    Iterator&lt;Integer&gt; iterator = integers.iterator();
    for (int i = 0; i &lt; ret.length; i++)
    {
        ret[i] = iterator.next().intValue();
    }
    return ret;
}


The purpose is to get out of table int array, where IDs are item IDs from category bycategory_id`.

The problem is, I need some kind of expandable collection to add DB result ID to, and return primitives array, and I find it prettier than creating new int[] variable for each iteration on Cursor.

Can you think of a prettier solution?
If you were going to use the above you could still utilise Java's autoboxing feature, but there is a shorter form as hinted at by Darth Beleg.

Untested:

int[] ret = new int[c.getCount()];
if (c.moveToFirst())
    for (int i = 0; i &lt; c.getCount();) {
        ids[i++] = c.getInt(2);
        c.moveToNext();
    }
}


Although it is not pertinent to your case it is worth remembering that

ret[i] = iterator.next().intValue();


could throw a NPE.