pvitkovsky
5/10/2018 - 10:46 AM

myArrayList

ArrayList implementation.

import java.util.Random;

public class arrayList_test {

	static Random gen = new Random();

  static Random fix = new Random(8);

	public static void removePersonsOver60(){
		myArrayList<Person> mal = new myArrayList<>(15);
		for(int i = 0; i< 15; i++) {
			mal.add(Person.getRandomPerson());
		}
		System.out.println("==============");
		System.out.println(mal.toString());
		for(int i = 0; i< mal.size(); i++) {
			Person p = mal.get(i);
			if(p.getAge() < 60){
				mal.set(p, i);
			} else {
				mal.remove(i);
				i--;
			}
		}
		System.out.println("==============");
		System.out.println(mal.toString());
	}
	
	public static void removePersonsOver60pro(){
		// use findFirst with condition
		// while findFirst == true remove (i)
	}
	
	public static void add1YearToEachPerson(){
		myArrayList<Person> mal = new myArrayList<>(15);
		for(int i = 0; i< 15; i++) {
			mal.add(Person.getRandomPerson());
		}
		System.out.println("==============");
		System.out.println(mal.toString());
		for(int i = 0; i< mal.size(); i++) {
			Person p = mal.get(i);
			p.setAge(p.getAge() + 1);
			mal.set(p, i);
		}
		System.out.println("==============");
		System.out.println(mal.toString());
	}
	
	public static void appendPerson(){
		myArrayList<Person> mal = new myArrayList<>(15);
		for(int i = 0; i< 15; i++) {
			mal.add(Person.getRandomPerson());
		}
		System.out.println("==============");
		System.out.println(mal.toString());
		Person p = mal.get(6);
		p.setAge(60);
		mal.set(p, 6);
		System.out.println("==============");
		System.out.println(mal.toString());
	}
	
	public static void personTest(){
		myArrayList<Person> mal = new myArrayList<>(15);
		for(int i = 0; i< 15; i++) {
			mal.add(Person.getRandomPerson());
		}
		System.out.println("==============");		
		System.out.println(mal.get(3));
		System.out.println("==============");
		mal.display();
		System.out.println("==============");
		System.out.println(mal.toString());
		mal.clear();
		System.out.println("==============");
		System.out.println(mal.isEmpty());
		System.out.println("==============");
		System.out.println(mal.toString());
		for(int i = 0; i< 10; i++) {
			mal.add(Person.getRandomPerson());
		}
		System.out.println("==============");
		System.out.println(mal.toString());
		mal.remove(3);
		System.out.println("==============");
		System.out.println(mal.toString());
		System.out.println("==============");
		System.out.println(mal.isEmpty());
	}
  
  public static void increaseTest(){
    myArrayList<Integer> mal = new myArrayList<>(15);
			//two digit: upper limit open interval
		System.out.println(mal.lenEqCap());
		System.out.println(mal.capacity());
		mal.enlargeArray();
		System.out.println(mal.lenEqCap());
		System.out.println(mal.capacity());
    
  }

	public static void intTest(){
		myArrayList<Integer> mal = new myArrayList<>(15);
			//two digit: upper limit open interval
		System.out.println(mal.capacity());
		mal.enlargeArray();
		System.out.println(mal.capacity());
		for(int i = 0; i< 15; i++) {
			mal.add(gen.nextInt(100));
		}
		System.out.println("==============");		
		System.out.println(mal.get(3));
		System.out.println("==============");
		System.out.println(mal.set(333, 11));
		System.out.println("==============");
		mal.display();
		System.out.println("==============");
		System.out.println(mal.indexOf(333));
		System.out.println("==============");
		System.out.println(mal.contains(333));
		System.out.println("==============");
		System.out.println(mal.toString());
		mal.clear();
		System.out.println("==============");
		System.out.println(mal.isEmpty());
		System.out.println("==============");
		System.out.println(mal.toString());
		for(int i = 0; i< 10; i++) {
			mal.add(gen.nextInt(100));
		}
		System.out.println("==============");
		System.out.println(mal.toString());
		mal.remove(3);
		System.out.println("==============");
		System.out.println(mal.toString());
		System.out.println("==============");
		System.out.println(mal.isEmpty());
	}
	
