Reputation: 33
I am struggling to comprehend the ArrayList when used in a constructor.
Let's say I have the following code:
public class Names {
ArrayList<String> names;
private int number;
public Names(int numberIn, ArrayList<String> namesIn) {
number = numberIn;
names = namesIn;
}
}
If I created another class to test the names class like so:
public class NamesTester {
public static void main(String[] args) {
Names n = new Names(10, ??? );
}
}
How can I add to the array using the constructor's parameter? Do I list the names somehow where the question marks are?
I can't figure this out so any help would be much appreciated. Just to clarify, when creating a new object 'n' in the NamesTester class, I would like to call the constructor so that the number of students (10) and their names ("Jack", "John", etc.) are held in the 'names' and 'number' attributes so that when I add the setters for both, I can print them.
Upvotes: 0
Views: 1185
Reputation: 11120
Let's have a look at each step separately.
ArrayList<T>
is an ordinary Reference Type (i.e. Class), like any other class, be it a String
, Integer
, Date
, or YourCustomClass
. The only difference is that it is a generic type.
When you define method parameter (for example ArrayList<T> list
), you define it with:
ArrayList<T>
from above example);list
from the same example);Now, have a look at your code again:
public class Names {
ArrayList<String> names;
private int number;
public Names(int numberIn, ArrayList<String> namesIn) {
number = numberIn;
names = namesIn;
}
}
Your constructor (which is, by definition, a special kind of method) expects two arguments: first of type int
, and second of type ArrayList<String>
.
I assume you know how to create object, with new
keyword.
So, you can either create an object and pass its reference to the constructor, like:
ArrayList<String> myObj = new ArrayList<>();
Names names = new Names(10, myObj);
or instantiate your object inline, right when you call the constructor, like:
Names names = new Names(10, new ArrayList<String>());
Finally, what you want to do is to add the objects into your ArrayList<String>
instance, and it is done like:
ArrayList<String> list = new ArrayList<>();
list.add("Jack");
list.add("John");
//and so on.
Upvotes: 1
Reputation: 4087
First, you probably want everything declared a java.util.List
rather than ArrayList
. In the vast majority of usecases, specifying the concrete collection type is undesireable as it forces you to use only a specific implementation, and just leads to lots of headaches.
The easiest way to specify and construct a list in java is using the one of the List.of methods. Note that these lists are immutable, and cannot be changed (e.g. added to or removed from).
This would look something like:
public class Names {
List<String> names;
private int number;
public Names(int numberIn, List<String> namesIn) {
number = numberIn;
names = namesIn;
}
}
public class NamesTester {
public static void main(String[] args) {
Names n = new Names(10, List.of("Joe", "Jill", "Bob", "Bobilina" );
}
}
Upvotes: 0
Reputation: 38531
Well, you could just do :
public class NamesTester {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>();
names.add("bob");
names.add("sally");
//you can add more here till you get 10 names....
Names n = new Names(10, names );
}
}
Upvotes: 2
Reputation: 92
If i understood your question correct, you need to pass in a new ArrayList to the Names constructor class, and maybe add a method to add elements to that list, so e.g.
public class Names {
private ArrayList<String> names;
private int number;
public Names(int numberIn, ArrayList<String> namesIn) {
number = numberIn;
names = namesIn;
}
public void addName(String name) {
if (names != null) {
names.add(name);
}
}
}
public class NamesTester {
public static void main(String[] args) {
Names n = new Names(10, new ArrayList<>());
n.addName("alice");
}
}
Upvotes: 1