Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using Lists/ArrayLists, code doesn't work

 
David Pountney
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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

Look here:
Inteface
 
David Pountney
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic