Sign in to follow this  
dxFoo

[java] ArrayList contain() question

Recommended Posts

dxFoo    100
This is my DiceCup class...
import java.util.ArrayList;

class DiceCup 
{
	public ArrayList<Die> alDice;		// 5 dice objects. 
	
	public DiceCup()
	{		
		alDice = new ArrayList<Die>(5);	// Instantiate all five dice objects.  
		
		for (int i = 0; i < 5; i++)
			alDice.add(new Die()); 	
	}
	
	// Roll the dice
	public void rollDice()
	{
		for (Die nextDie : alDice)
			nextDie.roll(); 			// Roll all five dice.
			
		showRolledDice();				// Display rolled dice.
	}	 
	
	// Display current rolled dice
	public void showRolledDice()
	{
		System.out.print("Rolled Dice: ");
		
		for (Die nextDie : alDice)
			System.out.print(nextDie.getFaceValue() + " ");
		
		System.out.println(); 
	}
}


In my Player class...
	    for (int i = 0; i < cup.alDice.size(); i++)
	    {
	    	if (cup.alDice.get(i).contains(cup.alDice.get(i).getFaceValue() == 6))
	    	{
	    	}
	    }


Yeah... there's a problem here. I want to go through each dice object in the cup's array list of 5 dice. If one of the dice contains a face value of 6, then do whatever. How do I look at an array list object with contains()? Thanks, dxfoo

Share this post


Link to post
Share on other sites
Argus2    140
You don't want to use 'contains'. 'contains' looks for an object in the list that equals the argument to the 'contains' method.

What you want to do is to go through the list of Die objects, check whether their 'face value' is 6, and act accordingly.

ie.

for (int i = 0; i < cup.alDice.size(); i++)
{
if(((Die)cup.alDice.get(i)).getFaceValue() == 6))
{
// do whatever
}
}


But if you *really* wanted to use 'contains', then in your Die class, override 'equals' with:


public boolean equals(Object o)
{
if(!(o instanceof Die)) return false;

if(((Die)o).getFaceValue() == this.getFaceValue()) return true;
else return false;
}


Then you do something like:

if(cup.alDice.contains(new Die(6)) // do whatever;


Share this post


Link to post
Share on other sites
Aldacron    4544
Quote:
Original post by dxFooYeah... there's a problem here. I want to go through each dice object in the cup's array list of 5 dice. If one of the dice contains a face value of 6, then do whatever. How do I look at an array list object with contains()?


Look very carefully at what your code is doing. First, you aren't calling ArrayList.contains, you are calling contains on a Die object. Second, ArrayList.contains accepts an Object argument, but you are passing a boolean. I'll break your code down to illustrate:



for (int i = 0; i < cup.alDice.size(); i++)
{
// your code:
// if (cup.alDice.get(i).contains(cup.alDice.get(i).getFaceValue() == 6)) {}

// translation:

// ArrayList<Die>.get returns an instance of Die
Die die = cup.alDice.get(i);

// alDice.get(i).contains is calling a contains method on the return
// value of alDice.get, which in this case is a Die
die.contains(...);

// (alDice.get(i).getFaceValue() == 6) results in a boolean, not an Object
boolean bool = cup.alDice.get(i).getFaceValue() == 6;

// so your code is doing this:
if(die.contains(bool)) {}
}





Aside from calling the wrong function on the wrong object with the wrong argument, your design is horrid. What happens if you want to switch from an ArrayList to a different container in the future? There's no reason for the Player object to be operating directly on the dice list. The DiceCup should make the list private and provide an interface for fetching dice.

When you really need to use the contains method on a List, there's no reason to iterate every object in the List. The List will do that for you internally. Contains exists to let you know if a specific object is currently a member of the List. What you are really trying to do here is to iterate the List and determine if any objects in the list have a face value of 6.

With that information, you might want to consider implementing something like this:


class DiceCup {
// make the ArrayList private!
private ArrayList<Die> dice;

... // constructor & other methods

// new accessor method
public boolean containsFaceValue(int val) {
boolean ret = false;
for(Die die : dice) {
if(die.getFaceValue() == val) {
ret = true;
break;
}
}
return ret;
}
}




From there you can add other methods to carry out different operations on all of the dice in the list, or to fetch individual Die objects, or to return an array or Set of all of the dice in the List (the idea being to avoid manipulating the List directly so that you don't inadvertently change anything). The Hotspot VM likes encapsulation, so don't be afraid to use it.

Share this post


Link to post
Share on other sites
Son of Cain    480
Of course I haven't seen your design as a whole, but why do you need to store Die instances, rather than their faces' values?



class DiceCup {

private ArrayList < Integer > dicePool;

DiceCup(int size) {
dicePool = new ArrayList < Integer > (size);
for (int i = 0; i < size; i++) {
int face = Dice.roll(); // use a static method, why not?
dicePool.add(face); // autoboxing
}
}

// Now you can use contains()
public final boolean containsFace(int face) {
return dicePool.contains(face);
}

// Table-top RPG ;)
public final int successes(int dificulty) {
int successes = 0;
for (int i = 0; i < dicePool.size(); i++) {
if ( dicePool.get(i) > dificulty ) {
succeesses++;
}
}
return successes;
}

}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this