Java 对于链表添加方法,如何将字符串输入到对象(数组)中?

Java 对于链表添加方法,如何将字符串输入到对象(数组)中?,java,Java,我目前正在为我的计算机科学3课程编写最后一个课程,我需要编写一个Object LinkedList tester类(在本例中是一个名为ListTeset.java的tester ActorLinkedList.java),其中一个要求是编写一个add方法,在列表的开头添加一个Actor对象。但当尝试向其中添加Actor对象名称(字符串)时,会显示不兼容的类型错误 我尝试将其放置在包含字符串的单独对象中,并使用.setName()命令,但无法正确创建连接,导致错误 public class Act

我目前正在为我的计算机科学3课程编写最后一个课程,我需要编写一个Object LinkedList tester类(在本例中是一个名为ListTeset.java的tester ActorLinkedList.java),其中一个要求是编写一个add方法,在列表的开头添加一个Actor对象。但当尝试向其中添加Actor对象名称(字符串)时,会显示不兼容的类型错误

我尝试将其放置在包含字符串的单独对象中,并使用.setName()命令,但无法正确创建连接,导致错误

public class Actor {

    String name;
    Actor next;

    Actor(){
        next = null;
        name = null;
    }

    Actor (String name){
        this.name = name;
        this.next = null;
    }

    String getName() {
        return name;
    }


    void setName (String name){
        this.name = name;
    }

    Actor getNextPtr(){
        return this.next;
    }

    void setNextPtr(Actor next){

        this.next = next;

    }
    @Override
       public String toString(){

           return "\nActor{ name = " + name  + '}';


       }


}
//-------------------------
public class ActorLinkedList {
    Actor head;

    int count;

    ActorLinkedList(){
        head = null;
    }

    public ActorLinkedList(Actor head, int count) {
        this.head = head;
        this.count = count;
    }

    void add(Actor actor){

        Actor add = new Actor();

        add.next = head;

        head = add;
    }

    Actor get(int index){
        int i;
        Actor current;

        current = head;

        for(i = 0; i  < count; i++){

            if(i == index-1)
                return(current);

            if(current == null)
                return(null);

            current = current.getNextPtr();

        }
        return null;
    }

    int size(){ return count; }


}
//----------------
public class ListTester {
    public static void main(String args[]){

        ActorLinkedList list = new ActorLinkedList();

        Actor act1 = "Bradly Madly";

        list.add(act1);


    }

}
公共类参与者{
字符串名;
演员其次;
演员(){
next=null;
name=null;
}
参与者(字符串名称){
this.name=名称;
this.next=null;
}
字符串getName(){
返回名称;
}
void setName(字符串名称){
this.name=名称;
}
Actor getNextPtr(){
把这个还给我,下一个;
}
void setNextPtr(Actor next){
this.next=next;
}
@凌驾
公共字符串toString(){
返回“\nActor{name=“+name+'}”;
}
}
//-------------------------
公共类ActorLink列表{
演员头;
整数计数;
ActorLink列表(){
head=null;
}
公共ActorLink列表(演员头,整数计数){
这个头=头;
this.count=计数;
}
无效添加(参与者){
参与者添加=新参与者();
add.next=头部;
头=加;
}
参与者获取(int索引){
int i;
演员电流;
电流=水头;
对于(i=0;i

它应该在linkedList中输入一个Actor对象名称,但会导致不兼容的类型。

为了创建类的对象,您需要使用在类中定义的构造函数,即

Actor act1 = new Actor("Bradly Madly");

在爪哇,我们应该创建一个不同于C++的关键字“Neo”的对象。然后加载构造函数,该构造函数将初始化由于new关键字而分配的内存

您如何将字符串分配给您自己的自定义对象参与者?它不会给出编译错误吗?
You need to create the Actor object properly
in the ListTester class 
Actor act1 = "Bradly Madly";//this is wrong
you are trying to assign string value to Actor object this is causing type mismatch you need to create the actor object properly check the below code you can use any one step
step 1)
  Actor act1 = new Actor("Bradly Madly");
          **OR**
step 2) Actor act1= new Actor();
act1.setName("Bradly Madly");