If I follow your logic, you should first generate the balls, then check for collision.
Example:
Code:int numBalls = 8;
boolean overlap;
Ball[] balls = new Ball[numBalls];
void setup(){
size(100,100);
noStroke();
smooth();
for (int i = 0; i < numBalls; i++){
balls[i] = new Ball(random(width), random(height), i, balls);
}
for (int i = 0; i < numBalls; i++){
balls[i].collide();
}
for (int i = 0; i < numBalls; i++){
balls[i].display();
}
}
class Ball{
float x, y;
float diameter;
int id;
Ball[] others;
boolean display = true;
Ball(float xin, float yin, int idin, Ball[] oin){
x = xin;
y = yin;
diameter = 20;
id = idin;
others = oin;
}
void collide(){
for (int i = id + 1; i < numBalls; i++){
float distance = dist(x, y, others[i].x, others[i].y);
float minDist = others[i].diameter/2 + diameter/2;
if (distance < minDist){
display = false;
break;
}
}
}
void display(){
if (!display) return;
fill(255,200);
ellipse(x, y, diameter, diameter);
}
}
Or, check against the previously generated balls, not the next:
Code:[...]
void setup(){
size(100,100);
noStroke();
smooth();
for (int i = 0; i < numBalls; i++){
balls[i] = new Ball(random(width), random(height), i, balls);
balls[i].collide();
}
for (int i = 0; i < numBalls; i++){
balls[i].display();
}
}
[...]
void collide(){
for (int i = 0; i < id - 1; i++){
float distance = dist(x, y, others[i].x, others[i].y);
float minDist = others[i].diameter/2 + diameter/2;
if (distance < minDist){
display = false;
break;
}
}
}