One - One Code All

Blog Content

java实现List集合中获取上一个下一个元素

Java   2014-11-13 20:53:12
public static void main(String[] args) {
		List list = new ArrayList();
		list.add(1);
		list.add(2);
		list.add(3);
		list.add(4);
		System.out.println(list);
		int one = 0;
		int two = 0;
		for (int i = 0; i < list.size(); i++) {
			int n = list.get(i);
			if(5 == n){
				if(i-1>=0){
					one = list.get(i-1);
				}
				if(list.size() > i+1){
					two = list.get(i+1);
				}
			}
		}
		System.out.println("上一个:"+one);
		System.out.println("下一个:"+two);
	}



上一篇:python的pandas中dataframe索引获取get_loc的用法
下一篇:张三丰祖师注解吕祖百字铭

The minute you think of giving up, think of the reason why you held on so long.