FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   ?-array size in constructor
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: ?-array size in constructor  (Read 353 times)
st33d

WWW Email
?-array size in constructor
« on: Dec 23rd, 2004, 12:56am »

I'm creating a web spider in Processing that calls htmls into a string, extracts the links and the creates a little map of links from that site. Then it roams to other sites and it's journey is mapped out on screen.
 
I've worked out I need to create a class which has properties regarding the url name, it's links, and the path taken. However, since the amount of links is unknown I'm a little confused as to how I should set up my variables in the class.
 
I'm going to need the link map object to be a dynamic array so I thought that the safest way of setting it up would be to start with a single index array for the url string inside and then expand it according to need using a method outside the constructor. But is all that gubbish necessary?
 
Code:

//brief example of my current theory
int c = 0;
linkMap [] maps = new linkMap[1];
 
void setup(){
framerate(5);
}
 
void loop(){
maps[c] = new linkMap("stuff"+c,int(random(4)));
mapLength (maps[maps.length-1]);
for (int i = 0; i < maps[c].url.length; i++){
println(maps[c].url[i]);
}
c++;
}//loop;
 
//url link object
class linkMap{
//array I don't know the length of until entering the constructor
String [] url = new String [1];
linkMap (String info, int times){
if (times > 1){
urlLength(url[0],times-1);
}
for (int i = 0; i < url.length; i++){
url[i]=info;
}
}//constructor;
//url array expansion gubbish
void urlLength (String newS, int xtra){  
  String[] tempS = new String[url.length + xtra];  
  System.arraycopy(url, 0, tempS, 0, url.length);  
  tempS[url.length] = newS;    
  url = tempS;
}//void;
}//class;
 
void mapLength (linkMap newM){
  linkMap[] tempM = new linkMap[maps.length + 1];  
  System.arraycopy(maps, 0, tempM, 0, maps.length);  
  tempM[maps.length] = newM;    
  maps = tempM;
}//void;

 
Is there an easier way of doing this? It does the job but do I really have to do this all the time?
 

I could murder a pint.
homesick_alien

WWW
Re: ?-array size in constructor
« Reply #1 on: Dec 23rd, 2004, 1:55am »

Maybe the ArrayList class is what you need:
 
http://java.sun.com/j2se/1.3/docs/api/java/util/ArrayList.html
 
hope this helps
 
st33d

WWW Email
Re: ?-array size in constructor
« Reply #2 on: Dec 23rd, 2004, 2:40am »

I'm a tad unsure how to populate this ArrayList. Sexy code though it may seem. Can anyone present a quick example of this in action? (I love the Java libraries right now but they don't have the homely simplicity of working in Processing.) Thanks for the tip though, interesting stuff.
 
Plus my concern was simply that; I have the variables which are global to the class set up before the constructor. But can I create those class-global variables whilst inside of the constructor without them being considered as local throw-away variables? It would mean that I could establish the String array length in the constructor without any of the fannying about worrying how long that array has to be with kooky work-arounds. Is it possible?
 

I could murder a pint.
st33d

WWW Email
Re: ?-array size in constructor
« Reply #3 on: Jan 9th, 2005, 9:16pm »

This is what I meant:
 
Code:

class1 c1 = new class1(3.45,int(random(3))+1);
void setup(){
}
void draw(){
for (int i = 0; i<c1.w.length; i++){
println(c1.w[i]);
}
}
 
class class1{
float [] w;
class1 (float z, int t){
this.w = new float[t];
for (int i = 0; i < this.w.length; i++){
w[i]=z+i;
}
}
}

 
The correct syntax for establishing an array template global to the class but leaving the dimensions of the array to be created by the constructor later. It's nice to know I don't have to drag around a load of deepcopying gubbinz into every class I make now.
 

I could murder a pint.
Pages: 1 

« Previous topic | Next topic »