aspose file tools*
The moose likes Beginning Java and the fly likes Using Lists/ArrayLists, code doesn't work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using Lists/ArrayLists, code doesn Watch "Using Lists/ArrayLists, code doesn New topic
Author

Using Lists/ArrayLists, code doesn't work

David Pountney
Greenhorn

Joined: May 26, 2011
Posts: 27
Hello,

I'm trying to pass a Model (the name of a class) into a method but I'm not getting any results, here is the code;



The output I get is nothing, when it should just generate 10 number 1's. As the player1 and player 2 WS variable values are different. I've tracked the problem down to the "If player1.WS > player2.WS" line in the code, I'm not sure why it doesn't make a comparrison between the two different instances of the Model class.

Can anyone please suggest why my code isn't working as I expected?

Thanks
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

Based on that code, I wouldn't expect any output. You just add the models to the list - you don't do anything with them. Is there something missing?
David Pountney
Greenhorn

Joined: May 26, 2011
Posts: 27
Oh dear, Just made an epic fail.

I didn't call the method...

Thanks for pointing that out to me, I'll get back to you if the problem I mentioned keeps happening.
Dawid Skrzypczynski
Ranch Hand

Joined: Jun 08, 2011
Posts: 52
Hi,

I think that you should use to compare object the Comparable Interface.

Look here:
Inteface
David Pountney
Greenhorn

Joined: May 26, 2011
Posts: 27
I called the method that outputs (using System.out.println) text on the screen but still I don't get anything, any ideas why?

Here is is the slightly updated code;




Thanks again
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

You're passing the same Model object to closeCombatHitting for both parameters. Which means that player1 == player2, so neither test will work.

I don't quite know what you're intending to happen, but maybe you need something like this?
David Pountney
Greenhorn

Joined: May 26, 2011
Posts: 27
Thanks for pointing that out, I've corrected it and my program does what I wanted it to do.

Just so I know, would this be a good way to remove the last element in a list?

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using Lists/ArrayLists, code doesn't work