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 › using "Vector" instead of "Array&qu
Page Index Toggle Pages: 1
using "Vector" instead of "Array&qu (Read 1152 times)
using "Vector" instead of "Array&qu
Jun 11th, 2005, 7:13am
 
Hi. Ive been toiling away at a problem that I cant seem to find an elegant solution to. My problem stems from the fact that a) append() does not work with objects, and b) I dont know java very well (besides what little i know from playing with processing).

I have  2 classes that I am building/populating from a mysql database:
Code:

class Media
{
 String url;
 String title;
 String metadata;
 float xpos;
 int mediaTypeID; // this is specified in prefs.php in the CMS software.. since media can be of multiple types, we have to know how to display it..
 PImage textureImage;
 PImage largerTextureImage;
 // CONTSTRUCTOR
 
 Media (String aUrl,String aTitle,String someMetadata, int aMediaTypeID)
 {
   url = aUrl;
   title = aTitle;
   metadata = someMetadata;
   largerTextureImage = loadImage(url);
   textureImage = loadImage(url);
   mediaTypeID = aMediaTypeID;

 }
}

class Event
{
 String title, date, description;
 float xpos, ypos, zpos, titleWidth, titleHeight;
 float opacity = 60;
 Media mediaItems[];
 
 // CONSTRUCTOR
 Event (String aTitle,String aDate,String aDescription,float aXpos,float aYpos,float aZpos, Media[] someMediaItems)
 {
     title = aTitle;
     date = aDate;
     description = aDescription;
     xpos = aXpos;
     ypos = aYpos;
     zpos = aZpos;
     mediaItems = someMediaItems;      
  }
}  

As you can see, the Class Event can contain many Media items within it (or none..). Now I have an array of Events, each with an array of Media items associated with them. These arrays are being built from an sql backend.

psuedo code is:
Code:

Event[] events;
Media[][] mediaItems;

New sql connection;
if(sql.connect)
{
  select "events from my database order date desc"
  while(results)
  {
    eventID = eventIDfromDatabase;
   
    select "media items with this eventID"
    while(results2)
   {
     mediaItems[EventRow][MediaRow] = New Media Object(blah);
   }

   events[EventRow] = New Event Object(blah.. mediaItems);

  }

}


However, because arrays in processing must be created with a known length when being populated with objects (you cant use append on objects), I have to force the array length to be constant each mysql result loop. A bad hack is to use another query to get the number of event items and media items in the database, but thats rather intensive db wise.. (and not elegant thefore not really acceptable).

Another poster was kind enough to show me the Vector class that java has, but I simply cannot figure out how to use Vector in a situation to build dynamic 'arrays' (vectors?) of objects. http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Syntax;action=display;num=1117681253

Can someone give me an example of using Vector within Processing to create a dynamic 'array' of objects?

Apologies for my newbness on this front. Im happy to zip up what code I have, as most of it works quite well, but I get major issues when I hit an event that does not have the appropriate (ie forced due to array creation issues) amount of media items.

Also, apologies if this is a bit rant-ish. Any chance append() could support objects for 1.0?  If youve made it this far, thanks for reading..
Re: using "Vector" instead of "Arra
Reply #1 - Jun 11th, 2005, 11:27am
 
Isn't there a MySQL num_rows method you can call to find out how many results are being returned?
Event[] resultArray = new Event[numRows];
Re: using "Vector" instead of "Arra
Reply #2 - Jun 11th, 2005, 6:49pm
 
Well, yes, but that hardly teaches me how to use vector Smiley. I dont think the mysql library had a provision for num_row (afaik), so id have to do another select query (ie select "count * as numEvents from media where eventID="+eventID; )which just taxes the DB more. Granted it is a solution, but I think the vector issue is just good to know about.

Thanks Markavian.

Markavian wrote on Jun 11th, 2005, 11:27am:
Isn't there a MySQL num_rows method you can call to find out how many results are being returned
Event[] resultArray = new Event[numRows];

Re: using "Vector" instead of "Arra
Reply #3 - Jun 11th, 2005, 9:47pm
 
The old way of appending an array, which works with objects:
Code:

int [] x = new int [1];
void setup(){
x[0] = 1;
}
void draw(){
newLength(x[x.length - 1]);
println(x.length);
}
void newLength(int newx) {
int[] tempx = new int[x.length + 1];
System.arraycopy(x, 0, tempx, 0, x.length);
tempx[x.length] = newx;
x = tempx;
}

And you can use java's vector class:

http://processing.org/discourse/yabb/YaBB.cgi?board=Syntax;action=display;num=1091820063;start=12

if you want heavily multi-dimensional arrays you can pack objects within objects to achieve this.
Page Index Toggle Pages: 1