java - 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 string:
string mestring = "ABC";
and then try to compare it with every single string contained in the array list
ArrayList & lt; String & gt; Array = new arreelist & lt; String & gt; (); Array.add ("The first letters of the alphabet are ABC"); Array.add ("abc"); Array.add ("alphabet"); Array.add ("nothing");
I have got a class which is the only thing which is important "searching" method.
Public Zero Search (ArrayList> Object> Objects)
If you have noticed, then this method is an array of object parameters As receives. The problem now is that Your problem appears with Java Generic. Generics can be used here by To instantiate an You mention that The ArrayList array mentioned above is a property of each of these objects which means I Is that the If you want to access the instance data of an object, then you need to tell Java that the data that you have Accessing the Java Compiler is very careful about what types of objects are, and which operations are defined on each object. If you try to use the instance data from one instance of ArrayList & lt; String & gt; The array which I have mentioned above is the property of each item, so I can not read it directly through the array list (I can do but I do not know how). Is there a way that the attributes of an object should be obtained in the form of a parameter inside the array or through it in a different square method?
ArrayList
In conjunction with Java, let Java know how you want to save that code ArrayList
if you want to create ArrayList
Want to store types of objects, tell examples of a class MyClass
that you have defined somewhere else, if you have defined Can
ArrayList & LT; MyClass & gt; MyObjs = New Arrestist & lt; MyClass & gt; ();
ArrayList
, you can only hold the object of MyClass
.
object
s contains the orleist and lieutenant; Object & gt; In the designated
). In that class, objects
you have examples of a special class in your Search
method (preceding the example, Code> MyClass ArrayList & lt; String & gt;
is that the issue you want to intersect as the example data is that you have not told Java that ArrayList & lt; Object & gt; Examples of
MyClass
are, so when you get something from objects, Java does not know it is MyClass
. (To be specific, the compiler does not know it.) MyClass
, but the compiler thinks that the type of example is object
, then the compiler will be very upset with you ( Because you do not know that in the question in the example is actually the data you use - it can be an object of another type!). What you need to do is tell the compiler that you can only store examples in MyClass
in your ArrayList
so that the compiler will know that the ArrayList < / Code> wants to access that instance data.
Comments
Post a Comment