1

I was wondering, in Processing, how to include an array as part of an object. Say I have an Object named "Node" and want it to contain a list of all the IDs of other nodes it's connected to. Please note that the length of this list can be variable. That is, one node can be connected to two or seven different other nodes. And how would I access that particular array within that object?

Here's some code I'm working with:

void setup(){
  size(200,200);
  Node node1 = new Node(color(255,0,0),40,80,2,0,.5,5,5,0);
  int neighbor = 6;
  node1.neighbors.add(neighbor);
}

void draw(){

}

class Node {
  Set<Node> neighbors;
  color c;
  float xpos;
  float ypos;
  float xspeed;
  float yspeed;
  float damp;
  float ForceX;
  float ForceY;
  int id;

  // The Constructor is defined with arguments.
  Node(color tempC, float tempXpos, float tempYpos, float tempXspeed, float tempYspeed, float tempDamp, float tempForceX, float tempForceY, int id_temp) {
    c = tempC;
    xpos = tempXpos;
    ypos = tempYpos;
    xspeed = tempXspeed;
    yspeed = tempYspeed;
    damp = tempDamp;
    ForceX = tempForceX;
    ForceY = tempForceY;
    id = id_temp;
    neighbors = new HashSet<Node>();
  } 
}

Thanks!

2 Answers 2

2

Arrays can be included in a class just as any other var:

    class Node{
       int[] array;
       Node (int[] _array){
           array = _array;
           }
        /// all he stuff
    }

But i think an object can't be "aware" of others objects of the same type. Maybe you need an other class Nodes, that takes an array of Node at constructor, or an ArrayList as the size must be variable. Or i'm afraid you have to handle the set of Node in draw().

Sign up to request clarification or add additional context in comments.

1 Comment

One thing to think about is whether you will change the size of the array after the constructor. If so you should use an ArrayList if not an array. Alternatively you could pass an int to the constructor and declare array = new int[_numberOfNodes]; and then set the individual elements of the array later. You could also define a maximum number of nodes and declare the array to that size and use a int to keep track of the number of elements in the array that you are using but I think the ArrayList often provides a simpler solution.
1

You're describing a graph. A graph class should look like this:

class Node {
    Set<Node> neighbors = new HashSet<Node>();
}

You may want to create methods such as addNeighbor(Node neighbor), isNeighbor(Node x) { return neighbors.contains(x); }, etc.

You described ids, but this method is more efficient assuming you have to load the other nodes in memory. The Set here containers references (e.g. C++ pointers) to other nodes.

You could also do a Set if you want ids and only integers.

4 Comments

When I try to do that, Processing tells me that "The method add(hashset.Node) in the type Set<hashset.Node> is not applicable for the arguments (int). Is it because the add() method means different things in Java and Processing?
If you want integers, then you want Set<Integer> neighbors = new HashSet<Integer>();
Do I have to add the HashSet library to Processing? How do I import it/from where do I download it? And will I be able to use the Hashet.add() method instead of the Processing add() method?
HashSet is a java.util class. You may import all of java.util via import java.util.*; You can use neighbors.add(42); if it is generic on Integer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.