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.
self.user1 = {"user":user1,"name":empty,"turn":empty}
so I made a class that has the 3 values.
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 has three constructors:
public ArrayList(int initialCapacity)
public ArrayList()
public ArrayList(Collection<? extends E> c)
User
is neither an int
nor a Collection
, and you're passing an argument so the middle constructor doesn't apply either.
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, "", ""));
}
}
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, "", ""));
}
}
I'm not a java developer but i suggest you try and look on Arraylist api.
Here is what i found online, sry if it doesn't help :)
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);
}
}
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";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.