linkedlist使用
package MONA.demo02_linkedlist;
import java.util.LinkedList;
import java.util.List;
/**
* linkedlist
*/
public class Demo01 {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
list.add("aaa");
list.add("bbb");
list.add("ccc");
list.add("ddd");
list.add("ccc");
//特有的方法
//添加到列头
//list.addFirst("eee");
//添加到列尾
//list.addLast("ddd");
//删除列头
//list.removeFirst();
//删除列尾
//list.removeLast();
System.out.println("ccc最后出现的索引"+list.lastIndexOf("ccc"));
System.out.println(list);
}
}
linkedlist数据结构
代码1
package MONA.demo02_linkedlist;
import java.util.ArrayList;
import java.util.LinkedList;
/**
* ArrayList和LinkedList速度
*/
public class Demo02 {
public static void main(String[] args) {
arr();
link();
}
//ArrayList添加多个数据
public static void arr(){
ArrayList<String> list = new ArrayList();
long start = System.currentTimeMillis();
for (int i = 0; i < 250000; i++) {
list.add(0,i+"");
}
long end = System.currentTimeMillis();
System.out.println("arr = "+(end-start)/1000);
}
public static void link(){
LinkedList<String> list1 = new LinkedList();
long start1 = System.currentTimeMillis();
for (int i = 0; i < 250000; i++) {
list1.add(0,i+"");
}
long end1 = System.currentTimeMillis();
System.out.println("link = "+(end1-start1)/1000);
}
}
//结果是link()快于arr()
代码2
package MONA.demo02_linkedlist;
import java.util.LinkedList;
import java.util.ListIterator;
/**
* 迭代器遍历LinkedList
*/
public class T {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
list.add("aaa");
list.add("bbb");
list.add("ccc");
list.add("ddd");
list.add("ccc");
ListIterator<String> it = list.listIterator();
while(it.hasNext()){
System.out.println(it.next());
}
System.out.println(list);
}
}