Array implements linked list

    // test class  
    public class ListTest {  
        public static void main(String[] args) {  
            // create an array object  
            Mylist<Integer> list = new Mylist<Integer>();  
            for (int i = 0; i < 10; i++) {  
                int s = 10*i;  
                list.add(s);  
                  
            }  
            //add another element  
            list.add(100);  
            list.modify(10, 100);  
              
            // get the element  
            for (int i = 0; i < list.size(); i++) {  
                int s = list.get(i);  
                System.out.println(s);  
            }  
        }  
      
    }  
      
      
    /**
     * Create an array queue
     *  
     * @author Shu Yaoyao
     *  
     */  
    public class Mylist<E> {  
      
        // create an empty initial array  
        Object src[] = new Object[0];  
        Object  stroke[]=new Object[500];  
      
        /**
         * add elements to the array
         *  
         * @return
         */  
        public void add(E s) {  
            Object dest[] = new Object[src.length + 1];  
            // assign the value of the initial array to the newly created array  
            for (int i = 0; i < src.length; i++) {  
      
                 dest[i] =  src[i];  
                  
            }  
            // assign the value of the heart array to the last position of the initial array  
                dest[src.length]=s;  
                src = dest;  
                  
              
        }  
      
        /**
         * Get the element in the array according to the subscript
         * return the number obtained
         */  
        public E get(int index) {  
            E s=(E)src[index];  
              
            return s;  
      
        }  
      
        /**
         * Modify array elements based on subscripts and element names
         */  
        public void modify(int index,E s) {  
            src[index]=s;  
      
        }  
      
        /**
         * Insert into array based on element name and subscript
         */  
        public void insert(int index, E s) {  
            src[index]=s;  
        }  
      
        /**
         * delete array by element name and subscript
         *  
         */  
        public void delete(int index, E s1) {  
            E s=(E)src[index];  
              
      
        }  
      
        /**
         * Statistics array size
         * return the size obtained
         */  
        public int size() {  
            return src.length;  
      
        }  
      
          
          
          
    }  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326397340&siteId=291194637