user3512777
user3512777

Reputation: 5

Passing a parameter of an object contained in an arraylist to a method

The title may be a little confusing or misleading but I'm trying to compare a single string:

String mystring = "abc";

And then try to compare it with every single string contained in an arraylist

ArrayList<String> array = new ArrayList<String>();
array.add("The first letters of the alphabet are abc");
array.add("abc");
array.add("Alphabet");
array.add("Nothing");

I got a class which the only thing that matters is the method "Search".

public void Search(ArrayList<Object> objects)

If you havent noticed, this method receives as a parameter an arraylist of objects. Now the problem is the ArrayList<String> array that i mentioned above is a property of each these objects, so i cant directly iterate through the arraylist i want (Maybe i can but i just dont know how). Is there a way to either receive a property of an object inside an arraylist as a parameter or iterate through it in a different class´s method?

Upvotes: 0

Views: 1080

Answers (2)

keith
keith

Reputation: 1

I believe you could create a Check if an object belongs to a class in Java inside the Search method. Then all you have to do next is to cast the object back to String object and do the rest of the stuff that you need for that method.

Upvotes: 0

matthugs
matthugs

Reputation: 93

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

Upvotes: 1

Related Questions