Results 1 to 3 of 3
  1. #1
    robbie.26 is offline Member
    Join Date
    Jun 2011
    Posts
    19
    Rep Power
    0

    Default Generic objects and collections

    Included code is what I'm trying to accomplish. I want to be able to hold different types of Property objects
    (Property<String>, Property<Double>, etc) in an ArrayList. I want to be able to set the value of a Property
    straight from the ArrayList like this: list.get(0).setValue( 50.25 ), list.get(1).setValue("new string"), etc.
    How would I do this without getting an "unchecked/unsafe operation" compiler note?

    Java Code:
    import java.util.*;
    
    public class Property<T>
    {
        public Property( T initial_value )
        {
            value = initial_value;
        }
    
        public T getValue()
        {
            return value;
        }
    
        public void setValue( T newValue )
        {
            value = newValue;
        }
    
        private T value;
    
        public static void main( String[] args )
    	{
    		ArrayList<Property> list = new ArrayList<Property>();
    
    		Property<Double> dblProp = new Property<Double>( 0.26 );
    		list.add( dblProp );
    
    		list.get( 0 ).setValue( 0.999999 );
    		System.out.println( list.get( 0 ).getValue() );
    
    		Property<String> strProp = new Property<String>( "some string" );
    		list.add( strProp );
    
    		list.get( 1 ).setValue( "a different string" );
    		System.out.println( list.get( 1 ).getValue() );
    	}
    }
    Compiles and works as expected, however the compiler says:
    "Note: C:\Users\Robbie\Desktop\Property.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details."

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: Generic objects and collections

    That is only a warning message, but you should not ignore this.
    Your code makes not really sense for me. How should the compiler know that in the list at position 1, a property is included with string? It can`t check it! So you could also write so well list.get(1).setValue(0.6666); - is that what you want?
    You can suppress the warnings with
    Java Code:
    	@SuppressWarnings({ "rawtypes", "unchecked" })
    	public static void main(String[] args) {

  3. #3
    robbie.26 is offline Member
    Join Date
    Jun 2011
    Posts
    19
    Rep Power
    0

    Default Re: Generic objects and collections

    Quote Originally Posted by eRaaaa View Post
    How should the compiler know that in the list at position 1, a property is included with string? It can`t check it! So you could also write so well list.get(1).setValue(0.6666); - is that what you want?
    Well no, I wouldn't want that to be possible.

    I want to be able to bundle all different types of Property objects (Property<Double>, Property<String>, etc)
    all in one ArrayList and be able to retrieve any one of them to call their designated setValue() or getValue().

    What if I instead used a HashMap with 2 values per entry; the first value being the Property and the second
    being the exact class type of the aforementioned Property, that way I can cast the Property to its appropriate
    type before returning it. Is that over-complicating the issue? I feel like there should be an easier way to do this...
    Last edited by robbie.26; 12-24-2011 at 12:31 PM.

Similar Threads

  1. Collections
    By NatureFreshMilk in forum New To Java
    Replies: 12
    Last Post: 05-23-2011, 09:12 AM
  2. Issues with comparing two generic objects
    By FOUAD-SPIDER in forum Advanced Java
    Replies: 0
    Last Post: 05-06-2011, 05:39 AM
  3. Collections help.
    By YoungJavaBoy in forum New To Java
    Replies: 3
    Last Post: 03-15-2011, 12:14 AM
  4. Generic Collections---Dysfuntional Java
    By beezerbutt in forum New To Java
    Replies: 6
    Last Post: 06-28-2009, 08:37 PM
  5. Collections Help
    By Dr Gonzo in forum New To Java
    Replies: 0
    Last Post: 12-07-2008, 10:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •