5
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?

1
  • Not related, but: Java naming conventions, dude. Commented Aug 16, 2013 at 8:39

6 Answers 6

11

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.

Sign up to request clarification or add additional context in comments.

3 Comments

If i did like this, it shows the value as "yellowPage@addbf1"; but how can i get the value stored inside that object? Please help me Amir
What is there in YellowPage class, you need to use that getter there. Can you post yellow page and tell what value you want to access?
what you are getting is hashcode of objects stored. You will have to override the toString method. See my answer
1
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

Comments

0

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

1 Comment

After getting the yellowPage object as "retrievedthing", get whatever you want from that.
0

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

}

Comments

0

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.