Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.t3.datastructures.lists;
- import java.sql.Array;
- import java.util.Arrays;
- /**
- * Created by Tony Nguyen on 24.03.2017.
- */
- public class ArrayList<T> {
- class ArrayListItem<T> {
- public ArrayListItem<T> next;
- // n is the amount of elements in the Array A
- public int n;
- public T[] A;
- public ArrayListItem(T key, int N) {
- this.n = 1;
- this.A = (T[]) new Object[N];
- this.A[0] = key;
- this.next = null;
- }
- public ArrayListItem(int N) {
- this.n = 0;
- this.A = (T[]) new Object[N];
- this.next = null;
- }
- }
- private ArrayListItem<T> head;
- // N is the length of each Array of the ArrayListItems
- private int N;
- public ArrayList(int N) {
- this.N = N;
- head = new ArrayListItem<T>(N);
- }
- public void add(T key) {
- ArrayListItem<T> p = head;
- // Array of the item has no elements
- if (p.n != this.N) {
- p.A[p.n] = key;
- p.n++;
- } else {
- while (p.n == this.N) {
- if (p.next != null) {
- p = p.next;
- } else {
- p.next = new ArrayListItem<T>(key, this.N);
- return;
- }
- }
- p.A[p.n] = key;
- p.n++;
- }
- }
- public void addAt(int index, T key) {
- }
- public boolean remove(T key) {
- ArrayListItem<T> p = head;
- int index = 0;
- while (p.A[index] != key) {
- if (index + 1 == N) {
- if (p.next == null) {
- return false;
- } else {
- p = p.next;
- index = 0;
- }
- } else {
- index++;
- }
- }
- for (int i = index; i < N-1; i++) {
- p.A[i] = p.A[i + 1];
- }
- p.n--;
- return true;
- }
- public T get(int i) {
- ArrayListItem<T> p = head;
- int iterator = 0;
- int index = 0;
- int tmp = i;
- while (tmp > p.n - 1) {
- if (p.next != null) {
- iterator += p.n;
- p = p.next;
- tmp = tmp - i;
- index = -1;
- } else {
- throw new IndexOutOfBoundsException();
- }
- }
- if (p.A[index] == null) {
- throw new IndexOutOfBoundsException("the desired index is too high");
- }
- return p.A[index];
- }
- // if(i > p.n-1){
- // iterator += p.n;
- // p = p.next;
- // index = -1;
- // }
- // while (iterator != i) {
- //
- // if (iterator != 0 && iterator % (N - 1) == 0) {
- // if (p.next == null) {
- // throw new IndexOutOfBoundsException("the desired index is too high");
- // } else {
- // p = p.next;
- // index = -1;
- // }
- // }
- // iterator++;
- // index++;
- // }
- // if(p.A[index] == null){
- // throw new IndexOutOfBoundsException("the desired index is too high");
- // }
- // return p.A[index];
- // }
- public boolean contains(T key) {
- ArrayListItem<T> p = head;
- int index = 0;
- while (p.A[index] != key) {
- if (index + 1 == N) {
- if (p.next != null) {
- index = -1;
- p = p.next;
- } else {
- return false;
- }
- }
- index++;
- }
- return true;
- }
- public static void main(String[] args) {
- ArrayList<Integer> list = new ArrayList<>(3);
- list.add(1);
- list.add(2);
- list.add(3);
- list.add(4);
- list.add(5);
- list.add(6);
- System.out.println(Arrays.toString(list.head.A));
- System.out.println(Arrays.toString(list.head.next.A));
- System.out.println(list.remove(1));
- System.out.println(list.remove(3));
- System.out.println(list.get(0));
- System.out.println(list.get(1));
- System.out.println(list.get(2));
- // System.out.println(list.get(3));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement