Java 如何显示数组列表中的所有元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/762400/
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 display all elements in an arraylist?
提问by
Say I have a car class with attributes make
and registration
, and i create an ArrayList to store them. How do I display all the elements in the ArrayList?
假设我有一个带有属性make
和的汽车类registration
,我创建了一个 ArrayList 来存储它们。如何显示 ArrayList 中的所有元素?
I have this code right now:
我现在有这个代码:
public Car getAll()
{
for(int i = 0; i < cars.size(); i++) //cars name of arraylist
{
Car car = cars.get(i);
{
return cars.get (i);
}
}
return null;
}
It compiles fine but when I try it out in my tester class using this code:
它编译得很好,但是当我使用以下代码在我的测试器类中尝试它时:
private static void getAll(Car c1)
{
ArrayList <Car> cars = c1.getAll(); // error incompatible type
for(Car item : cars)
{
System.out.println(item.getMake()
+ " "
+ item.getReg()
);
}
}
I am getting a error of incompatible type. Is my coding correct? If not can someone please show me how it should be?
我收到类型不兼容的错误。我的编码正确吗?如果没有,有人可以告诉我它应该如何吗?
Thank You
谢谢你
回答by Darron
Your getAll()
method does not get all. It returns the first car.
你的getAll()
方法没有得到全部。它返回第一辆车。
The return
statement terminates the loop.
该return
语句终止循环。
回答by Uri
You are getting an error because your getAll function in the Car class returns a single Car and you want to assign it into an array.
您收到错误是因为 Car 类中的 getAll 函数返回一个 Car 并且您想将它分配到一个数组中。
It's really not clear and you may want to post more code. why are you passing a single Car to the function? What is the meaning of calling getAll on a Car.
这真的不清楚,您可能想发布更多代码。你为什么将单个 Car 传递给函数?在 Car 上调用 getAll 是什么意思。
回答by Esko Luontola
Are you trying to make something like this?
你想制作这样的东西吗?
public List<Car> getAll() {
return new ArrayList<Car>(cars);
}
And then calling it:
然后调用它:
List<Car> cars = c1.getAll();
for (Car item : cars) {
System.out.println(item.getMake() + " " + item.getReg());
}
回答by nsayer
It's not at all clear what you're up to. Your function getAll() should return a List<Car>, not a Car. Otherwise, why call it getAll?
根本不清楚你在做什么。你的函数 getAll() 应该返回一个 List<Car>,而不是一个 Car。否则,为什么称它为getAll?
If you have
如果你有
Car[] arrayOfCars
and want a List, you can simply do this:
并想要一个列表,你可以简单地这样做:
List<Car> listOfCars = Arrays.asList(arrayOfCars);
Arrays is documented Here.
数组记录在此处。
回答by nsayer
Hi sorry the code for the second one should be:
嗨,对不起,第二个的代码应该是:
private static void getAll(CarList c1) {
私有静态无效 getAll(CarList c1) {
ArrayList <Car> cars = c1.getAll(); // error incompatible type
for(Car item : cars)
{
System.out.println(item.getMake()
+ " "
+ item.getReg()
);
}
}
}
I have a class called CarList which contains the arraylist and its method, so in the tester class, i have basically this code to use that CarList class:
我有一个名为 CarList 的类,它包含 arraylist 及其方法,因此在测试器类中,我基本上有以下代码来使用该 CarList 类:
CarList c1; c1 = new CarList();
CarList c1; c1 = new CarList();
everything else works, such as adding and removing cars and displaying an inidividual car, i just need a code to display all cars in the arraylist.
其他一切正常,例如添加和删除汽车以及显示单个汽车,我只需要一个代码来显示数组列表中的所有汽车。
回答by Carl Manaster
Tangential: String.format() rocks:
切线: String.format() 岩石:
public String toString() {
return String.format("%s %s", getMake(), getReg());
}
private static void printAll() {
for (Car car: cars)
System.out.println(car); // invokes Car.toString()
}
回答by hallidave
Another approach is to add a toString()
method to your Car
class and just let the toString()
method of ArrayList do all the work.
另一种方法是在toString()
您的Car
类中添加一个方法,让toString()
ArrayList的方法完成所有工作。
@Override
public String toString()
{
return "Car{" +
"make=" + make +
", registration='" + registration + '\'' +
'}';
}
You don't get one car per line in the output, but it is quick and easy if you just want to see what is in the array.
您不会在输出中每行获得一辆汽车,但如果您只想查看数组中的内容,则可以快速轻松地完成。
List<Car> cars = c1.getAll();
System.out.println(cars);
Output would be something like this:
输出将是这样的:
[Car{make=FORD, registration='ABC 123'},
Car{make=TOYOTA, registration='ZYZ 999'}]
回答by Moe Sandar
You can use arraylistname.clone()
您可以使用 arraylistname.clone()
回答by BloodLoss
Arraylist uses Iterator interface to traverse the elements Use this
Arraylist 使用 Iterator 接口遍历元素 使用这个
public void display(ArrayList<Integer> v) {
Iterator vEnum = v.iterator();
System.out.println("\nElements in vector:");
while (vEnum.hasNext()) {
System.out.print(vEnum.next() + " ");
}
}