• Post Reply Bookmark Topic Watch Topic
  • New Topic

Dealing with ArrayList items in a static method  RSS feed

 
Deena Lee
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Friends,
I am having problems with ArrayList items in a method that must be static. The code picks two random mice in the mice ArrayList. It then is suppose to compare the two mice to see if they are of different sex and both over the age of 1.

Here is my code
The code as is generates an error message like this for all my metods in the if loop ...
non-static method getAge(java.util.ArrayList,int) cannot be referenced from a static context
&& ((getAge(mice, a) >1) && (getAge(mice, b) >1)))

I have tried to change the code (and the methods) to not accept parameters, to something similar to this ...
but, I then get this error message:
cannot find symbol
symbol : method mice(int)
location: class Mouse ... I have imported the Java.util.ArrayList class into the Mouse class. The mice is the ArrayList and the (int) should be the index of the item in the list.

Any help on this is appreciated.
Thanks!
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getAge() will need to be a static method in order to be referenced from a static method. There's nothing you can do to make it work other than making it static.
 
Deena Lee
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gregg Bolinger wrote:getAge() will need to be a static method in order to be referenced from a static method. There's nothing you can do to make it work other than making it static.


Ok, that's understandable, but when I changed the getAge code so it wasn't static, I got a cannor find mice(int) method
mice(a).getAge()

What should I be looking for to make it work?
 
Ram Narayan.M
Ranch Hand
Posts: 247
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mice ArrayList contains list of "Mouse" objects..right?

to get the object stored in the arraylist and if your Mouse class comprises getAge() method , use


 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
== true???

Dreadful. Never write == true. You just leave that bit out. Never write == false. You use the ! (bang operator) to negate the Boolean value.
NotbutOr better, if you simply want two mice of different sex, try
 
Deena Lee
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Narayan and Ritchie,

I've got the code in better form and compiling thanks to both of your comments.

However, the program isn't returning the results it should be. So, I'll keep at it.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or better yet, "areMateable(mousie, otherMousie)" or "mouse.canMateWith(anotherMouse)"

After all, a male mouse may be a castrato, and we must be sensitive to their needs.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got a few remarks about your code that hopefully will improve your code:

If your ArrayList only contains mouses or subclasses of mouse then you can declare your ArrayList to be generic:
ArrayList<Mouse> mice = new ArrayList<Mouse>();
Or even better, program against an interface instead of an implementation:
List<Mouse> mice = new ArrayList<Mouse>();

The generic part will provide compile time type safety and will remove casts from your code.
The interface part will allow you to change the list implementation without massively changing your code
and then there is no need to know the implementation details because you only need the specification on the interface.

And finally the method isMale() sounds like a prime candidate to be a property of mice.
 
Deena Lee
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone! I've got it working!
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome . As an alternative to the != operator, you could try the XOR operator (caret = ^).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!