Your issue appears to be with Java generics. Generics are being used here in conjunction with ArrayList
to tell Java what type of Object you'd like to store in that ArrayList
. If you want to create an ArrayList
that stores a particular variety of object, say instances of a class MyClass
that you've defined elsewhere, then you can do
ArrayList<MyClass> myObjs = new ArrayList<MyClass>();
to instantiate an ArrayList
that can hold only objects of type MyClass
.
You mention that
the ArrayList array that i mentioned above is a property of each these objects
which I take to mean that the Object
s contained in the ArrayList<Object>
called objects
that you pass into your Search
method are instances of a particular class (continuing the example from earlier, MyClass
). That class contains the ArrayList<String>
that you'd like to iterate over as instance data. The issue is that you haven't told Java that the ArrayList<Object>
holds instances of MyClass
, so when you get something out of objects, Java doesn't know that it's of type MyClass
. (To be specific, the compiler doesn't know that.)
If you want to access the instance data of an object, then you're going to have to tell Java what sort of thing you're accessing that data from. The Java compiler is very careful about what types objects are, and what operations are defined on each object. If you attempt to access instance data from an instance of MyClass
, but the compiler thinks that instance is of type Object
, then the compiler will be very upset with you (since it doesn't know whether the instance in question actually has the data you're trying to access--it might be some other kind of object!). What you need to do is tell the compiler that you can only store instances of MyClass
in your ArrayList
, so that the compiler will know that objects in that ArrayList
have the instance data you want to access.