I'm trying to validate a specific type and make my list"type-safe". I'm trying to ensure that only one type of reference can be added. For example if I want this list to ONLY be able to take String objects, it can ONLY take string objects.
The array is already set to an Object type, and everything inherits from Object, i've tried to substitute/subtype. However i'm getting errors when trying to call my add/display/remove functions. Not sure if i'm even validating my types correctly? Unfortunately, I can't use generics for this as my professor has not yet covered that topic in class and doesn't expect us to know how to use it.
brian leung wrote:. . . make my list"type-safe". . . . array is already set to an Object type, . . .
That is confusing. Lot of people on this forum write list when they mean array and vice versa; I think you know the difference, but calling the field theList doesn't help. Try calling it values like the similarly‑named class in the standard API.
Generics is really the only way to ensure type safety. It has the advantage that errors are caught at compile time. Anything else will cause exceptions at runtime, or even getting mixed types out of the list. Multiple instanceof test look horrible, and make me think there is a problem with the design of inheritance
The only thing I can think of is to use a Class<T> object as a field of the List. Decide whether to assign that in the constructor (final field) or with the first add() call, in which case any attempt to change it should cause anIllegalStateExceptionor similar to be thrown. Remember the Class object has methods testing whether an object is or isn't an instance. Remember that instances of subtypes will cause that test to pass. If the test fails, you will have to think what sort of exception to throw.
That isn't real type safety because you will suffer exceptions which generics would have avoided.
Like Campbell suggests, if you want to deal with "types" in your system then Objects aren't the way to do that. A Class object would be better. So:
Then when you're trying to add an object to the list, you can examine the type of the object (via its getClass() method) and see if it matches the type stored in your MyArrayList object. For starters you can just use == to test the match but that isn't sufficient in general.
You can get a Class object for e.g. the Integer type like this: "Integer.class".
posted 2 weeks ago
Paul Clapham wrote:. . . You can get a Class object for e.g. the Integer type like this: "Integer.class".
. . . or myIntegerObject.getClass(). Paul did mention that method.
When you say errors, do you mean compile time errors or runtime errors? What's going on with line 7 in the above code? What are you trying to do there?
I was primarily getting compile time errors for the .add() and .remove() methods.
In terms of line 7. I have updated my code to this
However, i'm still able to add different data types into my arraylist "newList" in the main method. Basically i'm trying to make it so the overloaded constructor parameter "public MyArrayList (Object type) will be used to set the type of reference that will be added to my "arraylist".