6

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<>.

7
  • 2
    Try new ArrayList<>(Arrays.asList(new User(user1,empty, empty)));. Commented Feb 26, 2018 at 10:36
  • where does the empty come fromin MainClassConstructon? @lexicore: this would create a ArrayList<List<User>>... Commented Feb 26, 2018 at 10:38
  • Are these lists going to grow at all? If not, use Collections.singletonList. If they are going to grow, then create them empty first, then add the element. Commented Feb 26, 2018 at 10:39
  • You don't need the lists it looks like, just use fields of type User. Commented Feb 26, 2018 at 10:39
  • @EmersonCod How so? ArrayList has a (Collection<? extends E> c) constructor. Commented Feb 26, 2018 at 10:39

4 Answers 4

8

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, "", ""));
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

or users = Arrays.asList(new User(user1, "", ""), new User(user2, "", ""));
A good alternative indeed, or one could choose LinkedList instead, or perhaps an immutable implementation depending on the needs. It's good to keep in mind though, that Arrays.asList returns an implementation of List that is defined as inner class in Arrays (conveniently also called ArrayList)
I assume that the OP doesn't want to add more users once the constructor is finished. So Arrays.asList() should work as it is imutable-like
Good way of phrasing it - the implementation supports set but not add
2

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";

Comments

0

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, "", ""));
    }
}

Comments

-1

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); 
}
}

1 Comment

Code only answers are discouraged. Also that it's just copied from somewhere without any link to its sourcecode makes this just a bad answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.