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++){

You are welcomed to check out [url=]Collision 2D[/url] to find more information.