1.java程序获取控制台输入的方法
import java.util.Scanner;//先导包
Scanner sc = new Scanner(System.in);
String str = sc.next();//获取的输入为String类型
int a1 = sc.nextInt();//获取的输入为int类型
double a2= sc.nextDouble();//获取的输入为double类型
2.Java建立一个栈
Stack.peek()和Stack.pop()的区别
Stack<TreeNode> stack = new Stack<>(); //
1. Stack.peek()
peek()函数返回栈顶的元素,但不弹出该栈顶元素。
2. Stack.pop()
pop()函数返回栈顶的元素,并且将该栈顶元素出栈。
3.Java建立一个队列
使用:Deque接口
Deque<Integer> test = new LinkedList<>();
int currentLevelSize = queue.size(); // 队列长度
public class Main {
public static void main(String[] args) {
//add()和remove()方法在失败的时候会抛出异常(不推荐)
Queue<String> queue = new LinkedList<String>();
//添加元素
queue.offer("a");
queue.offer("b");
queue.offer("c");
queue.offer("d");
queue.offer("e");
for(String q : queue){
System.out.println(q);
}
System.out.println("===");
System.out.println("poll="+queue.poll()); //返回第一个元素,并在队列中删除
for(String q : queue){
System.out.println(q);
}
System.out.println("===");
System.out.println("element="+queue.element()); //返回第一个元素
for(String q : queue){
System.out.println(q);
}
System.out.println("===");
System.out.println("peek="+queue.peek()); //返回第一个元素
for(String q : queue){
System.out.println(q);
}
}
}
a
b
c
d
e
===
poll=a
b
c
d
e
===
element=b
b
c
d
e
===
peek=b
b
c
d
e
Deque是一个双端队列接口,继承自Queue接口,Deque的实现类是LinkedList、ArrayDeque、LinkedBlockingDeque,其中LinkedList是最常用的。
4.LinkedList用法
参考:
LinkedList用法详解_韩家老大的博客-CSDN博客_linkedlist
5.ArrayList用法
3-1.ArrayList的添加方法
方法1:依次按照顺序向ArrayList中添加数据。
用法:将a添加到list中,list.add("a");
3-2 ArrayList的删除方法
方法1:按照位置删除单个数据
用法:将list中第2个数据删除
list.remove(2);
注意:位置从0开始计算(0、1、2、3...)
方法2:按照内容删除单个数据
用法:
将list中的数据"d"删除
list.remove("d");
注意:对于int,String,char这样的原始类型数据是可以删除的,但是对于复杂对象,例如自己编写的User类、Person类对象,需要重写equals方法,负责remove方法无法匹配删除。
参考:
Java的ArrayList用法_Fighting_初心的博客-CSDN博客_arraylist用法
中序和后序序列,建立树
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("张三", 18);
map.put("李四", 28);
map.put("王五", 38);
map.put("赵六", 48);
//1.获取map集合的entry 集合
Set<Map.Entry<String, Integer>> entries = map.entrySet();
//2.用迭代器 遍历这个entries这个Set集合
Iterator<Map.Entry<String, Integer>> it = entries.iterator();
//3.遍历
while(it.hasNext()){
Map.Entry<String, Integer> entry = it.next();
//一个entry中 有两个属性
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println(key+"="+value);
}
public class MapDemo {
public static void main(String[] args) {
//定义一个Map集合 存储键为学生(姓名,年龄)值为学生自己的家庭住址。
Map<Student, String> map = new HashMap<Student, String>();
//向集合添加数据
map.put(new Student("王宝强", 40), "北京五环外");
map.put(new Student("谢霆锋", 50), "北京180环外");
map.put(new Student("马蓉", 45), "上海交通路");
map.put(new Student("郭德纲", 55), "广州德云社");
map.put(new Student("马蓉", 45), "我家");
//map判断键重复不重复,是通过hashCode和equals方法
//如果 我要求一个学生的姓名和年龄一样 就认为是同一个学生
//遍历集合 keySet entrySet
//1.获取entry的集合
Set<Map.Entry<Student, String>> entries = map.entrySet();
//2.迭代器遍历 foreach
for (Map.Entry<Student, String> entry : entries) {
Student key = entry.getKey();
String value = entry.getValue();
System.out.println(key+"="+value);
}
}
}