aspose file tools
The moose likes Beginning Java and the fly likes Dealing with ArrayList items in a static method Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Dealing with ArrayList items in a static method" Watch "Dealing with ArrayList items in a static method" New topic
Author

Dealing with ArrayList items in a static method

Deena Lee
Greenhorn

Joined: Aug 19, 2010
Posts: 13
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

Joined: Jul 11, 2001
Posts: 15230

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

Joined: Aug 19, 2010
Posts: 13
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

Joined: Jul 11, 2010
Posts: 244

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



SCJP 6 [SCJP - Old is Gold]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32654
    
    4
== 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

Joined: Aug 19, 2010
Posts: 13
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

Joined: Sep 29, 2008
Posts: 12617

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
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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.

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Deena Lee
Greenhorn

Joined: Aug 19, 2010
Posts: 13
Thanks everyone! I've got it working!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32654
    
    4
You're welcome . As an alternative to the != operator, you could try the XOR operator (caret = ^).
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Dealing with ArrayList items in a static method
 
Similar Threads
constructor problems
cant find API method problems
cant find symbol problems
cant find variables problems
static problems