4 Replies - 4140 Views - Last Post: 27 May 2012 - 09:48 PM

#1 aveanez   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-April 12

Can't get size() of Iterable?

Posted 27 May 2012 - 08:03 PM

Hi! My program is basically an Android app creating various types of Creatures (such as a Runner Creature, Chaser Creature...). The method tries to figure out which Creature is closest to the current Creature. One of my if-statements below is not compiling and I can't figure out how to get the size (how many elements) of the carr array. Can someone help me figure this out?

import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.Paint;
import android.graphics.Rect;
import java.util.*;

public class Interactor {

    public void closest(Creature c, Iterable<Creature> carr) {
        double shortestDistance = Double.MAX_VALUE;
        Creature c2 = null;
        int sum = 0; 
        // for each Creature in the array...
        for (Creature creature : carr) {
            // if there's only Creature c and no other creature in the array...
            sum = this.size(carr); // Error: Cannot find symbol
            if (sum == 1) {
                return;
            }
            // if c is closest to a Creature in the array...
            if (c.distanceTo(creature) < shortestDistance && !(c == creature)) {
                shortestDistance = c.distanceTo(creature);
                c2 = creature;
            }
        }
    }



Is This A Good Question/Topic? 0
  • +

Replies To: Can't get size() of Iterable?

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Can't get size() of Iterable?

Posted 27 May 2012 - 08:07 PM

You just can't as, in certain cases, the list content, and thus its size, can actually change when you iter over it
Was This Post Helpful? 1
  • +
  • -

#3 aveanez   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-April 12

Re: Can't get size() of Iterable?

Posted 27 May 2012 - 08:11 PM

View Postpbl, on 27 May 2012 - 08:07 PM, said:

You just can't as, in certain cases, the list content, and thus its size, can actually change when you iter over it


So you can't use size() on Iterables?

What would you suggest I do then to have the closest() method return null if there's only one Creature (that Creature itself) in the array?
Was This Post Helpful? 0
  • +
  • -

#4 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Can't get size() of Iterable?

Posted 27 May 2012 - 08:14 PM

just do

int sum = 0;
for(... {
++sum;
}
if(sum == 1)
Was This Post Helpful? 1
  • +
  • -

#5 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12557
  • View blog
  • Posts: 45,683
  • Joined: 27-December 08

Re: Can't get size() of Iterable?

Posted 27 May 2012 - 09:48 PM

*Moved to Android*
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1