1、手写ArrayList
package com.interview;
import java.util.Arrays;
public class OwnListAchieve
{
/**
* 数组
*/
private Object[] elements;
/**
* 数组长度
*/
private int size = 0;
/**
* 初始化数组长度
*/
private final static int init_length = 10;
/**
* 数组最大长度
*/
private int max_length = Integer.MAX_VALUE;
/**
* 无参的构造函数,默认调用有参的构造函数
*/
public OwnListAchieve() {
this(init_length);
}
/**
* 有参的构造函数
*
* @param size
*/
public OwnListAchieve(int size) {
if (size < 0) {
System.out.println("创建集合时,长度务必大于等于0!!!");
} else {
elements = new Object[size];
}
}
/**
* 向集合里面添加元素
*
* @param e
*/
public void add(E e) {
arrayCapacityIsSufficient(size + 1);
elements[size++] = e;
}
/**
* 获取集合中的元素
*
* @param index
* @return
*/
public E get(int index) {
checkRange(index);
return (E) elements[index];
}
/**
* 移除集合中的元素
*
* @param index
* @return
*/
public E remove(int index) {
checkRange(index);
E value = get(index);
int moveSize = size - index - 1;
if (moveSize > 0) {
System.arraycopy(elements, index + 1, elements, index, moveSize);
}
elements[size--] = null;
return value;
}
/**
* 获取集合的长度
*
* @return
*/
public int size() {
return size;
}
/**
* 检查下标是否越界
*
* @param index
* @throws Exception
*/
public void checkRange(int index) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException("数组下标越界!!!");
}
}
/**
* 判断数组的长度是否能满足再添加该元素
*
* @param i
*/
private void arrayCapacityIsSufficient(int size) {
if (size > init_length) {
dilatation(size);
}
}
/**
* 数组扩容
*/
private void dilatation(int capacity) {
int newLength = init_length * 2;
if (newLength - capacity < 0) {
newLength = capacity;
}
if (newLength > max_length) {
newLength = (capacity > max_length ? Integer.MAX_VALUE : max_length);
}
elements = Arrays.copyOf(elements, newLength);
}
}
2、进制之间的转换
package com.interview;
/**
* 进制之间的转换
*
* @author Administrator
*
*/
public class DigitalConvert {
/**
* 二进制转换为十进制
*
* @param digital
* @return
*/
public int twoToTenConvert(String digital) {
int total = 0;
for (int i = 0; i < digital.length(); i++) {
total += Integer.parseInt(digital.charAt(i) + "") * Math.pow(2, digital.length() - i - 1);
}
return total;
}
/**
* 十进制转换为二进制
*
* @param digital
* @return
*/
public String tenToTwoConvert(int digital) {
StringBuffer buffer = new StringBuffer();
while (digital > 1) {
int i = digital % 2;
digital = digital / 2;
buffer.append(i);
}
if (digital == 1) {
buffer.append(digital);
}
if (buffer.length() % 8 != 0) {
for (int i = 0; i <= 8 - buffer.length(); i++) {
buffer.append(0);
}
}
char[] chars = buffer.toString().toCharArray();
StringBuffer tmp = new StringBuffer();
for (int i = chars.length - 1; i >= 0; i--) {
tmp.append(chars[i]);
}
return tmp.toString();
}
}
3、==与equals的区别
package com.interview;
public class differenceEquals {
public static void main(String[] args) {
String str1 = "123";
String str2 = "123";
System.out.println(str1.equals(str2));// true
System.out.println(str1 == str2);// true
String str3 = new String("123");
String str4 = new String("123");
System.out.println(str3.equals(str4));// true
System.out.println(str3 == str4);// false
String str6 = str3;
String str7 = str3;
System.out.println(str6.equals(str7));// true
System.out.println(str6 == str7);// true
}
}
4、生产者与消费者模式(多线程,同步)
package com.interview.producer_comsumer;
/**
* 面包类
*
* @author Administrator
*
*/
public class Bread {
private int id;
private String name;
public Bread(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.interview.producer_comsumer;
import java.util.ArrayList;
import java.util.List;
/**
* 面包工厂
*
* @author Administrator
*
*/
public class BreadFactory {
/**
* 装面包的链表
*/
private List
breads = new ArrayList
();
/**
* 最大的生产量
*/
private final static int max = 20;
/**
* 面包编号
*/
private int id = 0;
/**
* 生产面包
*
* @throws InterruptedException
*/
public void produce() throws InterruptedException {
synchronized (breads) {
if (breads.size() < max) {
Bread bread = new Bread(id++, "好面包");
breads.add(bread);
System.out.println("生产面包了,盒子里面还有" + breads.size() + "个面包");
breads.notify();
} else {
breads.wait();
}
}
}
/**
* 消费面包
*
* @throws InterruptedException
*/
public void comsumer() throws InterruptedException {
synchronized (breads) {
if (breads.size() > 0) {
breads.remove(0);
System.out.println("消费面包了,盒子里面还剩" + breads.size() + "个面包");
breads.notify();
} else {
breads.wait();
}
}
}
}
package com.interview.producer_comsumer;
/**
* 消费者
*
* @author Administrator
*
*/
public class Comsumer implements Runnable {
private BreadFactory factory;
public Comsumer(BreadFactory factory) {
this.factory = factory;
}
@Override
public void run() {
while (true) {
try {
factory.comsumer();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
package com.interview.producer_comsumer;
/**
* 生产者
*
* @author Administrator
*
*/
public class Producer implements Runnable {
private BreadFactory factory;
public Producer(BreadFactory factory) {
this.factory = factory;
}
@Override
public void run() {
while (true) {
try {
factory.produce();
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
package com.interview.producer_comsumer;
/**
* 测试终端
*
* @author Administrator
*
*/
public class Client {
public static void main(String[] args) {
BreadFactory factory = new BreadFactory();
Producer producer = new Producer(factory);
Comsumer comsumer = new Comsumer(factory);
new Thread(producer).start();
new Thread(comsumer).start();
}
}
package com.interview.producer_comsumer;
/**
* 测试终端
*
* @author Administrator
*
*/
public class Client {
public static void main(String[] args) {
BreadFactory factory = new BreadFactory();
Producer producer = new Producer(factory);
Comsumer comsumer = new Comsumer(factory);
new Thread(producer).start();
new Thread(comsumer).start();
}
}
5、代理
实现跟CEO的谈判,无法直接联系CEO,得找CEO的代理秘书才可以。
5.1、静态代理
package com.interview.static_proxy;
/**
* 首席执行官
*
* @author Administrator
*
*/
public interface CEO {
public void negotiate(float money);
}package com.interview.static_proxy;
/**
* BAT的首席执行官
*
* @author Administrator
*
*/
public class BATCEO implements CEO {
@Override
public void negotiate(float money) {
if (money > 1000000) {
System.out.println("金额大于100万,约一下!!!");
return;
}
System.out.println("金额太小,整段垮掉!!!");
}
}
package com.interview.static_proxy;
/**
* 秘书
*
* @author Administrator
*
*/
public class Secretary implements CEO {
private CEO bat;
public Secretary(CEO bat) {
this.bat = bat;
}
@Override
public void negotiate(float money) {
before();
bat.negotiate(money);
after();
}
private void after() {
System.out.println("谈的怎么样!!!");
}
private void before() {
System.out.println("金额能不能达到要求!!!");
}
}
emptypackage com.interview.static_proxy;
/**
* 小罗罗
*
* @author Administrator
*
*/
public class Client {
public static void main(String[] args) {
CEO ceo = new BATCEO();
Secretary sy = new Secretary(ceo);
sy.negotiate(100000000);
}
}
5.2、java动态代理
package com.interview.dynamic_proxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
* 动态代理
*
* @author Administrator
*
*/
public class DynamicProxy implements InvocationHandler {
private Object proxyObj;
public DynamicProxy(Object proxyObj) {
this.proxyObj = proxyObj;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
before();
Object returnValue = method.invoke(proxyObj, args);
after();
return returnValue;
}
private void after() {
System.out.println("谈的怎么样!!!");
}
private void before() {
System.out.println("金额能不能达到要求!!!");
}
public static Object getProxyInstance(Object obj) {
return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(),
new DynamicProxy(obj));
}
}
package com.interview.dynamic_proxy;
import com.interview.static_proxy.BATCEO;
import com.interview.static_proxy.CEO;
/**
* 小罗罗
*
* @author Administrator
*
*/
public class Client {
public static void main(String[] args) {
CEO bat = (CEO) DynamicProxy.getProxyInstance(new BATCEO());
bat.negotiate(10000000);
}
}
5.3、Cglib动态代理
package com.interview.cglib;
import java.lang.reflect.Method;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
/**
* cglib实现动态代理
*
* @author Administrator
*
*/
public class Cglib implements MethodInterceptor {
private Object proxyObj;
public Cglib(Object proxyObj) {
this.proxyObj = proxyObj;
}
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy arg3) throws Throwable {
before();
Object returnValue = method.invoke(proxyObj, args);
after();
return returnValue;
}
public Object getProxyInstance() {
Enhancer en = new Enhancer();
en.setSuperclass(proxyObj.getClass());
en.setCallback(this);
return en.create();
}
private void after() {
System.out.println("谈的怎么样!!!");
}
private void before() {
System.out.println("金额能不能达到要求!!!");
}
}
package com.interview.cglib;
import com.interview.static_proxy.BATCEO;
import com.interview.static_proxy.CEO;
/**
* 小罗罗
*
* @author Administrator
*
*/
public class Client {
public static void main(String[] args) {
CEO ceo = (CEO) new Cglib(new BATCEO()).getProxyInstance();
ceo.negotiate(10000000);
}
}