arraylist of arraylist or 2d arraylist

edited November 2016 in How To...

Hi,

I'm trying to create an neighbor list for points in a nodal graph(set of points). Each point will have certain set of neighbor points. I'm trying to create this using arraylist of integer arraylist. I can declare one of the arraylist as objects of a class and use it to create a 2 dimensional array list. I have provided a sample of points below which expalins how the neighbor list looks like. Also i need to have flag array for each row of arraylist(0,1,2..) which will keep track of the points when i try to pick one of the neighbors from the list and use them to connect a line. and the same node should not be connected.

0 -> 1 4 5 6

1 -> 0 2 3 7

2 -> 4 3 5 9 11 12

3 -> ....................

Say, If 0 is connected to 1, 1 can't connect to zero and 1 should connect to some other points from the 2nd arraylist of neighbors ( 2 , 3 or 7).

Can you guys please help me provide a sample code using class objects and arraylist in java for the above scenario. It would be real helpful. Please. Thanks in advance

Tagged:

Answers

  • is this a 2D grid ?

    with cells and neighbors?

  • ArrayList<Cell> grid = new ArrayList(); 
    
    
    void setup() {
      size(888, 777); 
      for (int i = 0; i< 12; i++) {
        grid.add(new Cell(i));
      }//for
    }//func
    
    
    void draw() {
      for (Cell c : grid) {
        c.dieplay();
      }
    }
    
    // ===========================================
    
    class Cell {
      float x=random(width);
      float y=random(height);
    
      int index;
    
      ArrayList<Integer> neighbors = new ArrayList();
    
      //constr
      Cell(int index_) {
        //constr
        //
        index=index_;
        int border=int(random(5)+2); 
        for (int i = 0; i< border; i++) {
          Integer neighborIndex = new Integer(int(random(4))); 
          if (neighborIndex!=index)
            neighbors.add(neighborIndex);
        }
      }//constr
    
      void dieplay() {
        for (Integer i : neighbors) {
          Cell currentNeighbor=grid.get(i); 
          line (x, y, currentNeighbor.x, currentNeighbor.y);
        }//for
    
        ellipse(x, y, 8, 8);
      }//method
    }//class
    //
    
  • edited November 2016

    @Chrisir , Thanks for your reply. Well it's not a 2d grid.

    I have a set of points random points say 30 points. I'm trying to connect each point randomly with among its neighbors. So I'm trying to create a simplex where each point has a set of neighbors based on the distance constraint. for eg:

    0 -> 1,2,4,8,10

    1-> 0,3,5,11,15,16,17

    2-> 0,4,8,10,9 . . . 29 -> 21,17,20,28,10

    Such that after connecting all points, either it forms a loop or it doesn't.

    One constraint while connecting the points is that any point can connect to a point only once and not the reverse way during its path and each point can be traversed only once. so i have to have a track of flag or pointers for each point such that when 0 connected to 1 doesn't go back and connect 1 to 0.

    It's an array-list of array-list since both of them are dynamic in size. I have to add the neighbor points to the inner arraylist using class object(arraylist).

  • edited November 2016

    My code suits the requirements in its data structure, just make the necessary adjustments, e.g. distance constraint

    eg. after line 8 say (pseudo code)

    for (i=0; i < ...; i++) {
       for (i2=i+1; i2 < ...; i2++) {  // note that we start at i+1 !!!
           if(dist(.... )) {
                Cell currentC=grid.get(i); 
                currentC.neighbors.add(i2);
    

    we took care of this:

    • any point can connect to a point only once and not the reverse way
  • It sounds like you have a very specific set of directed graph requirements, so this may not be relevant.

    However, one algorithm for choosing edges to connect a set of random points is a Delaunay triangulation -- there is an implementation of this in the Processing mesh library.

  • edited November 2016

    @chrisir, I'm unable to understand the code. Can you please explain in detail. I'm sorry. Please forgive for my stupidity.

    I just need a help to create a program for this scenario,

    I have set of points point ( x , y )

    0 - (888.71967 401.93594)
    1 - (817.91113 579.8652)
    2 - (579.4381 593.5302)
    3 - (487.64703 408.01547)
    4 - (527.1854 208.826)
    5 - (712.8809 204.9348)
    

    Now i have to create a neighbor list for the set of 6 points, each point having it's neigbor list based on the distance constraint say <150 .

    So the neighbor arraylist will look like

    0 - 1 , 3 , 4
    1 - 0 , 4
    2 - 3, 4, 5
    3 - 0 , 2
    4 - 0 , 1 , 2
    5 - 2
    

    This simplex of neighbor list should be in a separate data structure so that it's global and i can use it in other places of my code.

    ==> ((1 3 4),(0 4),(3 4 5),(0 2),(0 1 2),(2)) - A two dimensional array list formed using a class object. A class having one of the object as arraylist and other arraylist declared in the main program.

    Also, an additional flag pointer to keep track of the neighbor points, such that when 0 is connected to 1. 1 connects to other point namely 4 in our case and 2 connects to 3 or 5 and not 4.

    Can you please write a code for this scenario.

  • is the numerber of points constant (6)

    Do they not move so the neighbours are constant?

    then make an array (the points) of arraylist (the neighbours)?

    you wrote: So the neighbor arraylist will look like

    do you mean : point number - neighbours as the index of the points?

    0 - 1 , 3 , 4 
    1 - 0 , 4 
    2 - 3, 4, 5 
    3 - 0 , 2 
    4 - 0 , 1 , 2 
    5 - 2
    

    ?

  • my approach qoes just what you require, it just uses a class and objects.

    are you familiar with that?

    The points are in an ArrayList named grid

    each point has an index (0,1,2...) and an ArrayList of its own for its neighbours

    ArrayList<Cell> grid = new ArrayList(); 
    
    
    void setup() {
      size(888, 777); 
      for (int i = 0; i< 12; i++) {
        grid.add(new Cell(i));
      }//for
    
      for (int i=0; i < grid.size()-1; i++) {
        for (int i2=i+1; i2 < grid.size(); i2++) {  // note that we start at i+1 !!!
          Cell currentC1=grid.get(i);
          Cell currentC2=grid.get(i2);
          if (dist(currentC1.x, currentC1.y, currentC2.x, currentC2.y ) < 350 ) {
            currentC1.neighbors.add(i2);
          }
        }
      }
    }//func
    
    void draw() {
      for (Cell c : grid) {
        c.display();
      }
    }
    
    // ===========================================
    
    class Cell {
      float x=random(width);
      float y=random(height);
    
      int index;
    
      ArrayList<Integer> neighbors = new ArrayList();
    
      //constr
      Cell(int index_) {
        //constr
        //
        index=index_;
        //int border=int(random(5)+2); 
        //for (int i = 0; i< border; i++) {
        //  Integer neighborIndex = new Integer(int(random(4))); 
        //  if (neighborIndex!=index)
        //    neighbors.add(neighborIndex);
        //}
      }//constr
    
      void display() {
        for (Integer i : neighbors) {
          Cell currentNeighbor=grid.get(i); 
          line (x, y, currentNeighbor.x, currentNeighbor.y);
        }//for
    
        ellipse(x, y, 8, 8);
      }//method
    }//class
    //
    
  • @chrisir , Thank you chris. It seems to be working now. Just one doubt. How do i access and print the arraylist. both the outer arraylist and the innerarraylist.

  • Answer ✓

    ??

    You need to understand classes and objects and oop when you want to understand this. See tutorial on this objects.

    Anyway: the outer ArrayList is addressed in lines 22 to 24

    The inner ArrayList that each point owns: 51 to 54

    You could easily make a list for println when you copy both parts to a double for loop

  • edited November 2016 Answer ✓

    new version that shows the primary Arraylist and also the inner ArrayList of each object at the end of setup() as println:

    I named the for-loops as // 1st control loop and // 2nd control loop :

    ArrayList<Cell> grid = new ArrayList(); 
    
    
    void setup() {
      size(888, 777); 
      for (int i = 0; i< 12; i++) {
        grid.add(new Cell(i));
      }//for
    
      for (int i=0; i < grid.size()-1; i++) {
        for (int i2=i+1; i2 < grid.size(); i2++) {  // note that we start at i+1 !!!
          Cell currentC1=grid.get(i);
          Cell currentC2=grid.get(i2);
          if (dist(currentC1.x, currentC1.y, currentC2.x, currentC2.y ) < 350 ) {
            currentC1.neighbors.add(i2);
          }
        }
      }
    
      // 1st control loop 
      for (Cell c : grid) {
        println (c.index);
      }// for
      println();
    
      // 2nd control loop 
      for (Cell c : grid) {
        print (c.index + ": ");
    
        // show c's neighbours now: 
        for (Integer i : c.neighbors) {
          print(i+", ");
          //Cell currentNeighbor=c.grid.get(i); 
          //line (c.x, y, currentNeighbor.x, currentNeighbor.y);
        }//for
        println();
      }//for
      //
    }//func
    
    void draw() {
      for (Cell c : grid) {
        c.display();
      }
    }
    
    // ===========================================
    
    class Cell {
      float x=random(width);
      float y=random(height);
    
      int index;
    
      ArrayList<Integer> neighbors = new ArrayList();
    
      //constr
      Cell(int index_) {
        //constr
        //
        index=index_;
        //int border=int(random(5)+2); 
        //for (int i = 0; i< border; i++) {
        //  Integer neighborIndex = new Integer(int(random(4))); 
        //  if (neighborIndex!=index)
        //    neighbors.add(neighborIndex);
        //}
      }//constr
    
      void display() {
        for (Integer i : neighbors) {
          Cell currentNeighbor=grid.get(i); 
          line (x, y, currentNeighbor.x, currentNeighbor.y);
        }//for
    
        ellipse(x, y, 8, 8);
      }//method
    }//class
    //
    
Sign In or Register to comment.