	public static void hwTest(){
		myArrayList<Integer> mal = new myArrayList<>(50);
		for(int i = 0; i< 50; i++) {
			mal.add(fix.nextInt(25));
		}
		System.out.println(mal.toString());
		mal.removeAll(22);
		System.out.println(mal.toString());
		mal.add(23, 0);
		System.out.println(mal.size());
		mal.add(23, 46);
		System.out.println(mal.size());
		mal.add(23, 47);
		System.out.println(mal.size());
		System.out.println(mal.toString());
		mal.set(23, 48);
		System.out.println(mal.toString());
	}
	
	public static void main(String[] args) {
  //  increaseTest();
  //  intTest();
		  hwTest();
	//	personTest();
	//	appendPerson();
	//	add1YearToEachPerson();
	//	removePersonsOver60();
	}

}
import java.util.stream.IntStream;

public class myArrayList<T> {
	private static final int DEFAULT_CAPACITY = 10;
	private Object[] arr;
	private int capacity; //max size
	private int size; //current size
	public boolean indexAdequate(int index){
		return (index >= 0 && index < size);
	}
	
	public myArrayList(){
		arr = new Object[DEFAULT_CAPACITY];
		capacity = DEFAULT_CAPACITY;
		size = 0;
	}
	
	public myArrayList(int cap){
		if (cap < DEFAULT_CAPACITY) cap = DEFAULT_CAPACITY;
		arr = new Object[cap];
		capacity = cap;
		size = 0;
	}
	
	public void enlargeArray() {
	  //ask professor why this works. isnt[] higher precedence than *=?
		Object[] newArray = new Object[capacity*=2];
		System.arraycopy(arr, 0, newArray, 0, arr.length);
		arr = newArray;
	}
	
	public void add(T data){
		arr[size++] = data;// what if array is full already?
	}
	//HW 2
	public void add(T data, int index){
		if(!indexAdequate(index)) throw new IndexOutOfBoundsException();
		size++;
		for(int i = size - 1; i > index; i-- ){
		  arr[i] = arr[i-1]; 
		}
		arr[index] = data;
	}
	//imperative style. imagine physically the boxes and what we put where.
	
	public int indexOf(T data){
		return IntStream.range(0, size).filter(i -> arr[i].equals(data)).findFirst().orElse(-1);
	}
	
	public boolean contains(T data){ //2 minutes
		return indexOf(data) >= 0;
	}
	
	@SuppressWarnings("unchecked")
	public T get(int index ){
		if(!indexAdequate(index)) throw new IndexOutOfBoundsException(); 
		return (T) arr[index];
	}
	
	@SuppressWarnings("unchecked")
	public T set(T data, int index){
		if(!indexAdequate(index)) throw new IndexOutOfBoundsException(); 
		T res = (T) arr[index];
		arr[index] = data;
		return res; 
	}

	public T remove(int index){
		if(!indexAdequate(index)) throw new IndexOutOfBoundsException(); 
		@SuppressWarnings("unchecked")
		T res = (T) arr[index];
		for(int i = index  + 1 ; i < size; i++ ){
			arr[i-1] = arr[i];
		}
		size--;
		return res;
	}
	//HW 1
	public void removeAll(T data){ // all e -> e == data.
		while (contains(data)){
		  remove(indexOf(data));
		}
	}
	
	public void clear(){
		arr = new Object[DEFAULT_CAPACITY];
		capacity = DEFAULT_CAPACITY;
		size = 0;
	}
	
	public boolean isEmpty(){return (size == 0);}
	
	public int size(){return size;}
	
  public int capacity(){return capacity;}
	
	public int length(){return arr.length;}
	
	public boolean lenEqCap(){
	  return this.length() == this.capacity() ;
	}
	
	public String toString(){
		StringBuilder sb = new StringBuilder();
		if(!isEmpty()){		
			for(int i = 0; i < size; i++){
				sb.append(arr[i].toString());
				sb.append(", ");
			}
		} else {
			sb.append("myArrayList empty");
		}
		return sb.toString();
	}
	
	public void display(){
		IntStream.range(0, size).forEach(i -> System.out.println(arr[i]) );
	}
	
	public static void main(String[] args) {
	}
}