4 Feet Software
Android Development & Technology
-
Dynamic Arrays of Arbitrary Objects
Posted on March 6th, 2009 No commentsEspecially on mobile phones it's important to keep the memory size as low as possible. For example an app might open an optional number of images (like 20 or more), but mostly it needs only 2 or 3. Use an Array with a fixed number of entries? In that case and many others it's probably not the best solution. One solution is to write a dynamic array class, which extends the data array as needed. This code demonstrates the idea:import android.util.Log; public class DynamicArray { private Object[] data; public DynamicArray() { data = new Object[1]; } public Object get(int position) { if (position >= data.length) return 0; else return data[position]; } public void put(int position, Object value) { if (position >= data.length) { int newSize = 2 * data.length; if (position >= newSize) newSize = 2 * position; Object[] newData = new Object[newSize]; System.arraycopy(data, 0, newData, 0, data.length); data = newData; Log.v("x", "Size of dynamic array increased to " + newSize); } data[position] = value; } }Now we can create dynamic arrays and put any objects including strings and integers to it like that:/* Greate Array */ DynamicArray arr = new DynamicArray(); /* Add Integers and Strings */ arr.put(0, 123); arr.put(1, "Hi There"); /* Output Array Content */ Log.v("Dynamic Array Test", "0: " + arr.get(0) + ", 1: " + arr.get(1));
Fatal error: Call to undefined function wpx_linkTo() in /var/www/4feets.com/htdocs/wp-content/plugins/exec-php/includes/runtime.php(42) : eval()'d code on line 48