java 无法推断 ArrayList<> 的类型参数

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

cannot infer type arguments for ArrayList<>

java

提问by mg22

I'm trying to make an ArrayList that contains an object of another class, a name, and turn. something similar to the python's dictionary.

我正在尝试创建一个包含另一个类的对象、名称和轮次的 ArrayList。类似于python的字典的东西。

self.user1 = {"user":user1,"name":empty,"turn":empty}

so I made a class that has the 3 values.

所以我做了一个具有 3 个值的类。

class User{
public userInterface user1;
String name;
String turn;

public User(UserInterface user1,String name,String turn) {
    this.user1=user1;
    this.name=name;
    this.turn=turn;
}}

and I'm trying to call it in the constructor of main class as following:

我试图在主类的构造函数中调用它,如下所示:

public class MainClassConstructon{
ArrayList<User> user1;
ArrayList<User> user2;

MainClassConstructon(UserInterface user1 ,UserInterface user2){
     this.user1 = new ArrayList<>(new User(user1,empty, empty));
     this.user2 = new ArrayList<>(new User(user2,empty, empty));

but it raises an error saying that: cannot infer type arguments for ArrayList<>.

但它引发了一个错误,指出:无法推断 ArrayList<> 的类型参数。

回答by Jeroen Steenbeeke

ArrayList has three constructors:

ArrayList 有三个构造函数:

public ArrayList(int initialCapacity)
public ArrayList()
public ArrayList(Collection<? extends E> c)

Useris neither an intnor a Collection, and you're passing an argument so the middle constructor doesn't apply either.

User既不是 anint也不是 a Collection,并且您正在传递一个参数,因此中间构造函数也不适用。

But that's beside the point, your goal is to create a single list of users, so instead of doing what you're currently doing, you need to use only a single list, and simply add your users:

但这不是重点,您的目标是创建一个用户列表,因此您无需执行当前正在执行的操作,只需使用一个列表,然后添加您的用户即可:

public class MainClassConstructon{
  List<User> users; // Or ArrayList, doesn't really matter

  MainClassConstructon(UserInterface user1 ,UserInterface user2){
    users = new ArrayList<>(); // Diamond syntax, requires Java 7+
    users.add(new User(user1, "", ""));
    users.add(new User(user2, "", ""));
  }
}

回答by mg22

well i made it like this:

好吧,我是这样制作的:

this.user1 = new ArrayList<>(Arrays.asList(new User(user1,empty, empty)));

and was able to access it by:

并且能够通过以下方式访问它:

t.user1.get(0).name="bla bla";

回答by ckoidl

you can try it like this

你可以这样试试

public class MainClassConstructon {
    ArrayList<User> user1 = new ArrayList<>();
    ArrayList<User> user2 = new ArrayList<>();

    MainClassConstructon(UserInterface user1, UserInterface user2) {
        this.user1.add(new User(user1, "", ""));
        this.user2.add(new User(user2, "", ""));
    }
}

回答by Daniel Arad

I'm not a java developer but i suggest you try and look on Arraylist api.

我不是 Java 开发人员,但我建议您尝试查看 Arraylist api。

Here is what i found online, sry if it doesn't help :)

这是我在网上找到的,如果没有帮助,请sry :)

import java.util.*; 
public class ArrayListDemo { public static void main(String args[]) { 
// create an array list 
ArrayList al = new ArrayList(); 
System.out.println("Initial size of al: " + al.size()); 
// add elements to the array list 
al.add("C"); 
al.add("A"); 
al.add("E"); 
al.add("B"); 
al.add("D"); 
al.add("F"); 
al.add(1, "A2"); 
System.out.println("Size of al after additions: " + al.size());

 // display the array list System.out.println("Contents of al: " + al);

 // Remove elements from the array list al.remove("F");
al.remove(2); 
System.out.println("Size of al after deletions: " + al.size());
 System.out.println("Contents of al: " + al); 
}
}