Cannot resolve constructor for ArrayList [closed]

天大地大妈咪最大 提交于 2020-01-06 18:13:38

问题


I created a new class and a constructor like this:

public class Person {
  String firstName;
  String lastName;
  String pass;

  public Person(){}

  public Person(String f,String l,String p){
    firstName=f;
    lastName=l;
    pass=p;
  }
}

Then I created an object in MainActivity:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    ArrayList<Person> list = new ArrayList<Person>();
    ArrayList<Person> list2 = new ArrayList<Person>("a","b","c");
}

The problem is that I have a message like this:

Cannot resolve constructor 'ArrayList(java.lang.String,java.lang.String,java.lang.String)'

and in Person class is says that the constructor is never used.

What did I do wrong?


回答1:


You are constructing a LIST of Persons. But you are providing arguments to create a PERSON.

You could write it as:

list2 = new ArrayList<>(Arrays.asList(new Person("a","b","c")));

Or alternatively:

list2 = new ArrayList<>(Collections.singletonList(new Person("a","b","c")));

In other languages there might be "implicit" (behind the covers) lookup of "matching" functionality; so that when you provide arguments to create a Person, the corresponding constructor is called.

In Java, that doesn't work. And note: even if that would work; ArrayList does not have a constructor that takes objects and adds them to the list.

Long story short: you have to use different syntax. You can use the suggestions by other folks; or if you prefer the "oneliner" (but "more expensive") solutions ... try playing with my suggestions. And of course: study the javadoc for the library functions that I am using; as they put certain restrictions on the returned lists.

And taking the input from 4castle: it would be better practice to not enforce the list type on the left hand side, like:

List<Person> list2 = Arrays.asList<>(new Person("a","b","c"));

Meaning: if possible, use the base collections interfaces; and not the concrete classes behind them. That will allow you later on to change the actual implementation. But if you are using "ArrayList" all over the place, you have to change each and every place where you fixed that type. So, it is good practice to avoid that where possible!




回答2:


You're instantiating an ArrayList, not a Person. For your second list, try this:

ArrayList<Person> list2 = new ArrayList<Person>();
list2.add(new Person("a","b","c"));

You need to declare your Person objects and add them after creating the List.




回答3:


The line that seems as culprit is this :

 ArrayList<Person> list2 = new ArrayList<Person>("a","b","c");

Try this:

ArrayList<Person> list2 = new ArrayList<Person>();
Person p = new Person("a","b","c");
list2.add(p)



回答4:


Always use the java documentation when you encounter such issues. Since the constructor is complaining it would be best to look at the documentation of the ArrayList constructor.

ArrayList() - Constructs an empty list with an initial capacity of ten.

ArrayList(Collection c) - Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.

ArrayList(int initialCapacity) - Constructs an empty list with the specified initial capacity.

This suggests that you definitely cannot pass Person object into the constructors. It either takes nothing, a Collection or a int. An easy way to construct a Collection when you have a few objects is Arrays.asList().

As mentioned by GhostCat the following definately works for the scenario that you mentioned. The reason is asList will return an ArrayList which is what you want.

List<Person> list = Arrays.asList(new Person("a","b","c")));

But if you were creating a set for example. you will have to use the following

Set<Person> set = new HashSet(Arrays.asList(new Person("a","b","c")));

It complains on the Person constructor not being used since its definetly not being used. The constructor usage has to be always followed by the "new" keyword. In your case it is not.




回答5:


You are using Strings instead of Persons.

You need to do something like this:

ArrayList<Person> list = new ArrayList<Person>();
Person p = new Person (first, last, pass);
list.add (p);

You could also try this:

ArrayList<Person> list = new ArrayList<Person>() {{
    add(new Person (first1, last1, pass1));
    add(new Person (first2, last2, pass2));
    add(new Person (first3, last3, pass3));
}};

Let me know if helped. If not I could try more.



来源:https://stackoverflow.com/questions/38982845/cannot-resolve-constructor-for-arraylist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!