Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-30-2007, 07:00 PM
Member
 
Join Date: Jul 2007
Posts: 40
elizabeth is on a distinguished road
how to return an object from an arraylist
How can I return an object from an arraylist given a specific index?

something like that?
Code:
private ArrayList container = new ArrayList; public Test() { for( int i = 0; i < container.size(); i++) object.exampleMethod(container.get(i)); }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-30-2007, 07:57 PM
Senior Member
 
Join Date: Jul 2007
Posts: 134
brianhks will become famous soon enough
container.get(i) will return the object at the i position in the array list.

It almost looks like you are wanting to call a method on that object that is returned. To do that you will want to do something like this:
Code:
//Say the array is containing objects of type MyObject ((MyObject)container.get(i)).myMethod();
A better way is to use the new Java types with your containers. Like so
Code:
private ArrayList<MyObject> container = new ArrayList<MyObject>(); public Test() { for( int i = 0; i < container.size(); i++) { //Now you can do things like this container.get(i).myMethod(); //Because the container knows it is holding an object of type MyObject } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Project Trouble: Searching one ArrayList with another ArrayList BC2210 New To Java 2 04-21-2008 12:43 PM
Object ArrayList - increment solution needed badly!! rugbyGeek New To Java 4 03-08-2008 01:47 AM
object instantiation and arrayList lockmac New To Java 5 08-09-2007 07:25 PM
Operator < cannot be applied to java.lang.Object, Object Albert Advanced Java 1 07-13-2007 04:19 PM


All times are GMT +3. The time now is 08:00 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org