ArrayList to Array

edited September 2018 in How To...

Hello!

I tried googling this question but I haven't found a way to do this.

I'm using several libraries (traer.physics, OpenNI and Triangulate), and I have these ArrayList objects:

//arraylists
ArrayList triangles = new ArrayList();
ArrayList point = new ArrayList();

I want to position the "point'' objects of the ArrayList in the location on some particles I created using traer physics, likes so:

//positioning objects
for (int i = 0; i < point.size(); i++) {
    PVector m = (PVector)point.get(i);

    // p.z is used to store an angle value (particle's direction)
    m.x += 2.0*cos(m.z);
    m.y += 2.0*sin(m.z);


    if (count < NUM_PARTICLES_X)
        point[count].position().set((p[ix][iy].position().x()), (p[ix][iy].position().y()));
  }

However, I (obviously) get the error of having an Array expression resolved to ArrayList.

How can I tackle this? Can I add the ArrayList objects to an Array and then .set() from there? or is there a quicker way to convert from arraylist to array.

P.S. The arraylist will have a discrete, finite number of items, but I can't use the array because the library Triangulate requires to use ArrayLists.

Thank you !

Tagged:

Answers

  •   ArrayList<PVector> p = new ArrayList<PVector>();
    
      for(int i = 0; i < 10; i++){
        p.add(new PVector(random(10), random(10)));
      }
    
      PVector[] p_Array = p.toArray(new PVector[0]);
    
      println(p_Array.length);
    
  • I would make one small change to the above code.

    PVector[] p_Array = p.toArray(new PVector[p.size()]);

    Rather than create a zero length array which must be replaced pass an array that is big enough to hold all the arraylist elements. This is then filled and return by the toArray method.

  • 11 point[count]...

    shouldn't this just be point.get(i)...?

    point is an ArrayList so you can't just point[i], you have to use point.get(i)

  • Answer ✓

    Couple of other points.

    Point 1

    I suggest that you use generics for the ArrayList so

    ArrayList<PVector> point = new ArrayList<PVector>();

    It means that any object returned from this arrayList is of type PVector so you don't have to cast the output so line 3 becomes

    PVector m = point.get(i);

    The advantage of this is type safety and helps prevent runtime errors when retrieving objects from an array list. Just noticed @billautomata has used generics but I will leave this explanation in.

    Point 2

    In Java the ArrayList holds references to the objects rather than the objects themselves, so the toArray method creates an array with copies of these references. It mean that if you use a reference to change an object that will be visible in both the array and the ArrayList e.g.

    ArrayList<PVector> p = new ArrayList<PVector>();
    
    for(int i = 0; i < 10; i++){
      p.add(new PVector(random(10), random(10)));
    }
    
    PVector[] p_Array = p.toArray(new PVector[0]);
    
    PVector m = point.get(2);
    m.x = -1234; 
    
    println(p_Array[2].x);
    

    will display -1234

Sign In or Register to comment.