java 方法无法将ArrayList中包含的类对象识别为正确的类对象

62lalag4  于 2023-03-11  发布在  Java
关注(0)|答案(2)|浏览(133)

(如果我误用了任何术语,我道歉,我不是词汇最好的)我正在尝试编写一个程序,它迭代Person对象列表,并从随机列表中输出一个事件。对于只涉及一个Person的事件,它工作得很好,但对于多个Person,我在实现一个好的方法时遇到了麻烦。我已经通过手动从主文件调用Event方法使其工作。但这非常笨拙,而且很难在以后扩展,我想到了使用ArrayList和迭代作为实参参数的一部分的想法,因为这样更干净,更容易在以后扩展,但我遇到了一个奇怪的错误,我不知道如何修复。
这是我正在处理的三个文件。

import java.util.*;

public class LifeSimulator {
    public static void main(String[] args) {
        String P1Name = "P1";
        String P2Name = "P2";
        String P3Name = "P3";
        String P4Name = "P4";
        String P5Name = "P5";
        String P6Name = "P6";
        ArrayList<Person> pList = new ArrayList<Person>();

        Person P1 = new Person(P1Name);
        Person P2 = new Person(P2Name);
        Person P3 = new Person(P3Name);
        Person P4 = new Person(P4Name);
        Person P5 = new Person(P5Name);
        Person P6 = new Person(P6Name);

        pList.add(P1);
        pList.add(P2);
        pList.add(P3);
        pList.add(P4);
        pList.add(P5);
        pList.add(P6);
for(int s = 1; s <= 3; s++){
        System.out.println("Session 1, Section " + s);
        Collections.shuffle(pList);
        for(int i = 0; i < pList.size(); i++){
            if (pList.get(i).alreadyActed == false){
            pList.get(i).getEvent(pList.get(i), pList, i);
        }
    }

        for(int i = 0; i < pList.size(); i++){
            if(pList.get(i).alive == false){
                System.out.println(pList.get(i).getName() + " - DEAD");
                pList.remove(i);
                i--;
            }else{
                pList.get(i).refresh();
            }
        }
     }
  }
}
import java.util.*;
public class Person {
    private int lives;
    private String name;
    public boolean alive;
    public boolean alreadyActed;

    public Person(String name){
        this.name = name;
        lives = 3;
        alive = true;
        alreadyActed = false;
    }

    public String getName(){return name;}
    public int getLives(){return lives;}
    
    public void loseLife(){
        lives -= 1;
        if (lives == 0){
            System.out.println(name + " has lost their final life and is now dead!");
            alive = false;}
    }
    public void refresh(){
        alreadyActed = false;
    }

    public void getEvent(Person self, ArrayList list, int place){
        Random rand = new Random();
        int determiner = rand.nextInt(10);
        switch(determiner){
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            Events.fillerEvent(self);
            break;
            case 6:
            case 7:
            case 8:
            if (place != list.size()-1){
                Events.groupEvent(self, list.get(place+1));
            }else{
                Events.fillerEvent(self);
            }
            break;
            case 9:
            case 10:
            Events.fatalEvent(self);
            default:

        }
        alreadyActed = true;

        }

    }
import java.util.Random;
public class Events {
    static Random rand = new Random();
    
    public static void fillerEvent(Person p){
    switch(rand.nextInt(2)){
        case 0:
        System.out.println(p.getName() + " works.");
        break;
        case 1:
        System.out.println(p.getName() + " crafts.");
        break;
    }
}
public static void groupEvent(Person p1, Person p2){
    switch(rand.nextInt(2)){
        case 0:
        System.out.println(p1.getName() + " helps " + p2.getName() + ".");
        break;
        case 1:
        System.out.println(p1.getName() + " chats with " + p2.getName() + ".");
        break;
    }
}
public static void fatalEvent(Person p){
    switch(rand.nextInt(2)){
        case 0:
        System.out.println(p.getName() + " died.");
        death(p);
        break;
        case 1:
        System.out.println(p.getName() + " died.");
        death(p);
    }
}
public static void death(Person p){
    p.loseLife();
    if(p.alive){
    System.out.println(p.getName() + " has " + p.getLives() + " lives left.");
    }
}
}

每当我尝试运行代码时,当它尝试调用[Events.groupEvent(self,list.get(place+1))]时,它都会抛出以下错误:

Exception has occurred: java.lang.Error
"java.lang.Error: Unresolved compilation problem: 
    The method groupEvent(Person, Person) in the type Events is not applicable for the arguments (Person, Object)
"

ArrayList包含Person对象,至少在主文件中是这样,所以我不知道为什么它只是把它们当作泛型对象,当把列表作为参数使用时,我是做错了什么,还是你不能用ArrayList做这件事?

lp0sw83n

lp0sw83n1#

你宣称:

ArrayList<Person> pList

但是该方法丢失了Type:

getEvent(Person self, ArrayList list, int place)

您必须在声明中保留Type,否则它将丢失并成为推断的ArrayList<Object> list

getEvent(Person self, ArrayList<Person> list, int place)
vngu2lb8

vngu2lb82#

这是因为list基本上是Object的列表。

ArrayList list

由于类型擦除,实际上意味着ArrayList<Object>
您可以将此参数更改为

ArrayList<Person> list // or even List<Person>

或显式强制转换变量:

Events.groupEvent(self, (Person)list.get(place+1));

阅读详情:

相关问题