Skip to content

Commit

Permalink
GP-33 Add into main
Browse files Browse the repository at this point in the history
  • Loading branch information
shryhus committed Jan 14, 2021
1 parent 7135461 commit e45f0db
Show file tree
Hide file tree
Showing 4 changed files with 585 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
package com.bobocode.array_list;

import com.bobocode.linked_list.List;

import java.util.Arrays;
import java.util.NoSuchElementException;
import java.util.Objects;

/**
* {@link ArrayList} is an implementation of {@link List} interface. This resizable data structure
* based on an array and is simplified version of {@link java.util.ArrayList}.
*/
public class ArrayList<T> implements List<T> {

public static final int DEFAULT_CAPACITY = 5;
private Object[] elementData;
private int size;

/**
* This constructor creates an instance of {@link ArrayList} with a specific capacity of an array inside.
*
* @param initCapacity - the initial capacity of the list
* @throws IllegalArgumentException – if the specified initial capacity is negative or 0.
*/
public ArrayList(int initCapacity) {
if (initCapacity > 0) {
elementData = new Object[initCapacity];
} else {
throw new IllegalArgumentException();
}
}

/**
* This constructor creates an instance of {@link ArrayList} with a default capacity of an array inside.
* A default size of inner array is 5;
*/
public ArrayList() {
elementData = new Object[DEFAULT_CAPACITY];
}

/**
* Creates and returns an instance of {@link ArrayList} with provided elements
*
* @param elements to add
* @return new instance
*/
public static <T> List<T> of(T... elements) {
List<T> list = new ArrayList<>(elements.length);
for (T element : elements) {
list.add(element);
}
return list;
}

/**
* Adds an element to the array and returns index of position.
*
* @param element element to add
*/
@Override
public void add(T element) {
increaseDataArrayIfFull();
elementData[size] = element;
size++;
}

private void increaseDataArrayIfFull() {
if (elementData.length <= size) {
elementData = getTrimmedArrayToSize(elementData.length * 2);
}
}

/**
* Adds an element to the specific position in the array where
*
* @param index index of position
* @param element element to add
*/
@Override
public void add(int index, T element) {
increaseDataArrayIfFull();
System.arraycopy(elementData, index, elementData, index + 1, size - index);
elementData[index] = element;
size++;
}

/**
* Retrieves an element by its position index. In case provided index in out of the list bounds it
* throws {@link IndexOutOfBoundsException}
*
* @param index index of element
* @return en element
*/
@Override
@SuppressWarnings("unchecked")
public T get(int index) {
Objects.checkIndex(index, size);
return (T) elementData[index];
}

/**
* Returns the first element of the list. Operation is performed in constant time O(1)
*
* @return the first element of the list
* @throws java.util.NoSuchElementException if list is empty
*/
@Override
@SuppressWarnings("unchecked")
public T getFirst() {
if (isEmpty()) {
throw new NoSuchElementException();
}
return (T) elementData[0];
}

/**
* Returns the last element of the list. Operation is performed in constant time O(1)
*
* @return the last element of the list
* @throws java.util.NoSuchElementException if list is empty
*/
@Override
@SuppressWarnings("unchecked")
public T getLast() {
if (isEmpty()) {
throw new NoSuchElementException();
}
return (T) elementData[size - 1];
}

/**
* Changes the value of array at specific position. In case provided index in out of the list bounds it
* throws {@link IndexOutOfBoundsException}
*
* @param index position of value
* @param element a new value
*/
@Override
public void set(int index, T element) {
Objects.checkIndex(index, size);
elementData[index] = element;
}

/**
* Removes an elements by its position index. In case provided index in out of the list bounds it
* throws {@link IndexOutOfBoundsException}
*
* @param index element index
*/
@Override
public void remove(int index) {
if (index == size - 1) {
elementData = getTrimmedArrayToSize(size - 1);
} else {
System.arraycopy(elementData, index + 1, elementData, index, size - index - 1);
}
size--;
}

/**
* Checks for existing of a specific element in the list.
*
* @param element is element
* @return If element exists method returns true, otherwise it returns false
*/
@Override
public boolean contains(T element) {
if (isEmpty()) {
return false;
} else {
for (Object elem : elementData) {
if (elem.equals(element)) {
return true;
}
}
}
return false;
}

/**
* Checks if a list is empty
*
* @return {@code true} if list is empty, {@code false} otherwise
*/
@Override
public boolean isEmpty() {
return size == 0;
}

@SuppressWarnings("unchecked")
private T[] getTrimmedArrayToSize(int size) {
return (T[]) Arrays.copyOf(elementData, size);
}

/**
* @return amount of saved elements
*/
@Override
public int size() {
return size;
}

/**
* Removes all list elements
*/
@Override
public void clear() {
elementData = new Object[DEFAULT_CAPACITY];
size = 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# <img src="https://raw.githubusercontent.com/bobocode-projects/resources/master/image/logo_transparent_background.png" height=50/>Array Wrapper exercise :muscle:
Improve your basic knowledge about data structures and Java Core.
This wrapper is a simple version of the `ArrayList` data structure.
### Task
`ListOfLongs` interface provides an API with a couple of methods for a wrapper of a default array in Java. Your job is to implement the *todo* section of `ArrayListOfLongs` class.
To verify your implementation, run `ListOfLongsTest.java`.

### Pre-conditions :heavy_exclamation_mark:
You're supposed to be familiar with arrays, cycles, conditions, exceptions and variables.

### How to start :question:
* Just clone the repository and start implementing the **todo** section, verify your changes by running tests
* If you don't have enough knowledge about this domain, check out the [links below](#related-materials-information_source)
* Don't worry if you got stuck, checkout the **exercise/completed** branch and see the final implementation

### Related materials :information_source:
###to do
Loading

0 comments on commit e45f0db

Please sign in to comment.