The code to process collision will fail if you have two balls at the same position. (This would not happened in real life).

If you have more than two ball , i.e. n ball
You need a double loop to process possible collision between any two balls.
The original code only process collision between two ball.
Change it a function e.g. collision(i,j) which will process collision between particle i and j.
The double will look like
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
  collision(i,j);
}
}

You are welcomed to check out [url=http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=120.0]Collision 2D[/url] to find more information.