Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Construct your own ArrayList Using Generics

    • 0
    • 2
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 4.47k
    Comment on it

    Here the Below Example will show you how you can construct your own Collection(ArrayList) in Java. The below example will show you that our Class Generics will behave like the ArrayList. I have use the Generic Programming to Implement the concepts and this blog will give you the core idea how you can create your own collection framework . This is not complete implementation as like ArrayList but it gives you the idea when you want to implement the functionality like ArrayList Class or makes your own Collections classes.

    package com.util;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import java.util.Vector;
    
    public class Generics<T> {
        private int i=0;
        private Object obj[];
        private T t;
    
        public Generics()
        {
    
            obj=new Object[5];
    
        }
    
        public void add(T t)
        {
            this.t=t;
    
            if(i==obj.length-1)
            {
                //Increase the Capacity of Array by 50% of it's size
                obj=Arrays.copyOf(obj, obj.length+(obj.length/2));
    
                System.out.println(obj.length);
            }
    
            obj[i]=this.t;
            i++;
    
        }
    
        public Object get(int i)
        {
    
            if(obj[i]!=null)
            {
                return obj[i];
            }
            else
            {
                throw new ArrayIndexOutOfBoundsException("index:"+i);
            }
    
        }
    
        public String toString()
        {
            String temp="";
            for(Object ob:obj)
            {
                if(ob!=null)
                {
    
                    temp=temp+ob+',';
                }
    
    
            }
    
            String temp1=temp.substring(0, temp.length()-1);
    
    
    
    
            return "["+temp1+"]";
        }
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            Generics<Integer> genList=new Generics<Integer>();
            Generics<String> gen1List=new Generics<String>();
            genList.add(12);
            genList.add(23);
            genList.add(25);
    
    
            gen1List.add("manish");
            gen1List.add("akki");
    
    
            System.out.println(genList+"  "+gen1List);
    
    
    
    
    
    
    
    
        }
    
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: