4

I'm new to Java and the code I'm working with is largely not my own, so please bear with me...

I have an arraylist containing several dozen instances of the same class, from which I'd like to extract the values of some variables in turn.

I've created an iterator and can iterate happily over the arraylist, the problem comes when I try to extract the class instances. Using .get(index) returns an Object and I'm ignorant of how to either convert an Object to the class type in order to access the variables or extract the instance in its own type.

Can anyone advise how to resolve this?

Thanks in advance.

Edit: sorry, I should have included the code in the first instance.

ArrayList TopicResults = results.getTopicResults(TopicNum);
ListIterator TopicResultsiter = TopicResults.listIterator();
while(TopicResultsiter.hasNext()){
                    int idx = TopicResultsiter.nextIndex();
                    ResultsList.Result result = TopicResults.get(idx);
                    String DocID = result.docID;
                    System.out.println(DocID);
                }
4
  • 4
    show your current code please ..... Commented Feb 18, 2013 at 15:45
  • 2
    Your iterator should not be returning Object types if you are properly specifying a generic parameter. Commented Feb 18, 2013 at 15:46
  • Read up on Casting and Generics Commented Feb 18, 2013 at 15:52
  • 1
    And Java naming conventions. Commented Feb 18, 2013 at 15:54

3 Answers 3

8

Just use casting :

MyClass classInstance = (MyClass)myArray.get(0); 

Assuming that all instances in the list are of the type MyClass otherwise you might get ClassCastException

Using generics iteration should be easier and much safer :

List<MyClass> myList = new ArrayList<MyClass>(); 
for (MyClass classInstance : myList) {
     classInstance.doSomething();
}

The type enclosed by the <> specifies the type of objects that are allowed to be stored in the list so this way already in compilation time you can safely use the list members without the need to check for their type. (Available from J2SE 1.5+)

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

4 Comments

(From 1.7: List<MyClass> myList = new ArrayList<>();.)
finally they got rid of this redudancy
Well some of it. If I want a new List, I think we know what implementation it's going to be. / From 1.5 you could use the shorter List<MyClass> myList = new ArrayList();, though you will get a warning (which most people seem to completely ignore).
What about case when myArray init and have myArray.size()==0?
1

MyType object = (MyType) list.get(index);

Comments

1

You can assign the object returned by the "get" Method of an ArrayList directly to an attribut of the same type as the ArrayList.

For example, if you have an

ArrayList<MyType> myList;

just use

MyType myObjectf = myList.get(0);

...should work without explicit typecasting

1 Comment

Right. The raw use of ArrayList remains for backward compatibility.

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.