Processing Intro #04 | Array List

Contents

  1. ArrayList
    1. Concept
    2. Declaration
    3. Functions
    4. Usage
  2. Practice – Random Walk 3D
    1. Random Walk 2D

ArrayList

Declaration

ArrayList<[data type]> [ArrayList name] = new ArrayList<[data type]>();

Functions

  • [ArrayList name].add([data])  :  add data at the end of the list
  • [ArrayList name].remove([index])  :  delete data with the index
  • [ArrayList name].clear()  :  delete all the data in ArrayList
  • [ArrayList name].get([index])  :  return data with the index
  • [ArrayList name].size()  :  return the current size of ArrayList

Usage

ArrayList<Integer> ints = new ArrayList<Integer>();

for(int i = 0; i<10; i++){
  ints.add(i);
}

int randint = int(random(3,8));

for(int i = 0; i<randint; i++){
  int randomindex = int(random(ints.size()));
  ints.remove(randomindex);
}

for(int i=0; i<ints.size(); i++){
  println(ints.get(i));
}

Practice – Random Walk 3D

Random Walk 2D

PVector pos,ppos,rand;
 
void setup(){
  size(500,500);
  ppos = new PVector(width/2, height/2);
}
 
void draw(){
  drawRandomWalk();
}
 
void drawRandomWalk(){
  rand = PVector.mult(PVector.random2D(),10);
  pos = PVector.add(ppos,rand);
  line(pos.x, pos.y, ppos.x, ppos.y);
  ppos = pos;
}