List集合:
void add(int index, E element)
Inserts the specified element at the specified position in this list (optional operation).
在此集合中的指定位置插入指定元素
E remove(int index)
Removes the element at the specified position in this list (optional operation).
删除指定索引处的元素,并返回被删除的元素
E set(int index, E element)
Replaces the element at the specified position in this list with the specified element (optional operation).
修改指定索引处的元素 ,修改成功返回原来的数据
E get(int index)
Returns the element at the specified position in this list.
返回指定索引处的元素
java
public class java {
public static void main(String[] args) {
List<String>list=new ArrayList<>();//List是一个抽象类
//List 有序,可重复,有索引
//add
list.add("java1");
list.add("java2");
list.add("java3");
System.out.println(list);
list.add(1,"java4");
System.out.println(list);//[java1, java4, java2, java3]
//remove
System.out.println(list.remove(1));//java4
//get
System.out.println(list.get(0));
//set,返回被修改的数据
System.out.println(list.set(0, "java20"));//java1
System.out.println(list);//[java20, java2, java3]
}
}
List集合的遍历:
1 for循环(List集合有索引)
2 迭代器
3 for循环增强
4 Lambda表达式
java
public class test {
public static void main(String[] args) {
List<String>list=new ArrayList<>();
list.add("java1");
list.add("java2");
list.add("java3");
System.out.println(list);
//for循环
for(int i=0;i<list.size();i++)
{
System.out.println(list.get(i));
}
//迭代器
Iterator<String> iterator = list.iterator();
while(iterator.hasNext())
{
String next = iterator.next();
System.out.println(next);
}
//增强for循环
for(String s:list)
{
System.out.println(s);
}
//Lambda表达式
list.forEach(new Consumer<String>() {
@Override
public void accept(String s) {
System.out.println(s);
}
});
list.forEach(s-> System.out.println(s));
list.forEach(System.out::println);
}
}
ArrayList集合的底层原理
基于数组实现的
特点:
查询数据快:(注意:是根据索引查询数据块)
删除效率低:可能需要把后面的大量数据往前移动
添加效率低:将后面大量数据后移动
ArrayList适合根据随机索引取数据(高效)或者数据不是很大的时候
不适合数据量大很大,又频繁的增删数据
LinkedList集合的底层逻辑
基于双向链表实现的
提点:
查询满,增删块**,但是对首尾元素的增删改查的速度是极快的**
public void addFirst(E e)
Inserts the specified element at the beginning of this list.
public void addLast(E e)
Appends the specified element to the end of this list.
public E getFirst()
Returns the first element in this list.
public E getLast()
Returns the last element in this list.
public E removeFirst()
Removes and returns the first element from this list.
从链表中返回第一个元素,并且删除
public E removeLast()
Removes and returns the last element from this list.
用于创建队列
java
public class test {
public static void main(String[] args) {
LinkedList <String>queue=new LinkedList<>();
//入队
queue.addLast("java1");
queue.addLast("java2");
//出队
System.out.println(queue.removeFirst());//java1
System.out.println(queue.removeFirst());//java2
}
}
用于创建栈
java
public class test2 {
public static void main(String[] args) {
LinkedList<String>stack=new LinkedList<>();
//入栈
/*stack.addFirst("java1");
stack.addFirst("java2");*/
stack.push("java1");
stack.push("java2");
//出栈
/* System.out.println(stack.removeFirst());//java2
System.out.println(stack.removeFirst());//java1*/
System.out.println(stack.pop());//java2
System.out.println(stack.pop());//java1
}
}