Results 1 to 2 of 2
Like Tree2Likes
  • 2 Post By Eranga

Thread: Casting problem (Type object)

  1. #1
    Join Date
    Aug 2011
    Posts
    16
    Rep Power
    0

    Default Casting problem (Type object)

    Managed to solve the problem by myself! As T is an object and int is a primitive, I need to convert int into Integer before casting.

    public BitMapSet(int[] init)
    {

    for(int i = 0;i<init.length;i++)
    {
    Integer c = new Integer(init[i]);
    add((T)c);
    }
    }

    Java Code:
    import java.util.ArrayList;
    public class BitMapSet<T> implements Set<T>{
    
        private ArrayList<T> content = new ArrayList<T>();
        private int counter = 0;
        private T val;
    
        public BitMapSet()
        {
    
        }
    
    
        public BitMapSet(int[] init)
        {
             T x = (T)init[i]; // Inconvertible types 
             for(int i = 0;i<init.length;i++)
             {
                 content.add(x);
             }
        }
    
    
        public void add(T val)
        {
            if(!contains(val) && val instanceof Integer)
            {
                T c = val;
                if(c.intValue()<=99 && c.intValue()>=0)
                {
                    content.add(val);
                    counter++;
                }
            }
        }
    Last edited by FiasseKrystella; 03-04-2012 at 09:02 AM. Reason: Solved the problem on my own

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default Re: Casting problem (Type object)

    Next time if you have solved the problem yourself please don't update the initial post. Just post a reply to your own. So its clear, if someone looking into the thread. May be someone else could looking for a solution for the same issue.
    DarrylBurke and quad64bit like this.

Similar Threads

  1. Casting Enum Type arrays to object type arrays
    By nmvictor in forum Advanced Java
    Replies: 4
    Last Post: 02-17-2012, 01:49 PM
  2. type casting
    By saidinu in forum New To Java
    Replies: 1
    Last Post: 10-21-2011, 02:09 PM
  3. Replies: 10
    Last Post: 06-26-2011, 02:07 AM
  4. Casting Object to another type
    By green_river48 in forum New To Java
    Replies: 12
    Last Post: 04-03-2010, 11:52 AM
  5. type casting
    By alvations in forum New To Java
    Replies: 1
    Last Post: 10-13-2008, 08:07 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
  •