# ArrayList.contains()



## hierUndDa (3. Jun 2011)

Hallo zusammen

In meinem Beispiel funktioniert zwar das equals() in beide Richtungen (Zeile 10 und 13), jedoch funktioniert es nicht, wenn ich contains er ArrayList gebrauche (Zeile 18), obwohl, so wie ich es verstanden habe, die ArrayList ebenfalls die equals-Methode des Objektes benutzt.



			
				http://download.oracle.com/javase/6/docs/api/java/util/ArrayList.html#contains%28java.lang.Object%29 hat gesagt.:
			
		

> public boolean contains(Object o)
> 
> Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : *o.equals(e)*).
> [...]



Mein Code:


```
package examples;

import java.util.ArrayList;

public class SimpleDecorator {

	public static void main(String[] args) {
		IMan jonny = new Man();
		IMan jonnyWithBeard = Accessoire.BEARD.add(jonny);
		if (jonny.equals(jonnyWithBeard)) {
			System.out.println("juppie!");
		}
		if (jonnyWithBeard.equals(jonny)) {
			System.out.println("juppie the second!");
		}
		ArrayList<IMan> list = new ArrayList<IMan>();
		list.add(jonny);
		if(list.contains(jonnyWithBeard)) {
			System.out.println("juppie the third!");
		}
	}
}

interface IMan {
	public boolean hasBeard();
	public boolean equals(IMan man);
	public IMan getOriginal();
}

class Man implements IMan {
	@Override
	public boolean hasBeard() {
		return false;
	}
	@Override
	public IMan getOriginal() {
		return this;
	}
	public boolean equals(IMan man) {
		try {
			return this.equals((Man) man.getOriginal());
		}
		catch (ClassCastException e) {
			return this.equals(man.getOriginal());
		}
	}
	public boolean equals(Man man){
		return this == man;
	}
}

enum Accessoire {
	BEARD;
	
	public IMan add(IMan man) {
		IMan decoratedMan = null;
		switch (this) {
			case BEARD:
				decoratedMan = new DecoratorBeard(man);
				break;
		}
		return decoratedMan;
	}
}

abstract class AbstractDecorator implements IMan {
	IMan original;
	
	public AbstractDecorator(IMan original) {
		this.original = original;
	}
	public boolean hasBeard() {
		return original.hasBeard();
	}
	public IMan getOriginal() {
		return original;
	}
	public boolean equals(IMan man){
		return original.equals(man);
	}
}

class DecoratorBeard extends AbstractDecorator {

	public DecoratorBeard(IMan original) {
		super(original);
	}

	public boolean hasBeard() {
		return true;
	}
}
```


----------



## SlaterB (3. Jun 2011)

so gesehen funktioniert  es doch nicht:

```
public static void main(String[] args)
    {
        Object jonny = new Man();
        Object jonnyWithBeard = Accessoire.BEARD.add((Man)jonny);
        if (jonny.equals(jonnyWithBeard))
        {
            System.out.println("juppie!");
        }
        if (jonnyWithBeard.equals(jonny))
        {
            System.out.println("juppie the second!");
        }
        ArrayList<IMan> list = new ArrayList<IMan>();
        list.add((Man)jonny);
        if (list.contains(jonnyWithBeard))
        {
            System.out.println("juppie the third!");
        }
    }
```


----------



## hierUndDa (3. Jun 2011)

Danke für den Tipp!

[JAVA=36]public boolean equals(Object obj) {
		if (obj instanceof IMan) {
			IMan man = (IMan) obj;
			try {
				return this.equals((Man) man.getOriginal());
			}
			catch (ClassCastException e) {
				return this.equals(man.getOriginal());
			}
		}
		else
			return false;
}[/code]

...

[JAVA=78]public boolean equals(Object obj){
		return original.equals(obj);
}[/code]


----------

