3

Possible Duplicate:
How do you cast a List of objects from one type to another in Java?

Searched the internet a little, and found no nice way of doing it... My solution now is:

public class A {}
List<Object> obj = new ArrayList<Object>();
obj.add(new A());
// Ugly solution here:
List<A> a = (List<A>) (List) obj;

But this is quite ugly and gets a warning. No "official" way of doing this?

EDIT: To the guys who closed this: I was aware of the solution posted in How do you cast a List of objects from one type to another in Java? It is the same as the one I posted in my question (just adding the <?> after the first cast does exactly the same) I Was looking for something more "clean". In the direction of using the Class<?> clazz = listobj.get(0).getClass way of getting the class and casting to the correct class at runtime (but no idea if something like that works... Eclipse doesn't seem to like it anyway...)


My answer is not based on research, it is how I think of simulating tactics in a small scope.

This is how I made my simulation:

The first thing that you need to do is making sure you can implement the tactics that you want to compare. If there is no way you can program them, then there is no way you could test them on your own.

Write the test scope down, you could expand your project every time you have a new idea. When you do that you are creating a project that never will finish. So mark what you want to do and consider every option. Bluffing is a good example. It's almost impossible to implement a bluff simulation, you could calculate how much chance you have to win, but a normal person bluffs for example when he thinks that other persons will pass or that he will win. It is a lot of work to implement “feelings” in a program. Not every one has the same feelings about bluffing. But you could make different bluff sets (feelings) and let the bot use them with every tactic. This will be a lot of work, in my case this was not worth it.

Find out how you want to compare the tactics. Do you want to know how much % change you have to win? Or do you want to do some random games for some other reason?

There are more dialects of every game, find out what set of rules you want to follow. Implement those rules. The simulation will be incorrect when you don't do this.

Implement all tactics, and use the same functions as much as possible. This way you're testing the core functions automatically. With this approach you can be done sooner and you will find bugs that you never would been found.

Test the system. Testing is a very important with simulating. One small problem can cause a wrong result. Walk over your code with a debugger to know if it does the ride thing. Make a "debug mode", show in this mode the results of every game and review them. Also let other persons review how the games are played.

4

3 に答える 3

8
List<Object> obj = new ArrayList<Object>();
obj.add(new A());

コードの正しい書き方ではありません。基本的に、一般的な List を作成し、そこに Object を追加すると、型が安全ではなくなり、オブジェクト型が保持されます。

List<Object> obj = new ArrayList<Object>();
obj.add(new A());
obj.add(new String("str"));
obj.add(1);

タイプセーフListのようなものを作成することをお勧めしますList<A> obj = new ArrayList<A>();

あなたはそのような方法でこれを行うことができます -

public <T>List<T> castCollection(List srcList, Class<T> clas){
    List<T> list =new ArrayList<T>();
    for (Object obj : srcList) {
    if(obj!=null && clas.isAssignableFrom(obj.getClass()))
        list.add(clas.cast(obj));
    }
    return list;
}
于 2013-01-08T09:41:47.230 に答える
6

では、なぜあなたのリストは次のように宣言されていないのですか

List<A> obj = new ArrayList<A>();

そもそも?

やろうとしていることをやるのは本当に危険です。あなたが言っているのは、あなたのリストにはAs とそのサブクラスが含まれているということです。しかし、もともとはObjects (つまり何でも) のリストであるためです。後で、厄介な驚きを得る可能性があります。

そのコンパイラ エラーが実際に意味することは、どこかに設計上の問題があるということです。

于 2013-01-08T09:38:29.363 に答える
2

おそらく問題は、異なる (ただし継承に関連する) タイプのオブジェクトをリストに入れたいということでしょうか? その場合は、Generics Tutorial、特に と に関する部分<? extends Something>を読んでください。<? super Something>

乾杯、

于 2013-01-08T09:41:13.020 に答える