如何从存储在数组列表的对象拉场? [英] How to pull a field from an object stored in an arraylist?

查看:209
本文介绍了如何从存储在数组列表的对象拉场?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能得到一些工作code的例子,我可以选择分开,研究他们是如何工作的?
具体来说,我需要弄清楚如何从我的列表中编号为对象,其x获取地址栏?
如何删除列表中的第X个对象?
我如何对象x的价格字段设置为一个新的价值?

Can I get some working code examples that I can pick apart and study how they work? Specifically, I need to figure out how do I get the address field from the x numbered object in my list? How do I delete the xth object from the list? How do I set the price field of object x to a new value?

我知道如何在 houseList.add 会将数据转换为追加到列表的末尾一个新的对象,但我不明白它的相反以及如何针对特定对象,并对其进行操作。请出示工作多类的例子。谢谢!

I understand how the houseList.add places the data into a new object that is appended to the end of the list, but I do not understand the converse of it and how to target a specific object and manipulate it. Please show a working multi-class example. Thanks!

创建众议院对象,它拥有以下信息类;

The class that creates the House object and it holds the following information;

public House(int address, String street, double price, int rooms) {
    this.address = address;
    this.street = street;
    this.price = price;
    this.rooms = rooms;
}

和它具有以下的方法...

and it has the following methods...

public void SetAddress (int address){
    setAddress(address);
}

private void setAddress (int address){
    this.address = address;
}

public int GetAddress (int address){
    address = getAddress(address);
    return address;
}

private int getAddress (int address){
    return address;
}

@Override
public String toString() {
    return address + " " + street + " " + price + " " + rooms;
}

等等......

etc...

在另一个类,我宣布一个名为houseList府对象的名单,它是一个ArrayList

In another class, I declare a List of House objects named houseList and that it is an ArrayList.

List<House> houseList;
houseList = new ArrayList();

,可以用数据填充它,

and can fill it with data,

houseList.add(new House(this.address, this.street, this.price, this.rooms));  

和遍历它...

  public void showHouses() {
    Iterator<House> itr = houseList.iterator();
    while (itr.hasNext()) {
        System.out.println(itr.next().toString());
    }
}

到这一点,没有我的房子方法如上面列出的发挥作用,除了已经覆盖了toString方法。我一直无法弄清楚是如何拉this.street或this.price为列表的第x个元素。我可以将数据置于清单,
    houseList.add(新楼(this.address,this.street,this.price,this.rooms));结果
我可以看到通过迭代时,预期我的数据存储。我有这些getter和setter方法​​,但我不理解它们是如何被用于拉出这些项目或者设置它们。由于我用houseList.add,我想加相反会得到,但没有想通了正确的语法。

up to this point, none of my House methods as listed above come into play, except for the toString method which has been overridden. What I have not been able to figure out is how to pull this.street or this.price for the xth element of the list. I can place data into the list with houseList.add(new House(this.address, this.street, this.price, this.rooms));
and I can see my data is stored as expected when iterated through. I have these setters and getters, but I am not understanding how they are to be used to pull these items out or set them in. Since I used houseList.add, I figured the opposite of add would be to get, but haven't figured out the correct syntax.

推荐答案

修复干将

首先,应对getter / setter方法​​。下面的getter:

First, deal with the getters/setters. The following getter:

private int getAddress (int address){
    return address;
}

应该是固定的,看起来像这样:

should be fixed to look like this:

private int getAddress() {
    return address;
}

在您的code,你的的getAddress(INT地址)方法有一个int参数,并直接返回参数。它甚至没有看现场存储地址值。

In your code, your getAddress(int address) method has an int parameter, and returns the argument directly. It doesn't even look at the field storing the address value.

遍历列表

您也可以重写此:

public void showHouses() {
    Iterator<House> itr = houseList.iterator();
    while (itr.hasNext()) {
        System.out.println(itr.next().toString());
    }
}

在一个更紧凑的方式,使用Java 5风格循环,像这样的:

in a more compact way, using a Java-5 style for loop, like this:

public void showHouses() {
    for (House house : houseList) {
        System.out.println(house);
        // or, say: System.out.println(house.getAddress());
    }
}

还是这个样子,如果你真的想用一个迭代器:

or like this, if you really want to use an iterator:

public void showHouses() {
    Iterator<House> itr = houseList.iterator();
    while (itr.hasNext()) {
        House house = itr.next();
        System.out.println(house);
        // or, say: System.out.println(house.getAddress());
    }
}

请注意,在上面的例子中,你通常需要小心调用的next()每次迭代一次。那就是:分配 itr.next()来一个局部变量,并用它,而不是调用 itr.next()不是每次迭代一次。

Note that in the example above, you typically need to be careful to call next() only once per iteration. That is: assign itr.next() to a local variable, and use that, instead of calling itr.next() more than once per iteration.

的第一方式的优点是,它的短;而很明显,你正在做的是遍历集合,不以任何方式修改它(尽管你仍然可以修改它的值)。

The advantage of the first way is that it's shorter; and it's clear that all you're doing is iterating over the collection, and not modifying it in any way (although you can still modify its values).

的第二种方式的好处是,你可以叫 itr.remove()你遍历(迭代)从集合中删除元素它。

The advantage of the second way is that you can call itr.remove() to remove elements from the collection as you traverse (iterate over) it.

第X个房子

如果您需要第X个房子,你可以这样写:

If you need the x'th house, you can write:

int x = ...;
House h = houseList.get(x);
System.out.println(h);
// or: System.out.println(h.getAddress());

编辑:

另外还有一个模式您在code,有时看到遍历一个列表:

Here's one more pattern you sometimes see in code that traverses a list:

for (Iterator<House> it = houseList.iterator(); it.hasNext();) {
    House house = it.next();
    // Blah, blah..
}

此具有相同的优点与上述基于迭代器的遍历。但是,此版本限制了迭代变量()的循环体的范围。在上面的版本中,迭代变量的范围是整个方法体。因此,它更容易看到你从来不使用迭代器外循环。

This has the same advantages as the iterator-based traversal above. However, this version limits the scope of the iterator variable (it) to the loop body. In the version above, the scope of the iterator variable is the whole method body. Hence, it's easier to see that you never use the iterator outside the loop.

限制范围的循环体也是有用的,当你想在一个方法(或其他块)执行多个基于迭代器的遍历。它也允许对第二回路您重新使用相同的变量名称(即使在一个不同类型的集合的第二循环迭代)。

Limiting the scope to the loop body is also useful when you want to perform more than one iterator-based traversal in a method (or other block). It also allows you to re-use the same variable name for the second loop (even if the second loop iterates over a collection of a different type).

这篇关于如何从存储在数组列表的对象拉场?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