SHARE
TWEET

Untitled

a guest Jan 24th, 2020 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package edu.ncsu.csc316.dsa.list;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Iterator;
  5.  
  6. public class ArrayBasedList<E> extends AbstractList<E> {
  7.  
  8.     private final static int DEFAULT_CAPACITY = 10;
  9.     private E[] data;
  10.  
  11.     private int size;
  12.  
  13.     public ArrayBasedList() {
  14.         this(DEFAULT_CAPACITY);
  15.     }
  16.  
  17.     @SuppressWarnings("unchecked")
  18.     public ArrayBasedList(int capacity) {
  19.         data = (E[]) (new Object[capacity]);
  20.         size = 0;
  21.     }
  22.  
  23.     @Override
  24.     public void add(int index, E value) {
  25.         // TODO Auto-generated method stub
  26.         ensureCapacity(size*2);
  27.         checkIndex(index);
  28.         for (int i = size-1; i >= index; i--) {
  29.             data[i+1]=data[i];
  30.         }
  31.         data[index] = value;
  32.        
  33.         }
  34.    
  35.  
  36.     @Override
  37.     public E get(int index) {
  38.         // TODO Auto-generated method stub
  39.         return null;
  40.     }
  41.  
  42.     @Override
  43.     public E remove(int index) {
  44.         // TODO Auto-generated method stub
  45.         return null;
  46.     }
  47.  
  48.     @Override
  49.     public E set(int index, E value) {
  50.         // TODO Auto-generated method stub
  51.         return null;
  52.     }
  53.  
  54.     @Override
  55.     public int size() {
  56.         // TODO Auto-generated method stub
  57.         return size;
  58.     }
  59.  
  60.     @Override
  61.     public Iterator<E> iterator() {
  62.         // TODO Auto-generated method stub
  63.         return null;
  64.     }
  65.    
  66.     private void ensureCapacity(int minCapacity) {
  67. //        int oldCapacity = data.length;
  68. //        if (minCapacity > oldCapacity) {
  69. //            int newCapacity = (oldCapacity * 2) + 1;
  70. //            if (newCapacity < minCapacity) {
  71. //                newCapacity = minCapacity;
  72. //            }
  73. //            data = Arrays.copyOf(data, newCapacity);
  74. //        }
  75.         E[] temp = (E[]) new Object[minCapacity];
  76.         for (int i = 0; i < size; i++) {
  77.             temp[i] = data[i];
  78.         }
  79.         data = Arrays.copyOf(temp, minCapacity);
  80.     }
  81.     //TODO: Complete this class
  82. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top