13)Java Misc.:

Java Misc.:

  • Length: array.length; ArrayList.size(); string.length();
  • HashMap<T>.put(key, value); map.get(key);
  • ArrayList:
    • add(element)
    • contains(element)
    • get(index)
  • LinkedList:
    • add(element)
      With or without an additional index argument. Without, adds to tail.
    • remove(element/index)
    • get(index)
    • getFirst()
      Dequeue if implementing queue.
    • list.size()
  • Stack:
    • empty()
    • peek()
    • pop()
    • push(element)
    • search(element)
      Returns offset from top of stack.
  • Arrays.sort(primitiveArray);
  • Collections.sort(arrayList);
  • Primitive arrays have to be initialized with a capacity, ArrayLists don't -- they dynamically grow.
  • currChar = theStr.charAt(i);
  • From char to String and back:
    • String s = String.valueOf(c);
    • char c = s.charAt(0);
    • char[] chars = s.toCharArray();
  • From int to String and back:
    • String str = String.valueOf(foo);
      Note that this is the same as above!
    • int foo = Integer.parseInt(str)
  • The conditional operator can seem weird:
    • int x = booleanExpr ? thisNum : thatNum;
      Note that it has to return a value.
    • But it's just equivalent to this:
      • if (booleanExpr) x = thisNum;
      • else { x = thatNum; }

No comments

darkmode