-7

In other words I get {b,b,b} I want 3. I am trying to replicate Conway's Game of Life and I want the cell to return the number of neighboring cells. Right now all it does is find out if there are object near it and if there are it stores them in an imaginary box how do you get it to convert the number of objects in that box to an integer.

edit: I've found out it returns as lists, got it so the compiler didn't notice any errors, but when I run it i get this.

java.lang.ClassCastException: java.util.ArrayList cannot be cast to Cell at Cell.lookForCells(Cell.java:33) at Cell.act(Cell.java:24) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194)

2 Answers2

4

You need to use method size() to get the number of objects in a list:

List<String> list = new ArrayList<String>();
list.add("balh");
list.add("balh");
list.add("balh");
System.out.println(list.size());

OUTPUT:

3

and for array of objects, you need to use the final field length:

String[] array = {"blah", "blah", "blah"};
System.out.println(array.length);

OUTPUT:

3
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
1

Sure, easy:

char[] array = {'b', 'b', 'b'};
int num = array.length;
jrad
  • 3,172
  • 3
  • 22
  • 24