如何显示数组列表中的所有元素?

发布于 2024-07-18 02:49:38 字数 767 浏览 5 评论 0 原文

假设我有一个带有属性 makeregistration 的汽车类,我创建了一个 ArrayList 来存储它们。 如何显示ArrayList中的所有元素?

我现在有这个代码:

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

它编译得很好,但是当我使用这个代码在我的测试器类中尝试它时:

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

我收到不兼容类型的错误。 我的编码正确吗? 如果没有,有人可以告诉我应该如何吗?

谢谢

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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

情独悲 2024-07-25 02:49:38

你想做这样的东西吗?

public List<Car> getAll() {
    return new ArrayList<Car>(cars);
}

然后调用它:

List<Car> cars = c1.getAll();
for (Car item : cars) {   
    System.out.println(item.getMake() + " " + item.getReg());
}

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());
}
节枝 2024-07-25 02:49:38

您收到错误是因为 Car 类中的 getAll 函数返回单个 Car 并且您希望将其分配到数组中。

这确实不清楚,您可能需要发布更多代码。 为什么要向该函数传递一个 Car ? 在 Car 上调用 getAll 是什么意思?

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.

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.

蒗幽 2024-07-25 02:49:38

根本不清楚你在做什么。 您的函数 getAll() 应该返回一个 List,而不是 Car。 否则,为什么叫它 getAll 呢?

如果您有

Car[] arrayOfCars

并且想要一个列表,您可以简单地执行以下操作:

List<Car> listOfCars = Arrays.asList(arrayOfCars);

数组已记录 这里

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?

If you have

Car[] arrayOfCars

and want a List, you can simply do this:

List<Car> listOfCars = Arrays.asList(arrayOfCars);

Arrays is documented Here.

天煞孤星 2024-07-25 02:49:38

切线: 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()
}

Tangential: String.format() rocks:

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()
}
困倦 2024-07-25 02:49:38

您的 getAll() 方法未获取全部内容。 它返回第一辆车。

return 语句终止循环。

Your getAll() method does not get all. It returns the first car.

The return statement terminates the loop.

给不了的爱 2024-07-25 02:49:38

另一种方法是向 Car 类添加一个 toString() 方法,并让 ArrayList 的 toString() 方法完成所有工作。

@Override
public String toString()
{
    return "Car{" +
            "make=" + make +
            ", registration='" + registration + '\'' +
            '}';
}

您不会在输出中每行得到一辆车,但如果您只想查看数组中的内容,那么它会快速而简单。

List<Car> cars = c1.getAll();
System.out.println(cars);

输出将是这样的:

[Car{make=FORD, registration='ABC 123'},
Car{make=TOYOTA, registration='ZYZ 999'}]

Another approach is to add a toString() method to your Car class and just let the toString() method of ArrayList do all the work.

@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'}]
乖乖兔^ω^ 2024-07-25 02:49:38

设置为每个循环获取所有值

for (String member : members){
    Log.i("Member name: ", member);
}

Set for each loop to get all values

for (String member : members){
    Log.i("Member name: ", member);
}
再见回来 2024-07-25 02:49:38

您好,抱歉,第二个代码应该是:

private static void getAll(CarList c1)
我有一个名为 CarList 的类,

ArrayList <Car> cars = c1.getAll(); // error incompatible type
for(Car item : cars)
{   
      System.out.println(item.getMake()
                       + " "
                       + item.getReg()
                       );
}

CarList

其中包含 arraylist 及其方法,因此在测试器类中,我基本上有以下代码来使用该 CarList 类:

c1;
c1 = new CarList();

其他一切都有效,例如添加和删除汽车以及显示单个汽车,我只需要一个代码来显示数组列表中的所有汽车。

Hi sorry the code for the second one should be:

private static void 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 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.

最笨的告白 2024-07-25 02:49:38

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

Arraylist uses Iterator interface to traverse the elements
Use this

public void display(ArrayList<Integer> v) {
        Iterator vEnum = v.iterator();
        System.out.println("\nElements in vector:");
        while (vEnum.hasNext()) {
            System.out.print(vEnum.next() + " ");
        }
    }
空袭的梦i 2024-07-25 02:49:38

您可以使用 arraylistname.clone()

You can use arraylistname.clone()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文