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 › Variable array size
Page Index Toggle Pages: 1
Variable array size (Read 471 times)
Variable array size
Mar 21st, 2008, 10:32pm
 
Hi everyone,
 Notice the subject reads variable and not dynamic array size.  I've created a custom class to import obj files into processing.  The class reads through the obj file and stores the vertex, normal, uv info. etc into  separate arrays.  The array sizes need to be variable depending on the imported object, but once the object is loaded they can remain static.
Everything works perfectly except that right now i have the array sizes specified to some huge number.  I've got a method that is called in the constructor that reads through the file and returns the numbers of vertics, faces etc, i use that to know where the actual end of my array is.  I only need to set the array size once when the object is instantiated after that it doesn't need to change, so i don't think i need to append the array or anything like that. Here's a really simplified example of what i'm trying to do:

class importObj(){
   int vnum; //total number of vertices
   vert[]= new vert[vnum]; //array that holds vertex info

   importObj(){  //constructor
      getnumberofvertices(); //gets total vertices
      }

  int getnumberofvertices(){ //method to get total vertices
      vnum= bunch of code to get total number of vertices;
      }
}
Re: Variable array size
Reply #1 - Mar 21st, 2008, 11:45pm
 
I don't understand why you have a massive array size.

Surely if the object is waiting for data to be put into it, it doesn't need the arrays to be initialised to a given size until the data is loaded.

For example:

Code:

String stuff = "1,2,3,4,5,6";
int [] nums;
String [] splits = split(stuff, ",");
nums = new int[splits.length];
for(int i = 0; i < splits.length; i++){
nums[i] = int(splits[i]);
}

for(int i = 0; i < splits.length; i++){
print(nums[i]+" ");
}
Re: Variable array size
Reply #2 - Mar 22nd, 2008, 7:19pm
 
WOW i'm retarded, i was trying to declare and initialize the array on the same line:

vert[] verices= new vert[vnum];

no wonder that didn't work.  Thanks St33d!
Page Index Toggle Pages: 1