• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

How to validate a specific type and make list "type-safe"

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
Marshal
Posts: 71103
292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 an IllegalStateException or 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.
 
Marshal
Posts: 26137
77
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
Campbell Ritchie
Marshal
Posts: 71103
292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Sheriff
Posts: 15999
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

brian leung wrote:


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?
 
brian leung
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

brian leung wrote:


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".
 
Junilu Lacar
Sheriff
Posts: 15999
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Per what others have told you already, I would just write it like this:


To create the list, you'd do something like this instead:

I don't know why you'd limit it to String, Integer, and Double though. Seems rather restrictive when it doesn't need to be.
 
catch it before it slithers away! Oh wait, it's a tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic