简体   繁体   English

如何检索存储在Java ArrayList中的对象值

[英]How to retrieve objects values stored in a Java ArrayList

ArrayList<yellowPage> ob1 = new ArrayList<yellowPage>(); 
yellowPage thing = new yellowPage(100,100);
thing.calc(i,y,s3); 
ob1.add(thing);

I stored some data in thing . 我在thing存储了一些数据。 How can I retrieve the value stored in ob1.thing ? 如何检索存储在ob1.thing的值?

If you know the index, you can do yellowPage 如果您知道索引,则可以执行yellowPage

yellowPage yp = ob1.get(index);

Otherwise you need to iterate over the list. 否则,您需要遍历列表。

Iterator<yellowPate> iter = ob1.iterator();
while(iter.hasNext())
{
    yellowPage yp = iter.next();
    yp.whateverYouwantGet();
}

Note: I just typed code here, there may be syntax errors. 注意:我只是在这里输入代码,可能存在语法错误。

int x=5;
int info=ob1.get(x).getInfo();

The above example will get whatever information you wanted from your yellow pages class (by using a getter method) at the 6th index (because 0 counts) of your array list ob1. 上面的示例将在您的数组列表ob1的第6个索引(因为0个计数)中从黄页类(通过使用getter方法)获得您想要的任何信息。 This example assumes you want an integer from the yellow page. 此示例假定您需要黄页中的整数。 You will have to create a getter method and change the x to the index of the yellow page you want to retrieve information from. 您必须创建一个getter方法并将x更改为要从中检索信息的黄页的索引。

An example getter method (which you should put in your yellow pages class) could look like this: 一个示例getter方法(您应该放在黄页类中)可能如下所示:

public int getInfo() { return z; }

In the above case z may be an instance variable in your yellow pages class, containing the information you're looking for. 在上面的例子中,z可能是黄页类中的实例变量,包含您要查找的信息。 You will most probably have to change this to suit your own situation. 您很可能必须根据自己的情况进行更改。

If you wanted to get information from all yellow pages stored in the array list then you will need to iterate through it as Chrandra Sekhar suggested 如果你想从存储在数组列表中的所有黄页中获取信息,那么你将需要迭代它,如Chrandra Sekhar建议的那样

Print the list and override toString method. 打印列表并覆盖toString方法。

public String toString()
{
 return (""+ a+b);    //Here a and b are int fields declared in class
}

System.out.print(ob1);

Use an Iterator object to do this. 使用Iterator对象执行此操作。

 ArrayList<yellowPage> ob1 = new ArrayList<yellowPage>(); 
 yellowPage thing = new yellowPage(100,100);
 thing.calc(i,y,s3);    
 ob1.add(thing);
 yelloPage retrievedThing = null;
 Iterator<yelloPage> i = ob1.iterator();
 if(i.hasNext()){
      retrievedThing = i.next();
 }

You could have the data stored in thing (horribly named variable) simply returned from the calc method. 您可以将数据存储在thing (可怕的命名变量)中,只需从calc方法返回。 That way you don't need to maintain state for prior calculations in subsequent calls. 这样,您不需要在后续调用中维护先前计算的状态。 Otherwise you just need a getter type method on the YellowPage class. 否则,您只需要在YellowPage类上使用getter类型方法。

public class YellowPage { 

    private int result;

    public void calc(...) { 
        result = ...
    }

    public int getResult() { 
        return result;
    }

}
Class ArrayList<E>

Syntax 句法

ArrayList<Integer> list = new ArrayList<Integer>();

You replace "Integer" with the class that the list is of. 您将“Integer”替换为列表所在的类。 An application can increase the capacity of an ArrayList instance before adding a large number of elements using the ensureCapacity operation. 在使用ensureCapacity操作添加大量元素之前,应用程序可以增加ArrayList实例的容量。 This may reduce the amount of incremental reallocation. 这可能会减少增量重新分配的数量。 E represents an Element, which could be any class. E代表一个元素,可以是任何类。 ensureCapacity is used to ensure that the list has enough capacity to take in the new elements. ensureCapacity用于确保列表具有足够的容量来接收新元素。 It's called internally every time you add a new item to the list. 每次向列表中添加新项目时都会在内部调用它。 As the name suggests, ArrayList uses an Array to store the items. 顾名思义, ArrayList使用Array来存储项目。 So when the array is initialized, it's given an arbitrary length, say 10. Now once you've added 10 items, if you go to add the 11th item, it'll crash because it exceeds the arrays capacity. 因此,当数组初始化时,它被赋予一个任意长度,比如10.现在你添加了10个项目,如果你去添加第11个项目,它会因为超过数组容量而崩溃。 Hence, ensureCapacity is called (internally) to ensure that there's enough space. 因此,在内部调用ensureCapacity以确保有足够的空间。 So if you were adding the 11th element, the array size might be, say, doubled, to 20. 因此,如果您要添加第11个元素,则数组大小可能会加倍,达到20。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM