Java将对象添加到ArrayList错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19510295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 17:52:57  来源:igfitidea点击:

Java Add Object to ArrayList error

javaclassobjectarraylistadd

提问by Ser Davos

I'm new to coding java and would love some help. I'm trying to add an object of the class Fish to an arraylist call fishList

我是 Java 编码新手,希望得到一些帮助。我正在尝试将 Fish 类的对象添加到数组列表调用 fishList

in my main I have for example

在我的主要我有例如

    public static void main(String[] args) {

    Fish f1 = new Fish("Nemo");}

and in my class and constructor I have

在我的班级和构造函数中,我有

public class Fish {
protected static String name;
protected static int number;
protected static List<Fish> fistList = new ArrayList<Fish>();


public Fish(String in){
name = in;
number = 15;
fishList.add(name, number);
}

but I get an error "no suitable method found for add(string, int) method List.add(int, Fish) is not applicable (actual argument String cannot be converted to int by method invocation conversion) method List.add(Fish) is not applicable (actual an formal argument list differ in length)

但我收到一个错误“没有找到合适的方法用于 add(string, int) 方法 List.add(int, Fish) 不适用(无法通过方法调用转换将实际参数 String 转换为 int)方法 List.add(Fish)不适用(实际形式参数列表的长度不同)

How do I add objects to an arraylist properly?

如何正确地将对象添加到数组列表?

采纳答案by Mateusz Dymczyk

First of all nameand numbershouldn't be static(unless you want all the Fish to have the same name/number but then creating more than 1 instance of that class would be a waste of resources)!

首先name并且number不应该是static(除非您希望所有 Fish 都具有相同的名称/编号,但随后创建该类的 1 个以上实例将浪费资源)!

Secondly, change :

其次,改变:

fishList.add(name, number);

To:

到:

fishList.add(this);

fishList can hold references to objects of type Fish. If you try to add "name, number" Java doesn't know you mean a Fish :-)

fishList 可以保存对 Fish 类型对象的引用。如果您尝试添加“名称,数字”,Java 不知道您的意思是鱼 :-)

thispoints to the objects that is currently being created in the constructor.

this指向当前在构造函数中创建的对象。

回答by Prabhakaran Ramaswamy

you need to change fishList.add(name, number);to fishList.add(this);

你需要 fishList.add(name, number);改为fishList.add(this);

Dont create object property with static modifier unless needed. static modifier means that property belongs to class. if any one of the object of Fish modifying these properties will get changed to last modified value.

除非需要,否则不要使用静态修饰符创建对象属性。static 修饰符意味着属性属于类。如果 Fish 修改这些属性的任何一个对象将更改为上次修改的值。

protected static String name;
protected static int number;

Please modify ypur pojo like this.

请像这样修改 ypur pojo。

class Fish {

    private String name;
    private int number;

    public Fish(String name, int number) {
        super();
        this.name = name;
        this.number = number;
    }

    @Override
    public String toString() {
        return "Fish [name=" + name + ", number=" + number + "]";
    }

}

public static void main(String[] args) {

   List<Fish> fistList = new ArrayList<Fish>();
   fistList.add(new Fish("name1",1));
   fistList.add(new Fish("name1",2));
   fistList.add(new Fish("name1",3));
   fistList.add(new Fish("name1",4));
   fistList.add(new Fish("name1",5));
   fistList.add(new Fish("name1",6));
   for (Fish fish : fistList) {
      System.out.println(fish);
   }

   System.out.println(fistList.get(0)); // getting the first Fish
   System.out.println(fistList.get(1)); // getting the second Fish

}

回答by Carlos Salazar

The ArrayList you defined is parameterized to accept Fish objects, placing an object that is not a Fish in there will result in an error. Furthermore, As the other answers have mentioned, you are using the wrong form of the ArrayList.add method.

您定义的 ArrayList 被参数化为接受 Fish 对象,在其中放置一个不是 Fish 的对象会导致错误。此外,正如其他答案所提到的,您使用的是 ArrayList.add 方法的错误形式。

fishList.add(name, number);

Should be,

应该,

fishList.add(this);

回答by Bhushan Kawadkar

you cannot pass two arguments to add method of list.

你不能传递两个参数来添加列表的方法。

pass this like below

像下面这样通过

fishList.add(this);

also correct typo error of fistListto fishListin declaration of list

还更正了列表声明中fistListto 的拼写错误fishList

回答by Hariprasath

The given name and number is in String format, but you specify Fish class in your generics, so you have to add only Fish objects in your Listlike

给定的名称和数字采用字符串格式,但您在泛型中指定了 Fish 类,因此您只需在 List 中添加 Fish 对象,例如

fishList.add(this);

fishList.add(this);

回答by Arun Kumar Mudraboyina

because you used generics and declared fishList of with . So you are only allowed to add the list items of type Fish. no other types are allowed. This is the compile time type safety provided by generics. add() of List is overloaded as follows. add(T) and add(int,T) where first argument is the index and second is of any type. but you are saying add(String,int) which is not there in java.util.List.

因为您使用了泛型并声明了 with 的 fishList 。所以你只能添加 Fish 类型的列表项。不允许其他类型。这是泛型提供的编译时类型安全。List 的 add() 重载如下。add(T) 和 add(int,T) 其中第一个参数是索引,第二个参数是任何类型。但是你说的 add(String,int) 在 java.util.List 中不存在。

static fields are never recomanded unless and until they are required.because they shows huge impact in performance. so remove static in properties declaration.

除非需要静态字段,否则永远不会重新使用它们。因为它们对性能产生巨大影响。所以删除属性声明中的静态。

If the property 'number' may takes different values then dont assign default value 15 in its constructor rather do like this.

如果属性 'number' 可能采用不同的值,则不要在其构造函数中分配默认值 15 而是这样做。

private String name;
private int number;

public Fish(String name,int number){
this.name = name;
this.number = number;
}

replace the above statement firstList.add(name,number);with

取代上述说法firstList.add(name,number);

firstList.add(new Fish("abc",10));