黑马程序员技术交流社区

标题: [已解决]泛型中的置换问题、、、 [打印本页]

作者: 王自强    时间: 2012-9-2 22:38
标题: [已解决]泛型中的置换问题、、、
本帖最后由 王自强 于 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;
}
}
小问题 求解下
作者: 袁艳超    时间: 2012-9-2 22:57
泛型的类型参数只能是类类型(包括自定义类),不能是简单类型
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;
        }
}
作者: 王金科    时间: 2012-9-2 23:04
  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[]
作者: 杨习平    时间: 2012-9-2 23:12
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;
        }
}
作者: 王陶成    时间: 2012-9-2 23:17
java 中基本数据类型不能使用泛型。只有引用类型才能作为泛型方法的实际参数
作者: 王自强    时间: 2012-9-2 23:23
杨习平 发表于 2012-9-2 23:12
1:泛型
        (1)不确定的类型,广泛的类型。
        (2)基本格式: 注意了:数据类型必须是引用数据类型

哦  详细 ,谢谢
作者: 武庆东    时间: 2012-9-2 23:56
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;
}
}




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2