学习 资源1
学习资源 2
1、ArrayList
List是集合类型的一个分支,它的主要特性有:
- 是一个有序的集合,插入元素默认是插入到尾部,按顺序从前往后存放,每个元素都有一个自己的下标位置
- 列表中允许存在重复元素(equals方法判断)
import com.test.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.function.Predicate;
public class Main {
public static void main(String[] args)
{
ArrayList<String> list = new ArrayList<String>();
list.add("abc");
System.out.println(list.get(0)==new String("abc"));//false
System.out.println(list.contains(new String("abc")));//true
}
}
ArrayList增删改查
add():末尾加元素,或在某位置加元素
remove():删除某位置元素,或删除第一次找到的某元素
indexOf(): 返回某元素第一次出现下标
lastIndexOf():返回某元素最后一次出现的下标
Collections.sort():对元素排序
binarySearch(list,):二分查找某元素
get():获取某下标的元素
import com.test.*;
import java.text.CollationElementIterator;
import java.util.*;
public class Main {
public static void main(String[] args)
{
ArrayList<String> list = new ArrayList<>();
list.add("abc"); list.add("def");list.add("def");
list.add(1,"def");
System.out.println(list);//[abc, def, def, def]
list.set(0,"def");
System.out.println(list);//[def, def, def, def]
list.remove(0);
System.out.println(list);//[def, def, def]
list.remove("def");
System.out.println(list);//[def, def]
System.out.println(list.indexOf("def"));//0
System.out.println( list.lastIndexOf("def"));//1
Collections.sort(list);
System.out.println(list);//[def, def]
System.out.println(Collections.binarySearch(list,"def"));//0
}
}
2、迭代器
import com.test.*;
import java.text.CollationElementIterator;
import java.util.*;
public class Main {
public static void main(String[] args)
{
ArrayList<String> a = new ArrayList<String>();
a.add("1");
a.add("2");
a.add("3");
for(int i=0;i<a.size();i++)
System.out.print(a.get(i));//123
System.out.println();
for(String s:a)
System.out.print(s);//123
System.out.println();
Iterator<String> it = a.iterator();
while(it.hasNext())
{
System.out.print(it.next());//123
}
}
}
3、Queue
import com.test.*;
import java.text.CollationElementIterator;
import java.util.*;
public class Main {
public static void main(String[] args)
{
Queue<String> queue = new LinkedList<>();
queue.add("1");
queue.add("2");
queue.add("3");
System.out.println(queue.poll());//1
System.out.println(queue.poll());//2
System.out.println(queue.poll());//3
}
}
4、deque
import com.test.*;
import java.text.CollationElementIterator;
import java.util.*;
public class Main {
public static void main(String[] args)
{
Deque<String> deque = new LinkedList<>();
deque.add("1");
deque.add("2");
deque.add("3");
System.out.println(deque.peek());//1
System.out.println(deque.peekFirst());//1
System.out.println(deque.peekLast());//3
System.out.println(deque.poll());//1
System.out.println(deque.pollFirst());//2
}
}
5、set
由map实现的
add():添加元素
size():大小
contains():是否包含某元素
remove():删除某元素
import com.test.*;
import java.text.CollationElementIterator;
import java.util.*;
public class Main {
public static void main(String[] args)
{
Set<String> set = new HashSet<>();
set.add("3");
set.add("2");
set.add("1");
System.out.println(set);//[1, 2, 3]
}
}
6、ArrayDeque
双端队列,是循环数组,队首不一定在0处
add():增加元素
remove():删除元素
contains():包含元素
import com.test.*;
import java.text.CollationElementIterator;
import java.util.*;
public class Main {
public static void main(String[] args)
{
ArrayDeque<String> arrayDeque = new ArrayDeque<>();
arrayDeque.add("1");
arrayDeque.add("2");
arrayDeque.add("3");
arrayDeque.add("4");
arrayDeque.add("5");
System.out.println(arrayDeque);//[1, 2, 3, 4, 5]
arrayDeque.remove("2");
System.out.println(arrayDeque);//[1, 3, 4, 5]
arrayDeque.add("6");
System.out.println(arrayDeque);//[1, 3, 4, 5, 6]
}
}
7、LinkedList
LinkedList 同时实现了 Stack、Queue、PriorityQueue 的所有功能。
import com.test.*;
import java.util.*;
public class Main {
public static void main(String[] args)
{
LinkedList<String> list = new LinkedList<>();
list.add("1");
list.add("2");
list.offer("3");
System.out.println(list.get(0));//1
System.out.println(list);//[1, 2, 3]
list.poll();//去掉1
list.pop();//去掉2
System.out.println(list);//3
System.out.println(list.peek());//3 取队头
System.out.println(list.contains("2"));//false
list.offerFirst("5");
Iterator<String> it = list.iterator();
while(it.hasNext())
{
System.out.print(it.next()+" ");//5 3
}
}
}
8、PriorityQueue
import java.util.*;
public class Main {
public static void main(String[] args)
{
PriorityQueue<String> pq = new PriorityQueue<String>();
pq.add("b");
pq.add("a");
pq.add("d");
pq.offer("c");
System.out.println(pq);//[a, b, d, c]
pq.poll();//删除元素
System.out.println(pq);//[b, c, d]
System.out.println(pq.peek());//b
System.out.println(pq.contains("c"));//true
for(String s:pq)
{
System.out.print(s+" ");//b c d
}
}
}
9、HashMap
import java.util.*;
public class Main {
public static void main(String[] args)
{
HashMap<String,String> map=new HashMap<String,String>();
map.put("1","1");
map.put("2","2");
map.put("3","3");
System.out.println(map.get("1"));//1
map.put("1","11");
System.out.println(map.get("1"));//11
map.remove("1");
for (String i:map.keySet()) {
System.out.print(i+' '+map.get(i)+",");//2 2,3 3,
}
}
}