basic data structure APIs

  • Array has member length, String has a function length()
  • initialize a variable even you are sure that u assign a value later; it might be uninitialized.
  • HashMap:
  • String
    • public char charAt(int index)
    • substring(int beginIndex, int endIndex) 
      • endIndex — the end index, exclusive.
      • beginIndex -- the begin index, inclusivehttps://myrubylearning.wordpress.com/wp-admin/post.php?post=1184&action=edit
    • boolean isEmpty()
    • String is immutable. Char array is not. A string is implemented with a char array underneath but every time you try to modify it (like with concatenation, replace etc.) it gives you a new String object.
  • queue
    • peek()
      Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.
    • poll()
      Retrieves and removes the head of this queue, or returns null if this queue is empty.
    • add(E e)
      Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is currently available.
    • isEmpty() or size()  ==0 to tell if the queue is empty or not
    • LinkedList is an extention of queue; has similar API of queue
  • ArrayList
    • get(int index)
      Returns the element at the specified position in this list.
    • add(E e)
      Appends the specified element to the end of this list.
    • remove(int index)
      Removes the element at the specified position in this list.
    • contains(Object o)
      Returns true if this list contains the specified element.
    • size()
  • Integer.MAX_SIZE
  • Queue<TreeNode> nodes = new LinkedList<TreeNode>();
  • Stack<TreeNode> nodes = new Stack<TreeNode>();
  • Queue
    boolean add(E e)

    Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is currently available.
    E element()

    Retrieves, but does not remove, the head of this queue.
    boolean offer(E e)

    Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions.
    E peek()

    Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.
    E poll()

    Retrieves and removes the head of this queue, or returns null if this queue is empty.
  • Stack
    boolean empty() // isEmpty() also works. remember isEmpty() which exists for all collections

    Tests if this stack is empty.
    E peek()

    Looks at the object at the top of this stack without removing it from the stack.
    E pop()

    Removes the object at the top of this stack and returns that object as the value of this function.
    E push(E item)

    Pushes an item onto the top of this stack.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s