user768990
user768990

Reputation: 89

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. How can I retrieve the value stored in ob1.thing?

Upvotes: 5

Views: 61886

Answers (6)

Vivek Patil
Vivek Patil

Reputation: 1

Class ArrayList<E>

Syntax

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

You replace "Integer" with the class that the list is of. An application can increase the capacity of an ArrayList instance before adding a large number of elements using the ensureCapacity operation. This may reduce the amount of incremental reallocation. E represents an Element, which could be any class. ensureCapacity is used to ensure that the list has enough capacity to take in the new elements. 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. 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. Hence, ensureCapacity is called (internally) to ensure that there's enough space. So if you were adding the 11th element, the array size might be, say, doubled, to 20.

Upvotes: -3

Malwaregeek
Malwaregeek

Reputation: 2274

Print the list and override toString method.

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

System.out.print(ob1);

Upvotes: 0

kosa
kosa

Reputation: 66637

If you know the index, you can do 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.

Upvotes: 11

user1201594
user1201594

Reputation:

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. 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.

An example getter method (which you should put in your yellow pages class) could look like this:

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. 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

Upvotes: 1

Amir Afghani
Amir Afghani

Reputation: 38511

You could have the data stored in thing (horribly named variable) simply returned from the calc method. 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.

public class YellowPage { 

    private int result;

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

    public int getResult() { 
        return result;
    }

}

Upvotes: 0

Chandra Sekhar
Chandra Sekhar

Reputation: 19492

Use an Iterator object to do this.

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

Upvotes: 0

Related Questions