<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">1. 对于Java中的常量的命名规则:所有单词的字母都是大写,如果有多个单词,那么使用下划线连接即可。比如说:</span>
public static final int AGE_0F_PERSON = 20;
2. 在Java中声明final常量时通常都会加上static关键字,这样对象的每个实例都会访问唯一一份常量值。
3. IDE(Integrated Development Environment),集成开发环境。
1) NetBeans。http://netbeans.org/
2) JBuilder。
3) Intellij IDEA
4) Eclipse(日蚀、月蚀)
Eclipse常用配置
1、 智能提示.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW
2、 格式化:括号自动换行
Windows-Preferences->Java-CodeStyle-->Formater>New
Braces nextline
ControlStatement=>general
快捷键 ctrl+shift+F
3、 去掉方法自动填充参数
Windows-Preferences->Java-Editor-ContentAssist-去掉Fill method arguments
快捷键
ctrl+shift+O 自动导入包
按住ctrl 点击对应的类,查看源码
alt+← 回到上一次代码
//* 注释 生成帮助文档 project -》Genrate javadoc
4. 集合中存放的依然是对象的引用而不是对象本身。
public class ArrayListTest5
{
public static void main(String[] args)
{
ArrayList list = new ArrayList();
list.add(new Point(2, 3));
list.add(new Point(2, 2));
list.add(new Point(4, 4));
for(int i = 0; i < list.size(); i++)
{
System.out.println(list.get(i));//调用Point toString方法
<span style="white-space:pre"> </span>
}
System.out.println(list);//调用list toString方法
}
}
5. ArrayList底层采用数组实现,当使用不带参数的构造方法生成ArrayList对象,实际上是构建一个空的Object类型的数组
public ArrayList(int initialCapacity) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
}
/**
* Constructs an empty list with an initial capacity of ten.
*/
public ArrayList() {
super();
this.elementData = EMPTY_ELEMENTDATA;
}
6. 当增加元素时,首先确定数组容量(调用ensureCapacityInternal),如果数组容量超过10(DEFAULT_CAPACITY),则对数组进行扩容(grow),并将原数组copy到新的数组中。最后将新增加的元素放在新的数组末尾。(Java1.7、1.8中通过移位(>>1)进行扩容,实际上也相当于把长度变为原来的1.5倍。与1.6不同)
Add源码
/**
* Appends the specified element to the end of this list.
*
* @param e element to be appended to this list
* @return <tt>true</tt> (as specified by {@link Collection#add})
*/
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
private void ensureCapacityInternal(int minCapacity) {
if (elementData == EMPTY_ELEMENTDATA) {
minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
}
ensureExplicitCapacity(minCapacity);
}
private void ensureExplicitCapacity(int minCapacity) {
modCount++;
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)//private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
newCapacity = hugeCapacity(minCapacity);
// minCapacity is usually close to size, so this is a win:
elementData = Arrays.copyOf(elementData, newCapacity);
}
7. 对于ArrayList元素的删除操作,需要将被删除元素的后续元素向前移动,代价比较高。
public E remove(int index) {
rangeCheck(index);
modCount++;
E oldValue = elementData(index);
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null; // clear to let GC do its work
return oldValue;
}
8. 集合当中只能放置对象的引用,无法放置原生数据类型,我们需要使用原生数据类型的包装类才能加入到集合当中。
9. 集合当中放置的都是Object类型,因此取出来的也是Object类型,那么必须要使用强制类型转换将其转换为真正的类型(放置进去的类型)。
10. 关于ArrayList与LinkedList的比较分析
a) ArrayList底层采用数组(顺序表)实现,LinkedList底层采用双向链表实现。
b) 当执行插入或者删除操作时,采用LinkedList比较好。
c) 当执行搜索操作时,采用ArrayList比较好。
LinkedList源码:
public LinkedList() {
}
public boolean add(E e) {
linkLast(e);
return true;
}
void linkLast(E e) {
final Node<E> l = last;
final Node<E> newNode = new Node<>(l, e, null);
last = newNode;
if (l == null)
first = newNode;
else
l.next = newNode;
size++;
modCount++;
}
private static class Node<E> {
E item;
Node<E> next;
Node<E> prev;
Node(Node<E> prev, E element, Node<E> next) {
this.item = element;
this.next = next;
this.prev = prev;
}
}