- 二个list取交集用法.
Array<String> A = new ArrayList<String>();
A.add("a");
A.add("b");
A.add("c");
Array<String> B = new ArrayList<String>();
A.add("a");
A.add("d");
A.add("e");
Boolean flag = false;
Try{
A.retainAll(B);//可以返回状态
}catch(Exception e ){
xxxxxx
}
返回为A对象 表示A与B的交集,返回为B对象 表示B 所以要主意。
2.产生随机数字
一、产生6位随机数字
double randNum = Math.random();
// multiply by 100,000 to usually make a 5 digit number
String password = String.valueOf((int) (randNum * 1000000));
if (password.length() < 6) {
int n = 6 - password.length();
if (n == 1) {
password = password + "0";
} else {
if (n == 2) {
password = password + "00";
} else {
if (n == 3) {
password = password + "000";
} else {
if (n == 4) {
password = password + "0000";
} else {
if (n == 5) {
password = password + "00000";
}
}
}
}
}//此方法需要优化
3.异常类的处理
在处理异常的原则,通常可以将异常分为2中,一种是业务异常,一种是非业务异常。
一、业务异常是指在进行正常的页面处理中,由于某些页面的特殊要求,我们预知的捕获异常。
二、非业务异常是指网络 或者配置文件的原因。
一、需要定义error code
二、此类需要继承Exception
例如 项目中系统的异常类:
public class SsoException extends Exception
{
private static final long serialVersionUID = 18200001L;
private String _errcode;
// for completeness, no point to use this constructor
/**
* Constructs a new SsoException.
*/
public SsoException()
{
super();
}
/**
* Constructs a new SsoException.
*
* @param errcode error code
*/
public SsoException( String errcode )
{
super();
_errcode = errcode;
}
/**
* Constructs a new SsoException.
*
* @param errcode error code
* @param s description
*/
public SsoException( String errcode, String s )
{
super( s );
_errcode = errcode;
}
/**
* Constructs a new SsoException.
*
* @param errcode error code
* @param t cause
*/
public SsoException( String errcode, Throwable t )
{
super( "" + t );
try
// if running on java 1.4 or higher, call initCause
{
Throwable.class.getMethod( "initCause",
new Class[] { Throwable.class } ).invoke( this,
new Object[] { t } );
}
catch ( Exception e ) // running on java 1.3
{
}
_errcode = errcode;
}
/**
* Constructs a new SsoException.
*
* @param errcode error code
* @param s description
* @param t cause
*/
public SsoException( String errcode, String s, Throwable t )
{
super( ( t != null && s != null && s.endsWith( t.toString() ) ) ? s : s
+ ":" + t );
try
// if running on java 1.4 or higher, call initCause
{
Throwable.class.getMethod( "initCause",
new Class[] { Throwable.class } ).invoke( this,
new Object[] { t } );
}
catch ( Exception e ) // running on java 1.3
{
}
_errcode = errcode;
}
/**
* Returns the error code
*
* @return String Error Code
*/
public String getErrorCode()
{
return _errcode;
}
/**
* Returns the error code description.
*
* @return String Description
*/
public String toString()
{
return super.toString() + " [ErrorCode: "
+ Info.explainEnum( _errcode, getClass() ) + "]";
}
/**
* Explain number
*
* @param value
* Object value
*
* @param clazz
* Class extends exception
*
* @return List return list
*/
public static String explainEnum(Object value,
Class<? extends Exception> clazz) {
List<String> list = decodeEnum(value, clazz);
if (list.isEmpty())
return (new StringBuilder()).append(value).append("").toString();
else
return (new StringBuilder()).append(value)
.append(" ")
.append(list)
.toString();
}
/**
* Decode number
*
* @param value
* Object value
*
* @param clazz
* Class extends exception
*
* @return List return list
*/
public static List<String> decodeEnum(Object value,
Class<? extends Exception> clazz) {
List<String> list = new ArrayList<String>();
try {
Field f[] = clazz.getFields();
for (int i = 0; i < f.length; i++)
try {
if (Modifier.isFinal(f[i].getModifiers())
&& value.equals(f[i].get(null))) {
list.add(f[i].getName());
}
} catch (Exception e) {
}
} catch (Exception e) {
}
return list;
}
/**
* Decode number
*
* @param value
* int value
*
* @param clazz
* Class extends exception
*
* @return List return list
*/
public List<String> decodeEnum(int value, Class<? extends Exception> clazz) {
return decodeEnum(new Integer(value), clazz);
}
}
当 项目中具体的异常类,可以继承系统的异常类(SsoException)。
Public Class ManagerException extends SsoException {
/**
* Constructs a new ManagerException.
*
* @param errorcode
* String
*/
public ManagerException(String errorcode) {
super(errorcode);
}
/**
* Constructs a new ManagerException.
*
* @param errorcode
* String
* @param message
* String
*/
public ManagerException(String errorcode, String message) {
super(errorcode, message);
}
/**
* Constructs a new ManagerException.
*
* @param errorcode
* String
* @param message
* String
* @param t
* Throwable
*/
public ManagerException(String errorcode, String message, Throwable t) {
super(errorcode, message, t);
}
/**
* Constructs a new ManagerException.
*
* @param errorcode
* String
* @param t
* Throwable
*/
public ManagerException(String errorcode, Throwable t) {
super(errorcode, t);
}
}
4.java.lang.Class类解析和反射机制
4.1获得class类的实例方法
例如:
A. Student stu = new Student();
Class clazz1 = stu.getClass();
System.out.println(clazz1.getName() + "1");
B . Class clazz2 = null;
try {
clazz2 = Class.forName("com.ufinity.atest.Student");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
System.out.println(clazz2.getName() + "2");
Class clazz3 = Student.class;
System.out.println(clazz3.getName() + "3");
C. Class clazz4 = int.class;
System.out.println(clazz4.getName() + "4");
Class clazz5 = Integer.TYPE;
System.out.println(clazz5.getModifiers()+ "5");
4.2 Class 类 newInstance()方法的应用
主意:此方法调用类缺省的构造方法(不带参数的构造方法),如果一个类中已经定义一个带参数的构造方法,那么此方法就会抛出异常。
为了解决这个问题,可以用反射reflect.
try {
Object obj = Class.forName("com.ufinity.atest.Student").newInstance();//Student student = (Student)obj;
System.out.println(student.getId());
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
4.3.java.lang.reflect.Constructor的应用.得到一个类的构造方法的信息。
例如:Constructor[] con = clazz.getDeclaredConstructors();//返回所有带修饰符的构造方法
Constructor[] con1 = clazz.getConstructors();//返回public 修饰符的构造方法
Object o = con[0].newInstance(paramValues);//向构造方法传递参数的返回类的实体
4.4 java.lang.reflect.Method 得到一个方法的信息。
例如: Method[] method = clazz.getDeclaredMethods();
//返回 Method 对象的一个数组,这些对象反映此 Class 对象表示的类或接口声明的所有方法,包括公共、保护、默认(包)访问和私有方法,
Method[] method1 = clazz.getMethods();
//返回该类public 修饰符的方法以及从超类和超接口继承的那些的类或接口
method[0].invoke(o, "1");//方法被调用,传递了类的实例 和 被调用方法的参数
4.5 java.lang.reflect 类 Field 提供类单个字段的信息
作用:可以得到一个类中字段与对应数值的集合 类似Tostring方法。
例如:
source 为一个类的实体
Field[] f = source.getClass().getFields();
f.get(object);//返回指定对象上此 Field 表示的字段的值。
f.getName();//以 String 的形式返回此 Class 对象所表示的实体(类、接口、数组类、基本类型或 void)名称
在设计的过程中,可以将字段对应的key和value 放在一个map的数组里。
如: map.put(f[i].getName(), f[i].get(source));
4.6 java.lang.reflect.Modifier;
对类和成员修饰符进行解码,判决修饰符的类型。
4.7 char字符小常识
字符的位长:16bit ;2个字节
字符常量表现形式为三种:
a)单引号引起的单个字符的表现
b)以10进制或者16进制表现 ' /101'(可以表示所有的unicode字符)
c)以特殊的字符表现 如:'/t ','/n'
例子:char c = '遍';//占用2个字节(byte) 16位(bit)
String s = "遍"; //如何utf8编码,是三个字节,24个位。。。。。。
java是用unicode来表示字符,"编"这个中文字符的unicode就是2个字节。String.getBytes(encoding)方法是获取指定编码的byte数组表示,通常gbk/gb2312是2个字节,utf-8是3个字节。如果不指定encoding则取系统默认的encoding
5.java正则表达式 取字符集元素
String str = "【电子客票可以在互联网进行改签、退票么?】s【电子客票可以在互联网进行改签、退票么?】s";
String reg = "【[^】]+】";
// String str = "[电子客票可以在互联网进行改签、退票么?]s[电子客票可以在互联网进行改签、退票么?]s";
//String reg = "\\[[^\\]]+\\]";