Awesome C++
A curated list of awesome C/C++ frameworks, libraries, resources, and shiny things.
We can freely search all kinds of C++ libraries we need on https://github.com/IDLER1229/awesome-cpp
        
        
          Idle About
No results found
A curated list of awesome C/C++ frameworks, libraries, resources, and shiny things.
We can freely search all kinds of C++ libraries we need on https://github.com/IDLER1229/awesome-cpp
Reoriganize From http://www.programcreek.com/2014/05/top-10-mistakes-java-developers-make/
用以下方式将数组转成ArrayList1
List<String> list = Arrays.asList(arr);
Arrays.asList()返回的Arrays的一个内部静态类ArrayList, 而不是java.util.ArrayList. java.util.Arrays.ArrayList会有一些诸如set(), get(), contains()的方法, 但是没有任何添加元素的方法, 因为这个类是定长的. 正确的做法如下:1
ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(arr));
ArrayList 是允许接收一个集合类为参数的, 所以要把java.util.Arrays.ArrayList再通过ArrayList给构造一下.
有人总喜欢这么做1
2Set<String> set = new HashSet<String>(Arrays.asList(arr));
return set.contains(targetValue);
但是没必要, 因为直接用也能看有无包含元素. 转成Set没有必要, 还要浪费构造开销.1
Arrays.asList(arr).contains(targetValue);
甚至1
2
3
4
5for(String s: arr){
	if(s.equals(targetValue))
		return true;
}
return false;
但第一个可读性好一点
size会变1
2
3
4
5ArrayList<String> list = new ArrayList<String>(Arrays.asList("a", "b", "c", "d"));
for (int i = 0; i < list.size(); i++) {
	list.remove(i);
}
System.out.println(list);
迭代器异常, 会爆ConcurrentModificationException.1
2
3
4
5
6ArrayList<String> list = new ArrayList<String>(Arrays.asList("a", "b", "c", "d"));
 
for (String s : list) {
	if (s.equals("a"))
		list.remove(s);
}
正确的方式1
2
3
4
5
6
7
8
9ArrayList<String> list = new ArrayList<String>(Arrays.asList("a", "b", "c", "d"));
Iterator<String> iter = list.iterator();
while (iter.hasNext()) {
	String s = iter.next();
 
	if (s.equals("a")) {
		iter.remove();
	}
}
因为在foreach循环里, 编译器会自动将删除元素后再进行迭代器的next()操作, 这就会引起ConcurrentModificationException.异常. 可以对比一下Iterator的源码.
Hashtable 和 HashMap 最大的区别是同步化.
Java里, 原生态型(raw type)和通配符(wildcard type)可以混用. 比如 Set<?>.
但如下代码1
2
3
4
5
6
7
8public static void add(List list, Object o){
	list.add(o);
}
public static void main(String[] args){
	List<String> list = new ArrayList<String>();
	add(list, 10);
	String s = list.get(0);
}
会爆1
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String at ...
因为原生态型的集合类跳过了泛型检查, Set, Set<?>, 和 Set<Object> 是存在很大差别的. More details in Raw type vs. Unbounded wildcard .
public很方便, 但是这样设计很丑陋.
如何优雅设计, 参见public, default, protected, and private.
LinkedList 大量删除插入, 少量随机访问
ArrayList 正好相反.
mutable通常用来避免过多的中间对象. 一个经典的例子就是大数字符串, 如果使用immutable的字符串, 那就将产生大量中间对象, 既耗空间又浪费时间, 还要垃圾立即回收.1
2
3
4String result="";
for(String s: arr){
	result = result + s;
}
因此使用StringBuffer这种mutable的类比较合适.
more details in Why String is immutable in Java ? 
!pic
父类默认构造未定义, 但是子类定义了. Java里, 一个类如果没有定义构造方法, 编译器会自动插一个无参的, 但是如果显示定义了那就不插了(这和C++不一样).
而此时子类的无参构造需要调用父类的无参构造, 但是发现不存在, 因此要报错.
要想解决这个问题, 要么给父类加一个无参的1
2
3public Super(){
    System.out.println("Super");
}
要么把自定义的父类构造给去了, 再要么直接把super(value)塞子类构造里.
两种方式创建String1
2
3
4//1. use double quotes
String x = "abc";
//2. use constructor
String y = new String("abc");
但是1
2
3
4
5
6
7
8
9String a = "abcd";
String b = "abcd";
System.out.println(a == b);  // True
System.out.println(a.equals(b)); // True
 
String c = new String("abcd");
String d = new String("abcd");
System.out.println(c == d);  // False
System.out.println(c.equals(d)); // True
字符串直接赋值是常量, 因此都相等; 但new的时候是各自开辟空间, == 比较的是对象是否相等, 因而不等; equals比较的是值那就相等.
1  | class Solution {  |