JAVA知识点
异常
Throwable
throw和throws
集合使用步骤
list
public class ListDemo {
public static void main(String[] args) {
List<Student> list = new ArrayList<>();
Student s1 = new Student("张大帅", 30);
Student s2 = new Student("林丹", 32);
Student s3 = new Student("张一位", 25);
list.add(s1);
list.add(s2);
list.add(s3);
//迭代器:集合特有的遍历方式
Iterator<Student> it = list.iterator();
while (it.hasNext()) {
Student s = it.next();
System.out.println(s.getName() + "," + s.getAge());
}
//普通for:带有索引的遍历方式
for (int i = 0; i < list.size(); i++) {
Student s = list.get(i);
System.out.println(s.getName() + "," + s.getAge());
}
//增强for:最方便的遍历方式
for (Student s :list){
System.out.println(s.getName()+","+s.getAge());
}
}
}
运行结果
张大帅,30
林丹,32
张一位,25
Set
哈希值
TreeSet
(不包含重复元素)
public class TreeSetDemo {
public static void main(String[] args) {
TreeSet<Integer> treeSet = new TreeSet<>();
treeSet.add(12);
treeSet.add(56);
treeSet.add(14);
treeSet.add(25);
treeSet.add(12);
for (Integer i:treeSet){
System.out.println(i);
}
}
}
结果
12
14
25
56
案列:无参构造
student类
public class Student implements Comparable<Student>{
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public int compareTo(Student a) {
//从小到大排序
int num = this.age-a.age;
//年龄相同,按照姓名子母排序
int num2=num==0?this.name.compareTo(a.name):num;
return num2;
}
}
-----------------
public class TreeSetDemo {
public static void main(String[] args) {
TreeSet<Student> ts = new TreeSet<Student>();
Student s1 = new Student("haha", 23);
Student s2 = new Student("than", 26);
Student s3 = new Student("sasa", 12);
Student s4 = new Student("haha", 32);
Student s5 = new Student("haa", 32);
ts.add(s1);
ts.add(s2);
ts.add(s3);
ts.add(s4);
ts.add(s5);
for (Student s:ts){
System.out.println(s.getName()+","+s.getAge());
}
}
}
运行结果
sasa,12
haha,23
than,26
haa,32
haha,32
有参构造
public class TreeSetDemo_1 {
public static void main(String[] args) {
TreeSet<Student> ts = new TreeSet<>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
int num = s1.getAge() - s2.getAge();
int num2 = num == 0 ? s1.getName().compareTo(s2.getName()) : num;
return num2;
}
});
Student s1 = new Student("haha", 23);
Student s2 = new Student("than", 26);
Student s3 = new Student("sasa", 12);
Student s4 = new Student("haha", 32);
Student s5 = new Student("haa", 32);
ts.add(s1);
ts.add(s2);
ts.add(s3);
ts.add(s4);
ts.add(s5);
for (Student s:ts){
System.out.println(s.getName()+","+s.getAge());
}
}
}
运行结果
sasa,12
haha,23
than,26
haa,32
haha,32
案列:不重复的随机数
(无序)
public class SetDemo {
public static void main(String[] args) {
Set<Integer> integers = new HashSet<Integer>();
Random r = new Random();
while (integers.size()<10){
int number=r.nextInt(20)+1;
integers.add(number);
}
for (Integer i:integers){
System.out.println(i);
}
}
}
运行结果
16
17
3
4
5
7
8
10
13
15
(有序)
public class SetDemo {
public static void main(String[] args) {
Set<Integer> integers = new TreeSet<Integer>();
Random r = new Random();
while (integers.size()<10){
int number=r.nextInt(20)+1;
integers.add(number);
}
for (Integer i:integers){
System.out.println(i);
}
}
}
运行结果
1
2
3
6
8
10
12
13
15
17
泛型方法
Generic类
public class Generic<T> {
public void show(T t){
System.out.println(t);
}
}
GenericDemo类
public class GenericDemo {
public static void main(String[] args) {
Generic<String> g1 = new Generic<Sting>();
g1.show("张森");
Generic<Integer> g2 = new Generic<Integer>();
g2.show(30);
Generic<Boolean> g3 = new Generic<Boolean>();
g3.show(true);
}
}
运行结果
张森
30
true
改进版
public class Generic<T> {
public <T> void show(T t){
System.out.println(t);
}
}
-------------------
public class GenericDemo {
public static void main(String[] args) {
Generic g= new Generic();
g.show("林丹");
g.show(52);
g.show(true);
}
}
类型通配符
可变参数
public class Demo {
public static void main(String[] args) {
System.out.println(sum(10,20,30,40));
System.out.println(sum(10,20,30,40,50));
System.out.println(sum(10,20,30,40,50,60));
}
public static int sum(int... a){
int sum=0;
for (int i:a){
sum+=i;
}
return sum;
}
}
运行结果
100
150
210
注意事项
-
这里的变量是一个数组
-
如果一方法有多个参数,包含可变参数,可变参数要放在最后
可变参数使用
Map
集合的获取
public class MapDemo {
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
map.put("asd01","张森");
map.put("asd02","诞生");
map.put("asd03","科大");
// System.out.println(map.get("asd01"));
//获取所有键的集合
Set<String> keySet = map.keySet();
for (String key : keySet){
System.out.println(key);
}
**运行结果**
asd02
asd03
asd01
//获取所有值的集合
Collection<String> values = map.values();
for (String i :values){
System.out.println(i);
}
**运行结果**
诞生
科大
张森
}
}
Map集合遍历
方法一
Set<String> keySet = map.keySet();
for (String key:keySet){
System.out.println(key+","+ map.get(key));
}
方法二
Set<Map.Entry<String, String>> entrySet = map.entrySet();
for (Map.Entry<String,String> i:entrySet){
String key = i.getKey();
String value = i.getValue();
System.out.println(key+","+value);
ArrayList嵌套HashMap
public class MapDemo_1 {
public static void main(String[] args) {
ArrayList<HashMap<String, String>> arrayList = new ArrayList<>();
HashMap<String, String> hm1 = new HashMap<>();
hm1.put("张飞", "翼德");
arrayList.add(hm1);
HashMap<String, String> hm2 = new HashMap<>();
hm1.put("关羽", "武帝");
arrayList.add(hm2);
HashMap<String, String> hm3 = new HashMap<>();
hm1.put("赵云", "子龙");
arrayList.add(hm3);
for (HashMap<String, String> hm : arrayList) {
Set<String> keySet = hm.keySet();
for (String key : keySet) {
String value = hm.get(key);
System.out.println(key + "," + value);
}
}
}
}
----------------
关羽,武帝
张飞,翼德
赵云,子龙
HashMap嵌套ArrayList
public class HashMap_include_ArrayList {
public static void main(String[] args) {
HashMap<String, ArrayList<String>> hashMap = new HashMap<>();
ArrayList<String> sg = new ArrayList<>();
sg.add("赵云");
hashMap.put("三国演义", sg);
ArrayList<String> sh = new ArrayList<>();
sh.add("宋江");
hashMap.put("水浒传", sh);
ArrayList<String> xy = new ArrayList<>();
xy.add("孙悟空");
hashMap.put("西游记", xy);
Set<String> keySet = hashMap.keySet();
for (String key:keySet){
System.out.println(key);
ArrayList<String> value = hashMap.get(key);
for (String s :value){
System.out.println('\t'+s);
}
}
}
}
-----------------------
水浒传
宋江
三国演义
赵云
西游记
孙悟空
案列:统计字符串中每个字符出现的次数
public class MapDemo_1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入字符串");
String line = sc.nextLine();
// HashMap<Character, Integer> hashMap = new HashMap<>();
TreeMap<Character, Integer> hashMap = new TreeMap<>();
for (int i=0;i<line.length();i++){
char key = line.charAt(i);
Integer value = hashMap.get(key);
if(value==null){
hashMap.put(key,1);
}else {
value++;
hashMap.put(key,value);
}
}
//进行拼接
StringBuilder sb = new StringBuilder();
Set<Character> keySet = hashMap.keySet();
for (Character key:keySet){
Integer value = hashMap.get(key);
sb.append(key).append("(").append(value).append(")");
}
String result = sb.toString();
System.out.println(result);
}
}
---------------
输入字符串
aassdfg
a(2)d(1)f(1)g(1)s(2)
File
遍历目录
字节流
实现换行
window:\r\n
linux:\n
mac:\r
追加写入
public FileOutputStream(String name,boolean append)
字节流读数据
FileInputStream file = new FileInputStream("idea_test\\dox.txt");
int by;
while ((by=file.read())!=-1){
System.out.print((char)by);
}
file.close();
字节缓冲流
字节缓冲流复制
public class BufferedDemo {
public static void main(String[] args) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\网易云\\梦一场.jpg"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("idea_test\\梦一场.jpg"));
byte[] bys = new byte[1024];
int len;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
bis.close();
bos.close();
}
}
字符串中编码解码
public static void main(String[] args) throws UnsupportedEncodingException {
//编码
String s ="中国";
byte[] bytes = s.getBytes("UTF-8");
System.out.println(Arrays.toString(bytes));
}
[-28, -72, -83, -27, -101, -67]
//解码
String s ="中国";
byte[] bytes = s.getBytes("UTF-8");
String ss=new String(bytes,"UTF-8");
System.out.println(Arrays.toString(bytes));
System.out.println(ss);
[-28, -72, -83, -27, -101, -67]
中国
字符流
写数据
public static void main(String[] args)throws IOException {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream("idea_test\\oso.txt"));
outputStreamWriter.write(97);
outputStreamWriter.flush();//刷新流可以继续写数据
outputStreamWriter.close();//关闭流不可以再写数据
}
读数据
InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream("idea_test\\oso.txt"));
char[] chars = new char[1024];
int len;
while ((len=inputStreamReader.read(chars))!=-1){
System.out.println(new String(chars,0,len));
}
inputStreamReader.close();
}
字符缓冲流
BufferedReader reader = new BufferedReader(new FileReader("idea_test\\oso.txt"));
char[] chars = new char[1024];
int len;
while ((len= reader.read(chars))!=-1){
System.out.println(new String(chars,0,len));
}
字符缓冲流特有功能
void newLine( ); //写一行行分隔符
public String readLine( ); //读一行文字,结果包含行的内容的字符串,不包括任何行终止字符,如果字符流结尾已到达,则为null。
IO流小结
案例:点名器
public static void main(String[] args)throws IOException {
BufferedReader buf = new BufferedReader(new FileReader("idea_test\\name.txt"));
ArrayList<String> arrayList = new ArrayList<>();
String line;
while ((line=buf.readLine())!=null){
arrayList.add(line);
}
buf.close();
Random random = new Random();
int i = random.nextInt(arrayList.size());
String name = arrayList.get(i);
System.out.println("幸运者是:"+name);
}
}
-----------------------
幸运者:欧文
复制单级文件夹
public static void main(String[] args) throws IOException {
File srcFolder = new File("D:\\wondtest");
String srcFolderName = srcFolder.getName();
File destFolder = new File("idea_test", srcFolderName);
if (!destFolder.exists()){
destFolder.mkdir();
}
File[] listFiles = srcFolder.listFiles();
for (File srcFile :listFiles){
String srcFileName = srcFile.getName();
File destFile = new File(destFolder, srcFileName);
copyFile(srcFile,destFile);
}
}
private static void copyFile(File srcFile, File destFile)throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
byte[] bys = new byte[1024];
int len;
while ((len=bis.read(bys))!=-1){
bos.write(bys,0,len);
}
bos.close();
bis.close();
}
复制多级文件夹
public class CopyFolderDemo {
public static void main(String[] args) throws IOException {
File srcFile = new File("D:\\wondtest");
File destFile = new File("F:\\");
copyFolder(srcFile,destFile);
}
//复制文件夹
private static void copyFolder(File srcFile, File destFile)throws IOException {
//判断数据源File是否是目录
if (srcFile.isDirectory()){
String srcFileName = srcFile.getName();
File newFolder = new File(destFile, srcFileName);
if (!newFolder.exists()){
newFolder.mkdir();
}
File[] fileArray = srcFile.listFiles();
for (File file:fileArray){
copyFolder(file,newFolder);
}
}else {
File newFile = new File(destFile, srcFile.getName());
copyFile(srcFile,newFile);
}
}
//复制文件
private static void copyFile(File srcFile, File desFile)throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desFile));
byte[] bys = new byte[1024];
int len;
while ((len=bis.read(bys))!=-1){
bos.write(bys,0,len);
}
bos.close();
bis.close();
}
}
序列化和反序列化
public class Student implements Serializable {
private static final long serialVersionUID =42L;
private String name;
private transient int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
public class ObjectStreamDemo {
public static void main(String[] args) throws IOException, ClassNotFoundException {
write();
read();
}
//反序列化
private static void read() throws IOException, ClassNotFoundException {
ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("idea_test\\oso.txt"));
Object object = inputStream.readObject();
Student s=(Student)object;
System.out.println(s.getName()+","+s.getAge());
inputStream.close();
}
//序列化
private static void write() throws IOException{
ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("idea_test\\oso.txt"));
Student s = new Student("张达", 12);
outputStream.writeObject(s);
outputStream.close();
}
}