1、使用Arrays.sort()方法对数组进行排序;
int[] nums={44,22,11,0};
Arrays.sort(nums);
System.out.println(Arrays.toString(nums));//[0, 11, 22, 44]
2、内部类、局部内部类、匿名内部类,静态内部类,代码如下:
public class test01 {
public static void main(String[] args) {
TalkingClock talkingClock = new TalkingClock(1000, true);
talkingClock.start();
JOptionPane.showMessageDialog(null, "终止执行?");
System.exit(0);
}
}
class TalkingClock {
private int interval;
private boolean beep;
public TalkingClock(int interval, boolean beep) {
this.interval = interval;
this.beep = beep;
}
public void start() {
ActionListener al = new TimePrinter();
Timer timer = new Timer(interval, al);
timer.start();
}
//内部类
public class TimePrinter implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
Date date = new Date();
System.out.println("Time is :" + date);
if (TalkingClock.this.beep) Toolkit.getDefaultToolkit().beep();
}
}
}
如上图:理解什么是内部类?就是定义在一个类里面的类;
public class test01 {
public static void main(String[] args) {
TalkingClock talkingClock = new TalkingClock(1000, true);
talkingClock.start();
JOptionPane.showMessageDialog(null, "终止执行?");
System.exit(0);
}
}
class TalkingClock {
private int interval;
private boolean beep;
public TalkingClock(int interval, boolean beep) {
this.interval = interval;
this.beep = beep;
}
public void start() {
//局部内部类
class TimePrinter implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
Date date = new Date();
System.out.println("Time is :" + date);
if (TalkingClock.this.beep) Toolkit.getDefaultToolkit().beep();
}
}
ActionListener al = new TimePrinter();
Timer timer = new Timer(interval, al);
timer.start();
}
}
如上图:什么是局部内部类? 就是定义在方法里面的内部类。
public class test01 {
public static void main(String[] args) {
TalkingClock talkingClock = new TalkingClock(1000, true);
talkingClock.start();
JOptionPane.showMessageDialog(null, "终止执行?");
System.exit(0);
}
}
class TalkingClock {
private int interval;
private boolean beep;
public TalkingClock(int interval, boolean beep) {
this.interval = interval;
this.beep = beep;
}
/*
public void start() {
//局部内部类
class TimePrinter implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
Date date = new Date();
System.out.println("Time is :" + date);
if (TalkingClock.this.beep) Toolkit.getDefaultToolkit().beep();
}
}
ActionListener al = new TimePrinter();
Timer timer = new Timer(interval, al);
timer.start();
}
*/
public void start() {
//匿名内部类
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Date date = new Date();
System.out.println("Time is :" + date);
if (TalkingClock.this.beep) Toolkit.getDefaultToolkit().beep();
}
};
Timer timer = new Timer(interval, actionListener);
timer.start();
}
}
如上图,所谓匿名内部类,可以理解成是对局部内部类的进一步的深入,至于定义,我也暂未想出这么个写法的定义。
3、StopWatch的简单使用:
public static void main(String[] args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
try {
sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
stopWatch.stop();
System.out.println(stopWatch.getTotalTimeSeconds()); //2
}
详解:
应用场景:计算部分程序代码块运行的时间;
注意:它在stopWatch.stop()执行前,stopWatch.start()不可执行多次,否则会报错。
4、Java类中 静态代码块和构造器的执行顺序
public class test01 {
public static void main(String[] args) {
Dog dog = new Dog();
Dog dog1 = new Dog();
}
}
class Animal{
static {
System.out.println("动物的静态代码块!");
}
public Animal() {
System.out.println("动物的构造器");
}
}
class Dog extends Animal{
static{
System.out.println("狗的静态代码块!!!!!");
}
public Dog() {
System.out.println("狗的构造器");
}
}
运行结果:如下
动物的静态代码块!
狗的静态代码块!!!!!
动物的构造器
狗的构造器
动物的构造器
狗的构造器
如上图:执行顺序为: 父类静态代码块→子类静态代码块→父类构造→子类构造。且静态代码块只执行一次。
5、String类中startWith()方法的简单使用
public static void main(String[] args) {
String str="123456";
boolean b = str.startsWith("123");
System.out.println(b);//true
}
6、理解static、final、static final的含义
7、为什么hashmap中常用字符串类型作为key?
个人简单理解: 第一,hashmap是根据k的hashcode值来查找对应的v,而String类型的hashcode就是通过字符串的内容来计算hashcode的,也就是说,只要字符串内容一致,那么其hashcode就一致。 第二,String类型的对象,每当创建时,其hashcode都会被缓存下来,当使用其作为k时,则无需再次计算其hashcode,提升效率;
为什么hashmap的key不使用对象而是使用字符串_map.get(key),key必须为string吗_那本来就是我的光的博客-CSDN博客