Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public interface IMyList<T> {
- void add(T element);
- public void add(int index, T element);
- public void clear();
- public T pop();
- public T get(int index);
- public int size();
- public T remove(int index);
- }
- import java.util.Arrays;
- public class MyCustomList<T> implements IMyList<T>
- {
- T[] ListOb;
- final int DefaultLenght = 10;
- int PositionIndex;
- int ListMaxIndex;
- public MyCustomList()
- {
- ListOb = (T[])new Object[DefaultLenght];
- ListMaxIndex = DefaultLenght;
- PositionIndex = 0;
- }
- @Override
- public void add(T Element)
- {
- if (PositionIndex >= ListMaxIndex)
- {
- resizeList(ListOb.length + 1);
- }
- ListOb[PositionIndex] = Element;
- PositionIndex++;
- }
- @Override
- public void add(int Index, T Element)
- {
- if(Index >= ListMaxIndex)
- {
- resizeList(Index + 1);
- }
- ListOb[Index] = Element;
- if(Index >= PositionIndex)
- {
- PositionIndex = Index + 1;
- }
- }
- @Override
- public void clear()
- {
- ListOb = (T[])new Object[DefaultLenght];
- ListMaxIndex = DefaultLenght;
- PositionIndex = 0;
- }
- @Override
- public T pop()
- {
- if(PositionIndex == 0)
- {
- return null;
- }
- T TempOb = ListOb[PositionIndex - 1];
- ListOb[PositionIndex - 1] = null;
- PositionIndex--;
- return TempOb;
- }
- @Override
- public T get(int Index)
- {
- if(Index <= ListMaxIndex)
- {
- return ListOb[Index];
- }
- else return null;
- }
- @Override
- public int size()
- {
- return PositionIndex;
- }
- @Override
- public T remove(int Index)
- {
- if(Index > ListMaxIndex)
- {
- return null;
- }
- T TempOb = ListOb[Index];
- ListOb[Index] = null;
- if(Index == PositionIndex - 1)
- {
- PositionIndex--;
- }
- return TempOb;
- }
- public void resizeList(int NewSize)
- {
- ListOb = Arrays.copyOf(ListOb, NewSize);
- ListMaxIndex = NewSize - 1;
- }
- }
- public class GenericList{
- public static void main(String[] args){
- IMyList<Integer>list;
- list = new MyCustomList<>();
- for(int TempNumber = 0; TempNumber < 20;i++)
- {
- list.add(TempNumber);
- }
- list.add(25,25);
- System.out.println(list.pop());
- System.out.println(list.pop(20));
- System.out.println(list.get(20));
- list.remove(10);
- list.clear();
- list.add(10);
- System.out.println(list.pop());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement