8Eclipse使用 ArrayList,LinkedList

编程入门 行业动态 更新时间:2024-10-06 18:25:47

8<a href=https://www.elefans.com/category/jswz/34/1769691.html style=Eclipse使用 ArrayList,LinkedList"/>

8Eclipse使用 ArrayList,LinkedList

<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。/

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 codeif (minCapacity - elementData.length > 0)grow(minCapacity);}
private void grow(int minCapacity) {// overflow-conscious codeint 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 workreturn 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;elsel.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;}}








 

版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:

更多推荐

8Eclipse使用 ArrayList,LinkedList

本文发布于:2024-02-17 18:06:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1694972.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Eclipse   ArrayList   LinkedList

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!