Method | Collection interface | StringBuilder class |
---|---|---|
add to | add() | append() |
Insert | add() | insert() |
Delete | remove(), removeAll(), removeIf() | delete(), deleteCharAt() |
Rewrite | set(), replace() | setCharAt() |
length | size() | length() |
public static void main(String[] args) {
StringBuilder sb = new StringBuilder(10);
System.out.println(sb.length()); // #=> 0
sb.append("123456", 1, 3);
sb.append("a");
sb.setCharAt(0, 'c');
sb.insert(1, "d");
System.out.println(sb); // #=> cd3a
sb.replace(0, 2, "ab");
sb.delete(0, 1);
sb.deleteCharAt(0);
List<String> list = new ArrayList<>();
list.add("a");
list.add("a");
list.add("a");
list.set(0, "b");
list.add(1, "b");
System.out.println(list); // #=> [b, b, a, a]
list.remove(0);
list.remove("b");
list.removeIf(s -> s.length() > 2);
System.out.println(list); // #=> [a, a]
list.removeAll(Arrays.asList("a", "b"));
list.size();
}
Recommended Posts