Java泛型01--任意数组中两元素交换

时间:2014-12-17 20:24:58   收藏:0   阅读:190
package com.zl.generic;

/**
 * 交换“任意”数组 中两个元素
 */
public class GenericSwapArray {

	public static void main(String[] args) {
		swap(new String[]{"1","2","3"},1,2);
	}

	public static <T> T[] swap(T[] t,int i,int j) {
		System.out.println("未变**:"+t[i]+"位置:"+i+"__"+t[j]+"位置:"+j);
		T tmp=t[i];
		t[i]=t[j];
		t[j]=tmp;
		System.out.println("改变**:"+t[i]+"位置:"+i+"__"+t[j]+"位置:"+j);
		return t;
	}
}

  

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!