We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
array managing (Read 245 times)
array managing
Feb 10th, 2009, 12:57am
 
What's the most elegant way to move an element in an array to the last position?
For example, take position 1 and put it at the end.
from
{ "OH", "NY", "CA", "DE" };
to
{ "OH",  "CA", "DE", "NY" };

Thanks
Re: array managing
Reply #1 - Feb 10th, 2009, 6:34am
 
No elegant way other than doing it manually (store "NY" into a temp variable, shift everything over, put it at the end).

However, the Vector class of java (which you can just type into processing without any trouble) is much better at this type of thing, with methods for removing objects (and adding at the end, inserting at a particular points, etc), plus a toArray() method that will spit out an array version of whatever the Vector contains...

This taks would simply be (untested!):

myVector.add(myVector.remove(1));
Re: array managing
Reply #2 - Feb 10th, 2009, 10:25am
 
danI is right, arrays are fast but a bit inflexible for this kind of operation.
But I will advocate the use of ArrayList over the older, slower, a bit obsolete Vector.
Note that Java offers other Collections, like LinkedList or ArrayDeque (like a stack), that might be better suited for some tasks.
Page Index Toggle Pages: 1