I need to create and order an array of objects, problem is the objects cannot be ordered by themselves. Instead they are ordered based off an integer value. So I thought of using a 2d array.
Object[][] array = new Object[][]
That way I can store in the array the
[integer to order the objects][object]
But the problem is I don’t know how many objects there will be that need to be ordered, so, do I need to use an Arraylist instead?
Either way, is it possible to use a collection.sort command, and how can I possible get an array to behave as described?
Thanks
You need to forget about raw arrays and start thinking in terms of the Java Collections Framework.
For sorting use a
TreeMap<Integer, Object>. After you’veputall your objects in, you can usevalues()to get the objects in proper order.Using the map you have also eliminated all problems of size determination.