I have a project that involves one array of thousands of circular objects. To apply some change to all of them, I use the usual for() loop to cycle through each one, and call a method, etc.
But now I want the ability to apply a change to only a *subset* of the objects, and I want to be able to order them. For example, I want to identify only the objects at a certain z depth value, and then sort them by largest to smallest. The end goal is to then feed this list of objects, in sorted order, to a method that will do something with them (e.g. cluster or arrange them by size, left to right).
I thought this would be possible by creating a second array of objects, and then "copying" or "referencing" the subset of objects in that new array. For example, here is a portion of my code:
Code:
//Create a new object array to store all objects to be clustered
Object[] objectsToCluster = new Object[1];
objectsToCluster[0] = new Object(0, 0);
//Grab all objects with a z depth of zero and throw them in the array
for (int i = 0; i < objects.length; i++) {
if (objects[i].z == 0) {
objectsToCluster = (Object[]) append(objectsToCluster, objects[i]);
}
}
The intent is for the for() loop to cycle through all objects, find the ones where z == 0, and then append them to the objectsToCluster array.
So I guess I have a few questions -- responses to any would be helpful.
1) Does this approach make sense?
2) Once I have copied the subset of objects over into the new array, how can I sort them by size? (Are ArrayLists or HashMaps the answer? I don't understand either.)
3) I get an error when trying to pass this new array of objects to a method. What does "object1.radius cannot be resolved or is not a field" mean?