1 Reply Latest reply: Dec 23, 2011 2:10 PM by EJP RSS

    unchecked method invocation: <T>sort

    tproc
      Hi All,

      Just registered out here to post this. I have checked out here for years, but never needed to post myself until today (meaning I usually found my answer). I mainly live in a C/C++ world. Java is a hobby, and I think this maybe a situation where not doing this daily has caught up to me. I keep getting this warning when I check my new class for depreciation. I am smulating some data. And found that I overflow the Heap after a few hundred thousand. All I really need is the top 20-50 and the bottom 20-50. Well, that means, I need to sort. I used an array list, because sets require unique data. Now, I could do my own sort function, but was hoping someone can point out my oversight.

      The error:
      MemSafeSortedArrayList.java:82: warning: [unchecked] unchecked method invocation: <T>sort(java.util.List<T>) in java.util.Collections is applied to (java.util.List) Collections.sort(sortList);
      import java.util.ArrayList;
      import java.util.Collection;
      import java.util.Collections;
      import java.util.List;
      import java.lang.Exception;
      
      public class MemSafeSortedArrayList < E extends Comparable< ? super E> > extends ArrayList<E>
      {
           protected int m_sMaxSize;
           protected int m_sResizeTo;
           protected boolean m_bSaveBottom;
           
           public final static long serialVersionUID = 1;
           
           public MemSafeSortedArrayList(int lMax, int lResize, boolean bSaveBottom) throws Exception
                {
                m_sMaxSize          = lMax;
                m_sResizeTo          = lResize;
                m_bSaveBottom     = bSaveBottom;
                
                if (lMax <= lResize)
                     throw new Exception("Max Size exceeds Resize to Size.  Max Size should generally be much larger than Resize to value");
                }
                
           @Override
           public boolean add(E o)
                {
                boolean bRet = super.add(o);
                if ( size() >= m_sMaxSize)
                     {
                     cleanupList();
                     }
                     
                return bRet;
                }
                
           @Override
           public boolean addAll(Collection<? extends E> c)
                {
                boolean bRet = super.addAll(c);
                if ( size() >= m_sMaxSize)
                     {
                     cleanupList();
                     }
                     
                return bRet;
                }
           
           @Override
           public Object[] toArray()
                {
                sort(this);
                
                return super.toArray();
                }
                
           @Override
           public <E> E[] toArray(E[] a)
                {
                sort(this);
                
                return super.toArray(a);
                }
                
           protected void cleanupList()
                {
                sort(this);
                
                if (m_bSaveBottom)
                     {
                     int sKeep = m_sResizeTo/2;
                     removeRange(sKeep,size()-sKeep);
                     }
                else
                     {
                     removeRange(m_sResizeTo,size());
                     }
                }
                
           public static void sort(MemSafeSortedArrayList sortList)
                {
                Collections.sort(sortList);
                }
      }
      Thanks,