“在arraylist构造函数中找不到适合add(java.lang.String)的方法”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34137792/
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-11 15:21:48  来源:igfitidea点击:

"no suitable method found for add(java.lang.String)"in arraylist constructor?

javarandomarraylistconstructor

提问by anony

import java.util.ArrayList;
import java.util.Random;

    public class College
    {
        // instance variables - replace the example below with your own
        private ArrayList<Student> students;

    public College()
        {
            // initialise instance variables
            ArrayList<Student> students = new ArrayList<Student>();
            students.add("Student 1");
            students.add("Student 2");
            students.add("Student 3");

        }
    }

basically it highlights the .add showing the error message "java.lang.IllegalArgumentException: bound must be positive", I don't understand what I did wrong here? I looked at a lot of these kinds of problem threads here but I did exactly what they did

基本上它突出显示了 .add 显示错误消息“java.lang.IllegalArgumentException:bound must be positive”,我不明白我在这里做错了什么?我在这里查看了很多此类问题线程,但我完全按照他们所做的去做

采纳答案by Mena

You are adding Strings to a Listparametrized to take Students.

您正在将Strings添加到List参数化以获取Students。

Of course this is not going to compile.

当然,这不会编译。

  • Add a constructor to your Studentclass, taking a Stringargument (and the relevant logic within).
  • Then, use the following idiom: students.add(new Student("Student 1"));
  • 向您的Student类添加一个构造函数,接受一个String参数(以及其中的相关逻辑)。
  • 然后,使用以下习语: students.add(new Student("Student 1"));

To be noted, generics are there precisely to fail compilation at that stage.

需要注意的是,泛型正是在那个阶段编译失败。

If you had used a raw List(Java 4-style), your code would have compiled, but all sorts of evil would have happened at runtime, since you'd expect Studentobjects to be contained in your List, but you'd get Strings instead.

如果您使用原始的List(Java 4 风格),您的代码会被编译,但在运行时会发生各种邪恶,因为您希望Student对象包含在您的 中List,但您会得到Strings。

回答by John-Paul Ensign

Can you show the code of the Student class? Like others have said, you have a Student arraylist and are sending a String instead. That is an invalid argument.

你能显示Student类的代码吗?就像其他人所说的那样,您有一个 Student 数组列表,而是发送一个字符串。这是一个无效的论点。

If your Student class takes a string to be initialized, you can try:

如果您的 Student 类需要初始化一个字符串,您可以尝试:

ArrayList<Student> students = new ArrayList<Student>();
students.add(new Student("Student 1"));

回答by Aragorn

The ArrayList, private ArrayList<Student> studentscan accept only Studentobjects.You are trying to add Stringto it.

ArrayList private ArrayList<Student> students只能接受Student对象。您正在尝试向其中添加对象String

If you want the list to accept String, define it like this: private ArrayList<String> student

如果您希望列表接受字符串,请像这样定义它: private ArrayList<String> student

Or, if you want the list to be of Studentobjects, then construct Studentobjects from the strings (howdepends on the Studentobject) and add the object to list.

或者,如果您希望列表是Student对象,Student则从字符串构造对象(如何取决于Student对象)并将对象添加到列表中。