Adam Jakiela
Adam Jakiela

Reputation: 2248

Objects in an ArrayList

Im working on a java project for a class which has a user enter a zip code, and the information (city, state..ect) that goes along with the zip code is returned to the user.

All the zip codes are stored in an arraylist which stores each zip code and its information in an object.

This is the code used to add zip code object to the array list:

ZipCode zip = new Zipcode(zipcode, city, state, lat, lon); 

zipCode.add(zip); 

My my question is how do i get certain information out of the arraylist from each object?

For example if i wanted to use the getZip() method to return the zipcode of the zipCode object in spot 39 how would that be done?

I hope I havent been to confusing.

Upvotes: 1

Views: 428

Answers (4)

paulsm4
paulsm4

Reputation: 121619

Remember - you've got (at least) two different classes:

  • your "zip codes"

    ... and ...

  • your list of zip codes

As such, your actual code should probably look more like this:

  List<ZipCode> zipCodeList = new List<ZipCode>();
  ZipCode aZipCode = new Zipcode(zipcode, city, state, lat, lon);
  zipCodeList.add (aZipCode);

Your "getZip()" function (wherever you decide to put it), might look something like this:

  ZipCode myZip = someOtherObject.getZip (i);

The key thing is to decide WHAT your classes are, and WHAT each class is "responsible" for doing. The rest (including your actual implementation) follows.

It might help to write a simple class diagram down on a piece of paper: boxes with the class name, main methods, and main fields. Kind of like UML - only as simple as possible.

'Hope that helps!

PS: I assumed that your "getZip()" method returned an entire "ZipCode" object (latitude, longtitude ... along with "zipcode"). Maybe you instead wanted an accessor method for just the "zipcode" (e.g. "90630"). In that case, I'd recommend something like this:

  List<ZipCode> zipCodeList = new List<ZipCode>();
  zipCodeList.add ("90210", "Beverly Hills", "CA", "000000.00", "000000.00");
  ...
  ZipCode zipCode = zipCodeList[0];
  string zipcode = zipCode.getZipcode ();
  string state = zipCode.getState ();
  ...

Upvotes: 1

jli
jli

Reputation: 6623

Simply access the arraylist with the get() method.

For your example:

zipCode.get(39).getZip();

Sorry about the mistake, I answered this too fast.

Upvotes: 0

Jon Newmuis
Jon Newmuis

Reputation: 26492

To retrieve the object at the nth index of an ArrayList, use:

arrayList.get(n)

In your case, this would be:

ZipCode result = zipCode.get(39);

Then you can invoke the getZip() method of the resulting ZipCode object, by saying:

result.getZip();

Upvotes: 0

aioobe
aioobe

Reputation: 420951

*For example if i wanted to use the getZip() method to return the zipcode of the zipCode object in spot 39 how would that be done? *

How about

zipCode.get(39).getZip()

However, it seems like you would benefit from using a Map<Integer, ZipCode> zipCodes and use

zipCodes.put(zipCodeNumber, zipObject);

Upvotes: 3

Related Questions