Data type for ArrayList get() function (class, OOP)

Hi
For Array List functions, I would like to know the type of data returned by the Particle part = particles.get(0) function ?

after some try, ;;; it’s not float or int or string … so, what is ?

Thank in advance

Eric

By example, we could have the following code (see the ??? at the end) :

INITIALIZE SHEET :
//////////////////////////////////////////////////////////////////////////
ArrayList<Class_B> class_b_elements = new ArrayList<Class_B>();

// CREATE - Class D

Class_B class_b_elementA1 = new Class_B(BX+10, BY+10, 10, 15, 25, “1”); // (x, y, w, h, couleur, Id)
Class_B class_b_elementA2 = new Class_B(BX+25, BY+25, 15, 20, 50, “2”); //
Class_B class_b_elementA3 = new Class_B(BX+45, BY+45, 20, 25, 75, “3”); //
Class_B class_b_elementA4 = new Class_B(BX+70, BY+70, 25, 30, 100, “4”); //
Class_B class_b_elementA5 = new Class_B(BX+100, BY+100, 30, 35, 125, “5”); //

//////////////////////////////////////////////////////////////////////////

CLASS B SHEET :
//////////////////////////////////////////////////////////////////////////
class Class_B {
float x;
float y;
float w;
float h;
int couleur;
String Blabel;
int tStatus=1; // specifies current status (0=off/not visible, 1=on/visible)
int hStatus=0; // specifies if current is highlighted (1) or normal (0)

//////////////////////////////////////////////////////////////////////////

Class_B(float x, float y, float w, float h, int couleur, String Blabel) { // rectangle
this.x=x;
this.y=y;
this.w=w;
this.h=h;
this.couleur=couleur;
this.Blabel=Blabel;
}

//////////////////////////////////////////////////////////////////////////

??? getClass_B(int i) {
Class_B refClass_B = class_b_elements.get(i);
return refClass_B
}
} // Class_B definition

//////////////////////////////////////////////////////////////////////////

MAIN SHEET :
//////////////////////////////////////////////////////////////////////////
final int SCREEN_WIDTH = 800;
final int SCREEN_HEIGHT = 500;
//////////////////////////////////////////////////////////////////////////

void settings() {
size(SCREEN_WIDTH, SCREEN_HEIGHT);
}

//////////////////////////////////////////////////////////////////////////

void setup() {
Initialize();
}

//////////////////////////////////////////////////////////////////////////

for (int i = 0; i<5; i = i+1){
??? refClass_B = getClass_B(i) {
println (“refClass_B:”, refClass_B);
}

//////////////////////////////////////////////////////////////////////////

please format code with </> button * homework policy * asking questions

Here the data type that gets returned is Class_B.

Class_B is a class that gets defined within your code (so it’s not a data type like int or float).

To understand the concept of class, object and OOP, check Objects \ Processing.org

1 Like

part is of data type / class part.

part is an object instantiated from the class Particle.

The method getClass_B

This method:

this would be:

Class_B getClass_B(int i) {
    Class_B refClass_B = class_b_elements.get(i);
    return refClass_B;
}

Oh, and welcome to the forum!

Great to have you here!

Regards,

Chrisir

Thank you for your answer,

but in the main sheet, when I want call the Class_B getClass_B(int i) function in the loop for, Processing inform me that “the function “getClass_B(int)” doest not exist” …

this method is inside the class! (Which is debatable!)

you need

Class_B dummy= new Class_B(BX+100, BY+100, 30, 35, 125, “5”); //
Class_B refClass_B = dummy.getClass_B(i);

But there are other errors

but there are other errors; missing function draw(), one { too much…

Initialize() doesn’t exist

the elements are not added to the ArrayList…


Here is a version that works




// INITIALIZE SHEET : see below: Initialize()
//////////////////////////////////////////////////////////////////////////
ArrayList<Class_B> class_b_elements = new ArrayList<Class_B>();
//////////////////////////////////////////////////////////////////////////

// CLASS B SHEET :
//////////////////////////////////////////////////////////////////////////
class Class_B {
  float x;
  float y;
  float w;
  float h;
  int couleur;
  String Blabel;
  int tStatus=1; // specifies current status (0=off/not visible, 1=on/visible)
  int hStatus=0; // specifies if current is highlighted (1) or normal (0)

  //////////////////////////////////////////////////////////////////////////

  //constr
  Class_B(float x, float y, float w, float h, int couleur, String Blabel) { // rectangle
    this.x=x;
    this.y=y;
    this.w=w;
    this.h=h;
    this.couleur=couleur;
    this.Blabel=Blabel;
  }//constr

  String toString() {
    return
      Blabel
      +": "
      +str(x)
      +","
      +str(y)
      +" etc. ....";
  }//func 

  //////////////////////////////////////////////////////////////////////////
} // Class_B definition

//////////////////////////////////////////////////////////////////////////

// MAIN SHEET :
//////////////////////////////////////////////////////////////////////////
final int SCREEN_WIDTH = 800;
final int SCREEN_HEIGHT = 500;
//////////////////////////////////////////////////////////////////////////

void settings() {
  size(SCREEN_WIDTH, SCREEN_HEIGHT);
}

//////////////////////////////////////////////////////////////////////////

void setup() {
  background(0);

  Initialize();

  for (int i = 0; i<5; i = i+1) {
    Class_B refClass_B = getClass_B(i); 
    println ("refClass_B:", refClass_B.toString());
  }
}

//////////////////////////////////////////////////////////////////////////

void draw() {
  //
  background(0);

  for (int i = 0; i<5; i = i+1) {
    Class_B refClass_B = getClass_B(i); 
    text ("refClass_B: " + refClass_B.toString(), 
      30, i*30+44);
  }
}

//////////////////////////////////////////////////////////////////////////

// CREATE - Class D
void Initialize() {
  float BX = 100; 
  float BY = 100; 
  Class_B class_b_elementA1 = new Class_B(BX+10, BY+10, 10, 15, 25, "1"); // (x, y, w, h, couleur, Id)
  Class_B class_b_elementA2 = new Class_B(BX+25, BY+25, 15, 20, 50, "2"); //
  Class_B class_b_elementA3 = new Class_B(BX+45, BY+45, 20, 25, 75, "3"); //
  Class_B class_b_elementA4 = new Class_B(BX+70, BY+70, 25, 30, 100, "4"); //
  Class_B class_b_elementA5 = new Class_B(BX+100, BY+100, 30, 35, 125, "5"); //

  class_b_elements.add(class_b_elementA1);
  class_b_elements.add(class_b_elementA2);
  class_b_elements.add(class_b_elementA3);
  class_b_elements.add(class_b_elementA4);
  class_b_elements.add(class_b_elementA5);
  //
} //

Class_B getClass_B(int i) {
  Class_B refClass_B = class_b_elements.get(i);
  return refClass_B;
} //
//

Hi,

Wonderful … :grin:

Thank you very much.

In fact, initialy I put the “Class_B getClass_B(int i)” function into the Class_B definition … and I saw that you put it outside …

Have a good day