如何在 Java 的 ArrayList 类中使用 add()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19208980/
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
How to use add() in ArrayList class in Java
提问by pei wang
I have a question about constructing an ArrayList, I have had a Car Class, now I need to make an ArrayList to put content in, I named it as Race, please see code below:
我有一个关于构造ArrayList的问题,我有一个Car Class,现在我需要制作一个ArrayList来放置内容,我将它命名为Race,请看下面的代码:
import java.util.ArrayList;
public class Race {
private ArrayList<Car>cars;
public Race(){
cars=new ArrayList<Car>();
}
Now I need to have a method for add content to the ArrayList, there are two ways to write that I am confused with:
现在我需要一个方法来向 ArrayList 添加内容,有两种写法让我感到困惑:
First one
第一
public void addCars(){
Car Toyota=new Car("Toyota",1.0,1.0,2.0,2.0);
Car Honda=new Car("Honda",1.0,2.0,1.0,2.0);
Car Mazda=new Car("Mazda",1.0,3.0,2.0,3.0);
Car Suzuki=new Car("Suzuki",1.0,4.0,4.0,2.0);
cars.add(Toyota);
cars.add(Honda);
cars.add(Mazda);
cars.add(Suzuki);
}
Second one
第二个
public void addCars(Object nm,String n, double s, double p, double a, double b){
Car name=new Car(n,s,p,a,b);
cars.add(name);
}
Both ways have no mistake reported when I coding, but I am not sure which one is correct, or maybe neither is correct, please help, cheers!
两种方式在我编码的时候都没有报错,但是我不确定哪个是正确的,或者都不正确,请帮助,干杯!
UPDATE:
更新:
public void addCars(Car car){
cars.add(car);
}
This is what I used finally, then I created a new class called Test that using main method to add cars individually, but there is mistake in the last line:
这是我最后使用的,然后我创建了一个名为Test的新类,使用main方法单独添加汽车,但最后一行有错误:
public class Test {
public static void main(String[] args) {
Car Toyota=new Car("Toyota",1.0,1.0,2.0,2.0);
**cars.addCars(Toyota);**
I have no idea how to fix it, please help!
我不知道如何解决它,请帮助!
回答by Xandaros
It depends on what you want to achieve. In general it is better to provide APIs and use them from the outside. In your case, however, I think your "addCars" function in used to populate your ArrayList(you should call it something like that) - which means to add predefined values to a Collection. In that case, use the first one.
这取决于您想要实现的目标。一般来说,最好提供 API 并从外部使用它们。但是,在您的情况下,我认为您的“addCars”函数用于填充您的 ArrayList(您应该这样称呼它) - 这意味着将预定义的值添加到集合中。在这种情况下,请使用第一个。
So, both of your methods are correct (though you should call the first populateCars
and the second addCar
) and should work, but you need to use them depending on your circumstance.
所以,你的两种方法都是正确的(虽然你应该调用 firstpopulateCars
和 second addCar
)并且应该可以工作,但是你需要根据你的情况使用它们。
Also, if you want to provide an API for adding Cars, let the user get a Car object himself, rather than constructing one in the add method. (You might want to change it before adding it, you never know)
另外,如果你想提供一个添加Cars的API,让用户自己获取一个Car对象,而不是在add方法中构造一个。(您可能想在添加之前更改它,您永远不知道)
So I suggest using either of the methods, but changing the first to:
所以我建议使用其中一种方法,但将第一种更改为:
public void populateCars(){
Car Toyota=new Car("Toyota",1.0,1.0,2.0,2.0);
Car Honda=new Car("Honda",1.0,2.0,1.0,2.0);
Car Mazda=new Car("Mazda",1.0,3.0,2.0,3.0);
Car Suzuki=new Car("Suzuki",1.0,4.0,4.0,2.0);
cars.add(Toyota);
cars.add(Honda);
cars.add(Mazda);
cars.add(Suzuki);
}
or the second to:
或第二个:
public void addCar(Car car){
cars.add(car);
}
回答by Java World
Both the ways you mentioned are correct but you should modify the method addCars() to take an argument of type Car .
您提到的两种方法都是正确的,但您应该修改 addCars() 方法以采用 Car 类型的参数。
For example , change the method signature to :
例如,将方法签名更改为:
public void addCars(Car car){
cars.add(car);
}
And just pass a new car like this :
然后像这样通过一辆新车:
addCars(new Car("Toyota",1.0,1.0,2.0,2.0));
回答by Hrvoje G.
The only difference is that in first case you have hard coded elements in the list and in the second approach you add them dynamically. What to use depends on your need. I suppose you will use the arraylist later on to get elements from it, do sth with them etc. Then you will use getter method on the arraylist, iterate through it and so on... This will work on both approaches you choose.
唯一的区别是,在第一种情况下,您在列表中有硬编码元素,而在第二种方法中,您动态添加它们。使用什么取决于您的需要。我想你稍后会使用 arraylist 从中获取元素,对它们做某事等等。然后你将在 arraylist 上使用 getter 方法,遍历它等等......这将适用于你选择的两种方法。
回答by Nathaniel Johnson
Both answers are correct. The first method uses a traditional approach to creating objects. The second method is sometimes called a Factory method. Using a factory method is useful when the construction of an object is externally complicated or stamping out multiple objects.
两个答案都是正确的。第一种方法使用传统方法来创建对象。第二种方法有时称为工厂方法。当对象的构造从外部复杂或冲压出多个对象时,使用工厂方法很有用。
So the answer is: The first approach is correct for objects that are reasonably simple and safe to construct. The second (factory) pattern is better for complex objects.
所以答案是:第一种方法对于构造起来相当简单且安全的对象是正确的。第二种(工厂)模式更适合复杂的对象。
回答by Luckyman Nevermore
@pei wang nice question .. Second apporach is gud as ur not hardcoding values .. You should go little bit forward .. please Use DTO design pattern in second approach use setters and getters
@pei wang 好问题.. 第二个方法是 gud,因为你不是硬编码值.. 你应该向前走一点.. 请在第二种方法中使用 DTO 设计模式,使用 setter 和 getter
回答by naveen prasanna
Add method expects object type reference for the add method if you pass any reference variable other than object type reference variable than those reference variable upcasted to object type reference.
如果传递对象类型引用变量以外的任何引用变量,而不是向上转换为对象类型引用的那些引用变量,则 Add 方法期望 add 方法的对象类型引用。