How to extract values from custom class?

I have a class with some variables like so

class Foo {
  // Instance Variables
  String myString;
  int myNums1;
  int myNums2;

  // Constructor Declaration of Class
  Flow(String myString, int myNums){
    this.myString = myString;
    this.myNums1 = myNums1;
    this.myNums2 = myNums2;
  }
}

In my draw function, I would like to access the myNums variable.

// create Foo instance foo
for (int i=0; i<myTable.getRowCount(); i++) {
    String myString = myTable.getString(i, 0); //myTable is defined elsewhere and works just fine
    int myNums = myTable.getInt(i,1);
}

draw(){
numFlag = 1;
for (int i=0; i < foo.size(); i++){
float myWeight = map(foo.get(numFlag).get(i), 0, 10000, 0, 50);
strokeWeight(myWeight);
// draw something here...
}

How can I get the ith value of the numFlagth variable?

maybe try returning the values with return

I think I figured it out myself. I ended up adding an ArrayList into the class and then called it with

float myWeight = map(foo.get(numFlag).myArrayList.get(i), 0, 10000, 0, 50);

Thanks for your help!

2 Likes

When using classes, you can extract varriables from them by using:

ArrayList<CustomClass> obj = new ArrayList<CustomClass>();
obj.add(new CustomClass("parameter 1"));
println(obj.get(0).varriableName);
//or if using an ArrayList
printArray(obj.get(0).arrayListName);
//or if using ArrayList element:
println(obj.get(0).arrayListName.get(<id>));

used in code:

ArrayList<CustomClass> obj = new ArrayList<CustomClass>();
void setup() {
  obj.add(new CustomClass("parameter 1"));
  println(obj.get(0).varriableName);
  //or if using an ArrayList
  printArray(obj.get(0).arrayListName);
  //or if using ArrayList element:
  println(obj.get(0).arrayListName.get(2));
}
class CustomClass {
  float varriableName = 123;
  ArrayList<Float> arrayListName = new ArrayList<Float>();
  CustomClass(String randomTxt) {
    if (randomTxt.equals("hax")) println("Hi");
    for (int i = 0; i < randomTxt.length(); i++) arrayListName.add(random(255));
  }
}

1 Like