What is equivalent to splice in array of vectors?

I want to add a pvector at a specific index in arraylist of vectors which already includes few vectors similar to splice function for array. Any idea how to go about this for vectors?

1 Like

I assume that you mean Arraylist (not array)

you can use add() but with an index :

add(int index, E element)

Inserts the specified element at the specified position in this list.

see https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html

In the example below,

  list.add(1, new PVector (311, 111) );

is different from

  list.add(new PVector (311, 111) );

Sketch:



float angle1=0;

ArrayList<PVector> list=new ArrayList(); 

void setup() {
  size(1640, 860);

  list .add(new PVector (111, 111) );
  list .add(new PVector (311, 311) );
}

void draw() {
  background(0);

  //
  int i=0;
  for (PVector pv : list) {
    ellipse(pv.x, pv.y, 
      5, 5);
    fill(255); 
    text (i, pv.x+6, pv.y );
    i++;
  }
}

void mousePressed() {
  //

  list .add(1, new PVector (311, 111) );
}
5 Likes

JS example
https://stackoverflow.com/questions/37601282/javascript-array-splice-vs-slice ,

but please show us this function for
JAVA Array,
as i did not find it.

@Chrisir
i find this ?add? very confusing in Processing:

There is a special array section in the reference

https://processing.org/reference/splice_.html

2 Likes