List:
//不能排序,可以有重复。
import java.util.*;
public class Main {
static Scanner cin = new Scanner(System.in);
static List <Integer> list = new LinkedList<Integer>();
public static void main(String[] args){
int n = cin.nextInt();
while(n-->0) {
list.add(cin.nextInt());//输入
}
System.out.println(list.get(3));
list.remove(2);
// forEach 循环
for (int x:list) {
System.out.print(x+" ");
}
System.out.println();
for (int i = 0; i < list.size();i++) {
System.out.print(list.get(i)+" ");
}
System.out.println();
/*
* 测试样例
5
2 3 5 0 1
0
2 3 0 1
2 3 0 1
*/
}
}
Set:
//TreeSet 是有序 从小到大 唯一但是HashSet我还不确定
import java.util.*;
public class Main {
static Scanner cin = new Scanner(System.in);
static Set<String> set1 = new TreeSet<String>();
static Set<String> set2 = new HashSet<String>();
public static void main(String[] args){
int n = cin.nextInt();
for (int i = 0; i < n;i++) {
set1.add(cin.next());//输入
}
// for (int i = 0; i < n;i++) {
// set2.add(cin.next());
// }
for (String x:set1) {
System.out.print(x+" ");
}
// for (String x:set2) {
// System.out.println(x+" ");
// }
System.out.println();
}
/*
5
c d a b a
a b c d
*/
}
Map<key-value> :
// Map<key-value>
//TreeMap 按照键值从小到大自动排序 HashMap 还有点迷
import java.util.*;
public class Main {
static Scanner cin = new Scanner(System.in);
static Map<String,Integer> map = new TreeMap<String,Integer>();
static Map<String,Integer> map1 = new HashMap<String,Integer>();
public static void main(String[] args){
int n = cin.nextInt();
while(n-->0) {
map.put(cin.next(), cin.nextInt());
//get()通过键来获取对应的值
}
// System.out.println(map.get("xx"));
//// 遍历map
//// 1.forEach
// Set<Map.Entry<String, Integer> >set = map.entrySet();
// for (Map.Entry<String, Integer> e:set) {
// System.out.println(e.getKey()+" "+e.getValue());
// }
/*
3
xx 1
cc 2
aa 3
1 //System.out.println(map.get("xx"));
aa 3
cc 2
xx 1
*/
//2.iterator 通过迭代器遍历集合
// Collection<Integer> c = map.values();
// Iterator<Integer> i = c.iterator();
// while(i.hasNext()) {
// System.out.println(i.next());
// }
/*
* 测试样例
3
c 1
b 3
a 4
4
3
1
*/
// //3.推荐 利用keyset 集合遍历map
// Set<String> key = map.keySet();
// for(String k:key) {
// System.out.println(k+": "+map.get(k));
// }
/*
3
xx 1
cc 2
aa 3
aa: 3
cc: 2
xx: 1
*/
//// //4.keyset iterator
// Set<String> key = map.keySet();
// Iterator <String> it = key.iterator();
// while(it.hasNext()) {
// String s = it.next();//我觉得这样写也行
// //学长这样写的String s = (String) it.next();
// System.out.println(s+": " + map.get(s));
// }
/*
3
xx 1
cc 2
aa 3
aa: 3
cc: 2
xx: 1
*/
}
}
Queue 先进先出 PriorityQueue按优先级自动排列(从小到大)
import java.util.*;
public class Main {
static Scanner cin = new Scanner(System.in);
static PriorityQueue<Integer> qq = new PriorityQueue<Integer>();//优先队列
static Queue<Integer> q = new LinkedList<Integer>();//普通队列,先进先出
public static void main(String[] args){
int n = cin.nextInt();
int x;
while(n-->0) {
x=cin.nextInt();
q.add(x);
}
//forEach 遍历集合
for(int e:q) {
System.out.print(e+" ");
}
System.out.println();
//size() 返回队列中的元素个数
System.out.println(q.size());
//peek() 返回队首元素
System.out.println(q.peek());
//poll 移除队列首元素,同时返回首元素的值
System.out.println(q.poll());
while(!q.isEmpty()) {
System.out.print(q.poll()+" ");
}
System.out.println();
System.out.println(q.size());
}
/*
* 普通队列测试样例
5
3 4 6 5 2
3 4 6 5 2
5
3
3
4 6 5 2
0
*/
// int n = cin.nextInt();
// int x;
// while(n-->0) {
// x=cin.nextInt();
// qq.add(x);
// }
// //forEach 遍历集合
// for(int e:qq) {
// System.out.print(e+" ");//不按照从小到大输出
// }
// System.out.println();
// //size() 返回队列中的元素个数
// System.out.println(qq.size());
// //peek() 返回队首元素
// System.out.println(qq.peek());
// //poll 移除队列首元素,同时返回首元素的值
// System.out.println(qq.poll());
// while(!qq.isEmpty()) {
// System.out.print(qq.poll()+" ");//按照从小到大输出
// }
// System.out.println();
// System.out.println(qq.size());
// }
/*
* 优先队列测试样例
5
3 4 6 5 2
2 3 6 5 4
5
2
2
3 4 5 6
0
*/
}
Stack: 后进先出
import java.util.*;
public class Main {
static Scanner cin = new Scanner(System.in);
static Stack<Integer> s = new Stack<Integer>();
public static void main(String[] args){
int n = cin.nextInt();
while(n-->0) {
int x = cin.nextInt();
s.push(x);
}
//输出栈内元素个数
System.out.println(s.size());
//输出栈顶元素
System.out.println(s.peek());
//弹出栈顶元素,也就是删除栈顶元素,并且返回栈顶元素的值
System.out.println(s.pop());
//输出坐标为2的元素,坐标从0开始
System.out.println(s.get(2));
while(!s.isEmpty()){
System.out.print(s.pop()+" ");
}
System.out.println();
System.out.println(s.size());
}
/*
5
3 4 2 1 5
5
5
5
2
1 2 4 3
0
*/
}
Vector:
import java.util.*;
public class Main {
static Scanner cin = new Scanner(System.in);
static Vector<Integer> v = new Vector<Integer>();
public static void main(String[] args){
int n=cin.nextInt();
while(n-->0) {
int x=cin.nextInt();
v.add(x);//输入
}
for(int i = 0; i < v.size();i++) {
System.out.print(v.get(i)+" ");
}
System.out.println();
System.out.println(v.remove(2));//移除下标为2的元素,下标从0开始, 并且返回该元素
for(int i = 0; i < v.size();i++) {
System.out.print(v.get(i)+" ");
}
System.out.println();
while(!v.isEmpty()){
System.out.print(v.remove(0)+" ");
}
System.out.println();
System.out.println(v.size());//输出元素个数
}
/*
*测试样例
5
3 1 4 5 2
3 1 4 5 2
4
3 1 5 2
3 1 5 2
0
*/
}