final关键字与数据类型转换
判断单链表是否有环
public boolean hasCycle(ListNode head) {
ListNode quick = head;
ListNode slow = head;
while(quick!=null&&quick.next!=null){
quick = quick.next.next;
slow = slow.next;
if(quick==slow){
return true;
}
}
return false;
}
链式栈插入元素
广义表头尾相同
抽象类编写格式
单例模式
无效的变量操作
多态机制
class Base {
public static void main(String[] args) {
Base base = new Son();
base.method();
}
public void method() {
System.out.println("Base");
}
}
class Son extends Base {
public void method() {
System.out.println("Son");
}
public void methodB() {
System.out.println("SonB");
}
}
多线程同步锁
class NameList {
private List names = new ArrayList();
synchronized void add(String name) {
names.add(name);
}
public static void main(String[] args){
final NameList sl = new NameList();
for (int i = 0; i < 2; i++) {
new Thread(){
public void run() {
sl.add("A");
sl.add("B");
sl.add("C");
sl.printAll();
}
}
}.start();
}
synchronized void printAll() {
for (int i = 0; i < names.size(); i++) {
System.out.print(names.get(i) + "");
}
}
}
程序设计题:自定义注解Id,要求见代码注释
package du;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Id {}
package du;
public class IdPropertyNotFoundException extends RuntimeException{
public IdPropertyNotFoundException(){}
public IdPropertyNotFoundException(String string){
super(string);
}
}
package du;
import java.lang.reflect.Field;
@Id
public class d844 {
String information;
String methods;
public static void main(String[] args) throws ClassNotFoundException{
Class<?> aClass = Class.forName("du.d844");
if (aClass.isAnnotationPresent(Id.class)){
Field[] declaredFields = aClass.getDeclaredFields();
boolean haveIntId = false;
for (Field declaredField : declaredFields) {
if ("id".equals(declaredField.getName()) && "int".equals(declaredField.getType().getSimpleName())){
haveIntId = true;
break;
}
}
if (!haveIntId){
throw new IdPropertyNotFoundException("Annotation @Id must have int type property id");
}
}
}
}
|