A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 王自强 中级黑马   /  2012-9-2 22:38  /  1640 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 王自强 于 2012-9-2 23:23 编辑

class GenericDemo
{
public static void main(String[] args)
{
  int []arr = {1,2,3,4};
  demo(arr,0,3);//这个错哪了、、、
  for(int i : arr)
  {
   System.out.println(i);
  }
}
public static <T> void demo(T[] arr,int x,int y)
{
  T temp  = arr[x];
  arr[x] = arr[y];
  arr[y] = temp;
}
}
小问题 求解下

评分

参与人数 1技术分 +1 收起 理由
唐志兵 + 1 赞一个!

查看全部评分

6 个回复

倒序浏览
泛型的类型参数只能是类类型(包括自定义类),不能是简单类型
class GenericDemo {
        public static void main(String[] args) {
                Integer[] arr = { 1, 2, 3, 4 };
                demo(arr, 0, 3);// 这个错哪了、、、
                for (int i : arr) {
                        System.out.println(i);
                }
        }

        public static <T> void demo(T[] arr, int x, int y) {
                T temp = arr[x];
                arr[x] = arr[y];
                arr[y] = temp;
        }
}

评分

参与人数 1技术分 +1 收起 理由
唐志兵 + 1

查看全部评分

回复 使用道具 举报
  1. public class GenericDemo{
  2.         public static void main(String[] args){
  3.                  Integer []arr = {1,2,3,4};
  4.                    demo(arr,0,3);//这个错哪了、、、
  5.                    for(int i : arr)
  6.                    {
  7.                     System.out.println(i);
  8.                    }
  9.         }
  10.        
  11.         public static <T> void demo(T[] arr,int x,int y)
  12.     {
  13.       T temp  = arr[x];
  14.       arr[x] = arr[y];
  15.       arr[y] = temp;
  16.     }
  17.        
  18. }
复制代码
应该是这样子的,int []是Object对象,int [][]才是Object[]
回复 使用道具 举报
1:泛型
        (1)不确定的类型,广泛的类型。
        (2)基本格式:<数据类型> 注意了:数据类型必须是引用数据类型
               基本数据类型,包括数值型,字符型和布尔型。
            引用数据类型:类、接口类型、数组类型、枚举类型、注解类型;
        (3)泛型的定义方式
                A:泛型定义在类上
                        class Demo<QQ>
                        {
                                public void show(QQ qq){}
                        }

                B:泛型定义在方法上
                        class Demo
                        {
                                public <BM> void show(BM bm){}
                        }

                C:泛型定义在接口上
                        interface Inter<QQ>
                        {
                                public abstrace void show(QQ qq);
                        }

        (4)泛型的使用
                A:一般,泛型就用于集合类中。
               
                B:好处:
                        **把运行过程中可能出现的ClassCastException转到了编译期。
                        **避免了多态的安全隐患

class GenericDemo {
        public static void main(String[] args) {
                Integer[] arr = { 1, 2, 3, 4 };//所以int 应该改成Integer类型
                demo(arr, 0, 3);// 这个错哪了、、、
                for (int i : arr) {
                        System.out.println(i);
                }
        }

        public static <T> void demo(T[] arr, int x, int y) {
                T temp = arr[x];
                arr[x] = arr[y];
                arr[y] = temp;
        }
}
回复 使用道具 举报
java 中基本数据类型不能使用泛型。只有引用类型才能作为泛型方法的实际参数
回复 使用道具 举报
杨习平 发表于 2012-9-2 23:12
1:泛型
        (1)不确定的类型,广泛的类型。
        (2)基本格式: 注意了:数据类型必须是引用数据类型

哦  详细 ,谢谢
回复 使用道具 举报
class GenericDemo
{
public static void main(String[] args)
{
  int []arr = {1,2,3,4};
  demo(arr,0,3);//arr不是引用类型的变量,不能使用泛型,可以讲int改为Integer
  for(int i : arr)
  {
   System.out.println(i);
  }
}
public static <T> void demo(T[] arr,int x,int y)
{
  T temp  = arr[x];
  arr[x] = arr[y];
  arr[y] = temp;
}
}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马