java 细节 split List 中的equal

java String.split("\\.");

用String.split(".“);不行 必须要进行转码



List 中的equal。

api中是这样描述的:

  • equals

    boolean equals(Object o)
    Compares the specified object with this list for equality. Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elements e1 and e2 are equal if (e1==null ? e2==null : e1.equals(e2)).) In other words, two lists are defined to be equal if they contain the same elements in the same order. This definition ensures that the equals method works properly across different implementations of the List interface.
list中是先判断长度是否一致,然后一个一个地判断是否相等,在想等中,是调用equal来判断的。如下面两处代码,第一处是返回true。第二处是返回false。
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		List<Test1> l1 = new ArrayList<Test1>();
		l1.add(new Test1());
		
		List<Test1> l2 = new ArrayList<Test1>();
		l2.add(new Test1());
//		l2.add("a");
		System.out.println(l1.equals(l2));
	}
	
	static class  Test1{
		int t;

		@Override
		public boolean equals(Object obj) {
			// TODO Auto-generated method stub
			return true;
		}
		
	}


/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		List<Test1> l1 = new ArrayList<Test1>();
		l1.add(new Test1());
		
		List<Test1> l2 = new ArrayList<Test1>();
		l2.add(new Test1());
//		l2.add("a");
		System.out.println(l1.equals(l2));
	}
	
	static class  Test1{
		int t;

//		@Override
//		public boolean equals(Object obj) {
//			// TODO Auto-generated method stub
//			return true;
//		}
		
	}

java 初始化导致的问题。

先看下面一段代码,能够看出问题么?

class BusinessHomeTopNews extends UIHelp{
	private static final String TAG = "businessHome";
	public static final String JUMP_TYPE ="jump_type";
	public BusinessHomeTopNews(Activity activity){
		super(activity);   if(adapter ==null){
			System.out.println("BusinessHomeTopNews adapter is null");   //很莫名其妙,此处为空值
		}
	}
	
	private TopNewsPagerAdapter adapter =null;
	private List<TopNewsInfo> listTop= new ArrayList<TopNewsInfo>();
	private TopNewsViewPagerControl pagerControl;
	public void init(){
		
		ViewPager pager = (ViewPager)activity.findViewById(R.id.main_pager2);
		LinearLayout linearPosition = (LinearLayout)activity.findViewById(R.id.position_linear2);
		
		pagerControl = new TopNewsViewPagerControl(activity, pager, linearPosition);
		
		System.out.println(listTop.size());	//此处
		adapter = new TopNewsPagerAdapter(listTop, activity);
		pagerControl.setAdapter(adapter);
	}
}
public abstract class UIHelp {
	protected Activity activity;
	public UIHelp(Activity activity){
		this.activity = activity;
		init();
	}
	
	public abstract void init();
}
在打印listTop.size() 的时候会爆空指针错误。为什么呢?

且看初始化顺序:UIHelp()  ->init() (调用子类的init'())   -> 然后对BusinessHomeTopNews 的子成员进行初始化(执行listTop = new ArrayList<TopNewsInfo>() -> 调用BusinessHomeTopNews 的构造函数.

看看这顺序就知道为什么会爆空指针错误了。


你可能感兴趣的:(java 细节 split List 中的equal)