Loading...
Logo
Processing Forum

Arrays of a class

in Programming Questions  •  8 months ago  
Hi all,

I am trying to create an array of a class but am getting an error code. The code below is supposed to import multiple csv files with file names listed in the list array . Please can you let me know what I have done wrong? 


String[] list;
WINCH[] winch;

void setup(){
  filelist();
  WINCH[] winch =new WINCH[list.length];
  for (int n=0 ; n<list.length ; n++ ){
    winch[n] = new WINCH(n);
  }

void filelist(){
  //create file list
  int starttrack = 4;
  int endtrack = 6;
  int startwinch = 1;
  int endwinch = 4;
  int winchquantity = (endtrack-starttrack+1)*(endwinch-startwinch+1);
  String[] list = new String [winchquantity];
  int index = 0;
  for (int tr=starttrack;tr<endtrack+1;tr++) {
    for (int wi=startwinch;wi<endwinch+1;wi++){
      list[index] = "car_" + tr + "_" + wi +".txt";
      index++;
    }} 
}

class WINCH{
   
  String lines[];
  String [][] csv;
  int csvWidth=0;
  float [] x;
  float [] y;
  int timesteps;
   
  WINCH(int ind){
    //load CSV
    int index = ind;
    lines = loadStrings(list[index]);
    //calculate max width of csv file
    for (int i=0; i < lines.length; i++) {
      String [] chars=split(lines[i],',');
      if (chars.length>csvWidth){
        csvWidth=chars.length;
      }
      timesteps=lines.length-1;
    }
    
    //defines variables
    csv = new String [lines.length][csvWidth];
    x = new float [timesteps];
    y = new float [timesteps];
     
    //parse values into csv array
    for (int i=0; i < lines.length; i++) {
      String [] temp = new String [lines.length];
      temp= split(lines[i], ',');
      for (int j=0; j < temp.length; j++){
       csv[i][j]=temp[j];
      } 
    }
    
    //parse variables to time, position variables
    for (int i=0; i < timesteps; i++) {
         x[i]=float(csv[i+1][1])*(-1);
         y[i]=float(csv[i+1][0]);
       }
  }}

Replies(6)

Re: Arrays of a class

8 months ago
What was the error code? Did the program stop at a particular line, if so which one?

BTW this line in ffilelist() is creating a local array which is masking the global array list[] defined above setup

String[] list = new String [winchquantity];

change it to
list = new String [winchquantity];

Re: Arrays of a class

8 months ago
The same local/global scope problem goes for the other array:
Copy code
  1. WINCH[] winch =new WINCH[list.length]; // in setup

Remove WINCH[] at the beginning of the above line in setup() to initialize the global array instead of creating a new temporary array which is local to the setup() method.

Re: Arrays of a class

8 months ago
String[] list;
WINCH[] winch;

void setup(){
  filelist();
  WINCH[] winch =new WINCH[list.length];
  for (int n=0 ; n<list.length ; n++ ){
    winch[n] = new WINCH(n);
  }
}

The second winch array isn't the same that the first one, which remains null, so you probably get a NullPointerException.
Very classical error.
Remove the declaration part of the second line.

Re: Arrays of a class

8 months ago
Thanks for the quick response. The problem was indeed redeclaring the array. If anybody is interested, the final working code is shown below.

String[] list;
WINCH[] winch;

void setup(){
  filelist();
  winch =new WINCH[list.length];
  for (int n=0 ; n<list.length ; n++ ){
    winch[n] = new WINCH(n);
  }

void filelist(){
  //create file list
  int starttrack = 4;
  int endtrack = 6;
  int startwinch = 1;
  int endwinch = 4;
  int winchquantity = (endtrack-starttrack+1)*(endwinch-startwinch+1);
  list = new String [winchquantity];
  int index = 0;
  for (int tr=starttrack;tr<endtrack+1;tr++) {
    for (int wi=startwinch;wi<endwinch+1;wi++){
      list[index] = "car_" + tr + "_" + wi +".txt";
      index++;
    }} 
}

class WINCH{
   
  String lines[];
  String [][] csv;
  int csvWidth=0;
  float [] x;
  float [] y;
  float [] load;
  int timesteps;
   
  WINCH(int ind){
    //load CSV
    int index = ind;
    println(list[index]);
    lines = loadStrings(list[index]);
    //calculate max width of csv file
    for (int i=0; i < lines.length; i++) {
      String [] chars=split(lines[i],',');
      if (chars.length>csvWidth){
        csvWidth=chars.length;
      }
    }
    
    //calculate length of data ignoring blank lines
    for (int i=0; i<lines.length; i++) {
      if (lines[i].length()>0) timesteps=i;
    }
    println(lines.length);
    println(timesteps);
    
    //defines variables
    csv = new String [timesteps+1][csvWidth];
    x = new float [timesteps];
    y = new float [timesteps];
    for (int i=0;i<timesteps;i++){
      x[i]=0;
      y[i]=0;
    }
     
    //parse values into csv array
    for (int i=0; i < timesteps+1; i++) {
      String [] temp = new String [lines.length];
      temp= split(lines[i], ',');
      for (int j=0; j < temp.length; j++){
       csv[i][j]=temp[j];
      } 
    }
    
    //parse variables to time, position and load variables
    for (int i=0; i < timesteps; i++) {   
      x[i]=float(csv[i+1][1]);
      y[i]=float(csv[i+1][0]);
       }
  }}

Re: Arrays of a class

8 months ago
Same topic, some other question:

I got an array of a class which is supposed to be dynamic somehow, so I don't know which array-index the objects reference retrieves in advance. It might be that some new objects get inserted as well as appended so I need the index to set the relation of the current object to other, already existing references.

How do I get the array-index from the objects reference inside the class-constructor?
Is the only way to pass it as a constructor-argument as shown above or is the some inherited method alike "this.Index" ?

Re: Arrays of a class

8 months ago
If you want to be able to insert Objects into an array then I suggest you use ArrayList instead of an array. The ArrayList also has methods for returning the index of a particular object.