Class Container<T>

java.lang.Object
com.serov.lab1.Container<T>

public class Container<T> extends Object
Container implementation
Author:
n.a.serov
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Default size
  • Constructor Summary

    Constructors
    Constructor
    Description
    Default constructor without parameters, creates a container of a size defined in Container.DEFAULT_SIZE
    Container(int size)
    Constructor with given size, creates a container of given size
    Container(int elementsCount, int size, T[] elements)
    Constructor with all given fields, creates a container with given elements count, given size and copies the array of type T
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Clears the container and sets elements count to 0, container size does not change
    getElementAt(int index)
    Returns the element at given index
    int
    Returns the count of elements in a container
    int
    Returns the size of a container
    void
    insertElement(T newValue)
    Inserts a new element to the end of the container
    removeElementAt(int index)
    Removes the element at given index, rebuilds the container without this element
    void
    setElementAt(int index, T newValue)
    Sets the count of elements of a container
    Returns the container's elements as a string

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

  • Constructor Details

    • Container

      public Container()
      Default constructor without parameters, creates a container of a size defined in Container.DEFAULT_SIZE
    • Container

      public Container(int size)
      Constructor with given size, creates a container of given size
      Parameters:
      size - the size of a new container
    • Container

      public Container(int elementsCount, int size, T[] elements)
      Constructor with all given fields, creates a container with given elements count, given size and copies the array of type T
      Parameters:
      elementsCount - the count of elements of a new container
      size - the size of a new container
      elements - the array of elements to be copied to a new container
  • Method Details

    • clear

      public void clear()
      Clears the container and sets elements count to 0, container size does not change
    • getSize

      public int getSize()
      Returns the size of a container
      Returns:
      the size of a container
    • getElementsCount

      public int getElementsCount()
      Returns the count of elements in a container
      Returns:
      the count of elements in a container
    • getElementAt

      public T getElementAt(int index) throws IndexOutOfBoundsException
      Returns the element at given index
      Parameters:
      index - index of an element to get value from
      Returns:
      the element at given index
      Throws:
      IndexOutOfBoundsException - If the given index is not in a current container's bounds
    • setElementAt

      public void setElementAt(int index, T newValue) throws NullPointerException
      Sets the count of elements of a container
      Parameters:
      index - the index of an element to be changed
      newValue - the new value of an element to be changed
      Throws:
      NullPointerException - If a new value is null
    • insertElement

      public void insertElement(T newValue) throws NullPointerException
      Inserts a new element to the end of the container
      Parameters:
      newValue - the value of an element to be added to the end of the container
      Throws:
      NullPointerException - If a new value in null
    • removeElementAt

      public T removeElementAt(int index) throws IndexOutOfBoundsException
      Removes the element at given index, rebuilds the container without this element
      Parameters:
      index - the index of an element to remove
      Returns:
      the value of the removed element
      Throws:
      IndexOutOfBoundsException - If the given index is not in a current container's bounds
    • toString

      public String toString()
      Returns the container's elements as a string
      Overrides:
      toString in class Object
      Returns:
      the container's elements