英文:
ArrayList as a paramater in constructor
问题
我在理解构造函数中使用ArrayList时遇到了困难。
假设我有以下的代码:
public class Names {
ArrayList<String> names;
private int number;
public Names(int numberIn, ArrayList<String> namesIn) {
number = numberIn;
names = namesIn;
}
}
如果我创建另一个类来测试Names类,就像这样:
public class NamesTester {
public static void main(String[] args) {
Names n = new Names(10, ??? );
}
}
我该如何通过构造函数的参数向数组中添加元素?我应该在问号的位置以某种方式列出这些名字吗?
我无法弄清楚这一点,所以非常感谢任何帮助。
只是为了澄清,在NamesTester类中创建一个新对象'n'时,我想调用构造函数,以便学生的数量(10)和他们的姓名("Jack"、"John"等)保存在'names'和'number'属性中,这样当我为两者都添加setter后,我可以打印它们。
英文:
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.
答案1
得分: 2
好的,以下是翻译好的内容:
好的,你可以直接这样做:
public class NamesTester {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>();
names.add("bob");
names.add("sally");
//在这里添加更多名称,直到达到10个...
Names n = new Names(10, names);
}
}
英文:
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 );
}
}
答案2
得分: 1
如果我理解你的问题正确的话,你需要将一个新的ArrayList传递给Names构造类,并且可能需要添加一个方法来向该列表中添加元素,例如:
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");
}
}
英文:
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");
}
}
答案3
得分: 1
让我们逐步看每个步骤。
ArrayList<T>
是一个普通的引用类型(即类),就像任何其他类一样,可以是String
,Integer
,Date
或YourCustomClass
。唯一的区别是它是一种泛型类型。
当你定义方法参数(例如ArrayList<T> list
),你使用以下方式定义:
- 参数类型(在上面的例子中是
ArrayList<T>
); - 变量标识符(变量名,在同一个例子中是
list
);
现在,再次查看你的代码:
public class Names {
ArrayList<String> names;
private int number;
public Names(int numberIn, ArrayList<String> namesIn) {
number = numberIn;
names = namesIn;
}
}
你的构造函数(根据定义,它是一种特殊类型的方法)需要两个参数:第一个是类型为int
,第二个是类型为ArrayList<String>
。
我假设你知道如何使用new
关键字创建对象。
因此,你可以创建一个对象并将其引用传递给构造函数,例如:
ArrayList<String> myObj = new ArrayList<>();
Names names = new Names(10, myObj);
或者在调用构造函数时直接内联实例化对象,例如:
Names names = new Names(10, new ArrayList<String>());
最后,你想要做的是将对象添加到ArrayList<String>
实例中,可以这样做:
ArrayList<String> list = new ArrayList<>();
list.add("Jack");
list.add("John");
//以此类推。
英文:
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:
- Parameter Type (which is
ArrayList<T>
from above example); - Variable identifier (variable name, which is
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.
答案4
得分: 0
首先,您可能希望将所有声明为 java.util.List
而不是 ArrayList
。在绝大多数情况下,指定具体的集合类型是不理想的,因为它强制您仅使用特定的实现,而且会导致许多麻烦。
在 Java 中指定和构造列表的最简单方法是使用 List.of 方法之一。请注意,这些列表是不可变的,不能更改(例如无法添加或删除)。
代码示例如下:
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"));
}
}
英文:
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" );
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论