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.
IndexProgramming Questions & HelpSyntax Questions › Array instantiation..
Page Index Toggle Pages: 1
Array instantiation.. (Read 1042 times)
Array instantiation..
Jun 2nd, 2005, 5:00am
 
Hi. Heres a question. Why does one have to pass the length / count of an array when creating it?

Id like to be able to add items to an array an not know how many elements I will have when im creating it. However, all of the examples Ive seen in the tutorials/reference always give a 'maxnumber' or 'arraymax' of some sort and pass that as the length when creating.

Why cant I do a

String[] urlArray = new String[];

And then just add them to the end of the array?

urlArray[i] = "http://example.com";
urlArray[i+1] = http://example2.com";  //etc.

The reason Im asking is because im integrating a mysql backend to some data visualization I am doing. I dont know ahead of time how many rows are being returned, and the only workaround I can see is doing 'expensive' 'count(row) as arraytotal' sql queries when I shouldnt have to?

Am I simply overlooking something? Is this a java thing? Im a PHP guy, and Im used to not having to worry about this. Sorry, im a proce55ing newb, so just point me in the right direction.

Thanks.
Re: Array instantiation..
Reply #1 - Jun 2nd, 2005, 7:38am
 
what you're looking for is this beautiful little gem named append()

http://processing.org/reference/append_.html

However append only supports int, float, char, string, and normal stuff. If you want to append your own objects to your own object arrays, then you need to extend/overload append yourself. To do this, use System.arraycopy(). You can google that function and find out the rest :]
Re: Array instantiation..
Reply #2 - Jun 2nd, 2005, 8:57am
 
If you find append() doesn't do everything you'd like try the Vector class.

Code:


Vector list;

list = new Vector();



The size of a vector is variable, though you might also checkout arrayList which has less functionality, but is quicker for it. It can contain references to Objects etc. Very flexible

Here's the documentation for it.

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Vector.html
Page Index Toggle Pages: 1