1、
class Operate<T> { private T[] arr = null; Operate(T[] arr) { this.arr = arr; } public T[] getArr() { return arr; } public void setArr(T[] arr) { this.arr = arr; } /* * 获取数组长度 */ public int size() { if(arr == null || arr.length == 0) { throw new RuntimeException("数组为空或者数组长度为0"); } return arr.length; } /* * 获取指定位置的值 */ public T getValue(int index) { if(arr == null || index < 0 || arr.length == 0) { throw new RuntimeException("数组为空,或数组长度为0,或索引小于0"); } return arr[index]; } } public class OperateDemo { public static void main(String[] args) { String[] str = {"abc", "zaq", "xsw", "cde"}; Integer[] in = {1, 2, 3, 4, 5}; Operate<String> o1 = new Operate<String>(str); Operate<Integer> o2 = new Operate<Integer>(in); System.out.println(o1.getValue(2)); System.out.println(o2.getValue(1)); print(o1.getArr()); print(o2.getArr()); } public static <T> void print(T[] t) { for(int i = 0; i < t.length; i++) { System.out.println(t[i]); } } }