前言
本期任务:毕向东老师Java视频教程学习笔记(共计25天)
原视频链接:黑马程序员_毕向东_Java基础视频教程day01:编写HelloWorld程序day02:操作符与条件选择语句day03:循环语句与函数day04:数组day07:继承、抽象类与接口day08:多态day09:异常处理day11:多线程day12:线程安全与同步机制day13:String类day14:集合(ArrayList,LinkedList,HashSet)day15:集合(TreeSet)和泛型)day16:集合(HashMap、TreeMap)day17:集合框架的工具类(Arrays、Collections)day18:IO流(字符流读写)day19:IO流(字节流、转换流读写)day20:IO流(File对象)
代码
import java
.util
.*
;
class Student implements Comparable {
private String name
;
private int age
;
Student(String name
, int age
) {
this.name
= name
;
this.age
= age
;
}
public String
getName() {
return name
;
}
public int getAge() {
return age
;
}
public int compareTo(Object obj
) {
if (!(obj
instanceof Student))
throw new RuntimeException("不是学生对象");
Student s
= (Student
) obj
;
if (age
> s
.age
)
return 1;
if (age
== s
.age
)
return s
.name
.compareTo(name
);
return -1;
}
}
public class TreeSetDemo {
public static void main(String
[] args
) {
TreeSet ts
= new TreeSet();
ts
.add(new Student("张三", 10));
ts
.add(new Student("张三", 10));
ts
.add(new Student("张三", 11));
ts
.add(new Student("李四", 20));
ts
.add(new Student("王五", 15));
Iterator it
= ts
.iterator();
while (it
.hasNext()) {
Student s
= (Student
) it
.next();
System
.out
.println(s
.getName() + "......" + s
.getAge());
}
}
}
import java
.util
.*
;
class Student2 {
private String name
;
private int age
;
Student2(String name
, int age
) {
this.name
= name
;
this.age
= age
;
}
public String
getName() {
return name
;
}
public int getAge() {
return age
;
}
}
class MyCompare implements Comparator {
public int compare(Object obj1
, Object obj2
) {
Student2 s1
= (Student2
) obj1
;
Student2 s2
= (Student2
) obj2
;
int num
= new Integer(s1
.getAge()).compareTo(new Integer(s2
.getAge()));
if (num
== 0) {
return s1
.getName().compareTo(s2
.getName());
}
return num
;
}
}
public class TreeSetDemo2 {
public static void main(String
[] args
) {
TreeSet ts
= new TreeSet(new MyCompare());
ts
.add(new Student2("张三", 10));
ts
.add(new Student2("张三", 10));
ts
.add(new Student2("张三", 11));
ts
.add(new Student2("李四", 20));
ts
.add(new Student2("王五", 15));
Iterator it
= ts
.iterator();
while (it
.hasNext()) {
Student2 s
= (Student2
) it
.next();
System
.out
.println(s
.getName() + "......" + s
.getAge());
}
}
}
import java
.util
.*
;
class LenComparator implements Comparator {
public int compare(Object obj1
, Object obj2
) {
String s1
= (String
) obj1
;
String s2
= (String
) obj2
;
int num
= new Integer(s1
.length()).compareTo(new Integer(s2
.length()));
if (num
== 0) {
return s1
.compareTo(s2
);
}
return num
;
}
}
public class TreeSetTest {
public static void main(String
[] args
) {
TreeSet ts
= new TreeSet(new LenComparator());
ts
.add("jdkaflskdijf");
ts
.add("asdfa");
ts
.add("gasdf");
ts
.add("dafgds");
ts
.add("sadjfl;dj");
Iterator it
= ts
.iterator();
while (it
.hasNext()) {
System
.out
.println(it
.next());
}
}
}
import java
.util
.TreeSet
;
public class TreeSetTest2 {
public static void main(String
[] args
) {
String str
= "90 -7 0 18 2 45 4";
String
[] arr
= str
.split(" ");
TreeSet ts
= new TreeSet();
for (int x
= 0; x
< arr
.length
; x
++) {
ts
.add(new Integer(arr
[x
]));
}
System
.out
.println(ts
);
}
}
import java
.util
.*
;
public class GenericDemo {
public static void main(String
[] args
) {
ArrayList
<String> al
= new ArrayList<String>();
al
.add("flkads;l");
al
.add("dfajdslk;l");
System
.out
.println(al
);
Iterator
<String> it
= al
.iterator();
while(it
.hasNext()){
System
.out
.println(it
.next());
}
}
}
import java
.util
.Comparator
;
import java
.util
.Iterator
;
import java
.util
.TreeSet
;
public class GenericDemo2 {
public static void main(String
[] args
) {
TreeSet
<String> ts
= new TreeSet<String>(new LenComparator2());
ts
.add("dakfhjl");
ts
.add("dafdfasdf");
ts
.add("daffadsf a");
Iterator
<String> it
= ts
.iterator();
while (it
.hasNext()) {
System
.out
.println(it
.next());
}
}
}
class LenComparator2 implements Comparator<String> {
public int compare(String o1
, String o2
) {
int num
= new Integer(o1
.length()).compareTo(new Integer(o2
.length()));
if (num
== 0) {
return o1
.compareTo(o2
);
}
return num
;
}
}
class Tool {
private Object obj
;
public void setObject(Object obj
) {
this.obj
= obj
;
}
public Object
getObject() {
return obj
;
}
}
class Utils<Q> {
private Q q
;
public void setObject(Q q
) {
this.q
= q
;
}
public Q
getObject() {
return q
;
}
}
class Worker{
}
class Student1{
}
public class GenericDemo3 {
public static void main(String
[] args
) {
Utils
<Student1> u1
= new Utils<Student1>();
u1
.setObject(new Student1());
System
.out
.println(u1
.getObject());
Utils
<Worker> u2
= new Utils<Worker>();
u2
.setObject(new Worker());
System
.out
.println(u2
.getObject());
}
}
class Demo<T> {
public void show(T t
) {
System
.out
.println("show: " + t
);
}
public <Q> void print(Q q
) {
System
.out
.println("print: " + q
);
}
public static <W> void method(W w
) {
System
.out
.println("method: " + w
);
}
}
public class GenericDemo4 {
public static void main(String
[] args
) {
Demo
<String> d
= new Demo<String>();
d
.show("hahaha");
d
.print("xixi");
d
.print(123);
Demo
.method("lalala");
Demo
.method(456);
}
}
interface Inter<T>{
void show(T t
);
}
class InterImpl<T> implements Inter<T>{
public void show(T t
){
System
.out
.println("show: "+t
);
}
}
public class GenericDemo5 {
public static void main(String
[] args
) {
InterImpl
<String> iis
= new InterImpl<String>();
iis
.show("abcd");
InterImpl
<Integer> iii
= new InterImpl<Integer>();
iii
.show(123);
}
}
import java
.util
.*
;
class Person {
private String name
;
Person(String name
) {
this.name
= name
;
}
public String
getName() {
return name
;
}
}
class Student3 extends Person {
Student3(String name
) {
super(name
);
}
}
public class GenericDemo6 {
public static void main(String
[] args
) {
ArrayList
<Person> al
= new ArrayList<Person>();
al
.add(new Person("张三"));
al
.add(new Person("张三"));
al
.add(new Person("李四"));
al
.add(new Person("王五"));
printColl(al
);
ArrayList
<Student3> al1
= new ArrayList<Student3>();
al1
.add(new Student3("张三1"));
al1
.add(new Student3("张三1"));
al1
.add(new Student3("李四1"));
al1
.add(new Student3("王五1"));
printColl(al1
);
}
public static void printColl(Collection
<? extends Person> al
) {
Iterator
<? extends Person> it
= al
.iterator();
while (it
.hasNext()) {
System
.out
.println(it
.next().getName());
}
}
}
import java
.util
.Comparator
;
import java
.util
.Iterator
;
import java
.util
.TreeSet
;
class Person01 {
private int age
;
private String name
;
Person01(String name
, int age
) {
this.name
= name
;
this.age
= age
;
}
public String
getName() {
return name
;
}
public int getAge() {
return age
;
}
}
class Student01 extends Person01 {
Student01(String name
, int age
) {
super(name
, age
);
}
}
class Worker01 extends Person01 {
Worker01(String name
, int age
) {
super(name
, age
);
}
}
class MyComp implements Comparator<Person01> {
public int compare(Person01 p1
, Person01 p2
) {
int num
= ((Integer
) p1
.getAge()).compareTo((Integer
) p2
.getAge());
if (num
== 0) {
return p1
.getName().compareTo(p2
.getName());
}
return num
;
}
}
class GenericDemo7 {
public static void main(String
[] args
) {
TreeSet
<Person01> ts1
= new TreeSet<Person01>(new MyComp());
ts1
.add(new Person01("张三", 10));
ts1
.add(new Person01("张三", 10));
ts1
.add(new Person01("李四", 10));
ts1
.add(new Person01("王五", 11));
show(ts1
);
TreeSet
<Student01> ts2
= new TreeSet<Student01>(new MyComp());
ts2
.add(new Student01("张三", 10));
ts2
.add(new Student01("张三", 10));
ts2
.add(new Student01("李四", 10));
ts2
.add(new Student01("王五", 11));
show(ts2
);
TreeSet
<Worker01> ts3
= new TreeSet<Worker01>(new MyComp());
ts3
.add(new Worker01("张三", 10));
ts3
.add(new Worker01("张三", 10));
ts3
.add(new Worker01("李四", 10));
ts3
.add(new Worker01("王五", 11));
show(ts3
);
}
public static void show(TreeSet
<? extends Person01> ts
) {
Iterator
<? extends Person01> it
= ts
.iterator();
while (it
.hasNext()) {
Person01 p
= (Person01
) it
.next();
System
.out
.println(p
.getAge() + "......" + p
.getName());
}
System
.out
.println();
}
}