——Java培训、Android培训、iOS培训、.Net培训、期待与您交流! ——-
泛型:
用于解决安全问题。是一个类型安全机制。
在集合定义时指定集合内存储的数据类型。
eg:ArrayList<String> al=new ArrayList<String>();
好处:
1.将运行时期出现的文题ClassCastException,转移到了编译时期,方便于程序员解决问题。让运行时期问题减少,提高安全性。
2.避免了强制转换的麻烦
泛型格式:通过<>来定义要操作的引用数据类型。
在使用java提供的对象时,什么时候写泛型呢?
通常在集合框架中很常见,只要见到<>就要定义泛型。其实<>就是用来接收泛型的。
当使用集合时,将集合中要存储的数据类型作为参数传递到<>中即可
//泛型在比较器中的使用,可以发现加入泛型之后程序避免了强制转换,提高了安全性。
class LenComparator implements Comparator<String>
{
/*
public int compare(Object o1,Object o2)
{
String s1=(String)o1;
String s2=(String)o2;
if(s1.length()==s2.length())
return s1.compareTo(s2);
return new Integer(s1.length()).compareTo(new Integer(s2.length()));
}
*/
public int compare(String s1,String s2)
{
if(s1.length()==s2.length())
return s1.compareTo(s2);
return new Integer(s1.length()).compareTo(new Integer(s2.length()));
}
}
泛型类
什么时候定义泛型类?
当类中要操作的引用数据类型不确定的时候,早期定义Object来完成扩展,现在定义泛型扩展。
class Utils<Q>
{
private Q q;
public void set(Q q)
{
this.q=q;
}
public Q get()
{
return q;
}
}
泛型类定义的泛型,在整个类中有效。如果被方法使用,那么泛型类的对象明确要操作具体的类型后,所要操作的类型就已经固定了。为了让不同的方法操作不同类型,而且类型还不确定,那么可以将泛型定义在方法上。
public <T> void show(T t)
{
System.out.println("Show:"+t);
}
特殊:静态方法不可以访问类上定义的泛型。
如果静态方法操作的应用数据类型不确定,可以将泛型定义在方法上
public static <W> void method(W w)
{
System.out.println("method:"+w);
}
//其中<W>写在修饰符和返回值类型之
泛型接口
Collection就是泛型接口
import java.util.*;
interface Inter<T>
{
void show(T t);
}
class InterImpl<T> implements Inter<T>
{
public void show(T t)
{
System.out.println("show:"+t);
}
}
class GenericDemo4
{
public static void main(String[] args)
{
InterImpl<Integer> i=new InterImpl<Integer>();
i.show(4);
}
}
泛型的高级应用
通配符 ?相当于 ?extends Object 为函数明确类型,但是类型不确定,使用后不能使用对象的特有方法
向上限定? extends E 能接收E 和E的子类型
向下限定? super E 能接收E和E的父类型
向上限定简单应用:
import java.util.* ;
class Person
{
private String name;
Person(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
}
class Student extends Person
{
private String name;
Student(String name)
{
super(name);
}
}
class GenericDemo5
{
public static void main(String[] args)
{
ArrayList<Person> al=new ArrayList<Person>();
al.add(new Person("abc1"));
al.add(new Person("abc2"));
al.add(new Person("abc3"));
ArrayList<Student> al1=new ArrayList<Student>();
al1.add(new Student("fdjkgfd----1"));
al1.add(new Student("fdg----21"));
al1.add(new Student("fghnb----3"));
printColl(al);
printColl(al1);
}
public static void printColl(Collection<? extends Person> al)
{
Iterator<? extends Person> it=al.iterator();
while(it.hasNext())
{
System.out.println(it.next().getName());
}
}
/*
ArrayList<String> al=new ArrayList<String>();
al.add("abc1");
al.add("abc2");
al.add("abc3");
ArrayList<Integer> al1=new ArrayList<Integer>();
al1.add(1);
al1.add(4);
al1.add(7);
printColl(al);
printColl(al1);
}
//当对象类型不确定时,可以用通配符表示,但是使用后不能使用对象的特有方法
//明确具体类型则可以public static<T> void printColl(Collection<T> al)使用这种方式可以接收和操作对象。
public static void printColl(Collection<?> al)
{
Iterator<?> it=al.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
*/
}
增强for循环:
增强for循环
格式:
for(数据类型 变量名: 被遍历的集合(Collection)或数组)
{
}
对集合进行遍历,只能获取元素但是不能对集合进行操作,如修改。
迭代器除了遍历,还可以进行remove集合中元素的动作。
如果使用ListIterator,还可以在遍历过程中进行增删改查动作。
传统for循环和高级for有什么区别呢?
高级for有一个局限性。必须有被遍历的目标。
eg:将haha打印100次,高级for做不了。
建议在遍历数组的时候,还是希望使用传统for,因为传统for可以定义角标。
自动拆装箱: 简化书写方向的升级
class IntegerDemo
{
public static void main(String[] args)
{
Integer x=4;//自动装箱
x =x/*x.intValue()*/+2;//自动拆箱 x进行了自动拆箱 变成了int类型和2进行加法运算
//再将和进行装箱付给x
//Integer y=4;
System.out.println(x);
// System.out.println(x==y);
Integer m=128;
Integer n=128;
System.out.println(m==n);//false
Integer a=127;
Integer b=127;
System.out.println(a==b);//true 因为a和b指向了同一个Integer对象
//★★★因为数值范围在byte内,对于新特性,如果该值已经存在,不会开辟新的空间
}
}
StaticImport 静态导入:
导入某个类中的所有静态成员。
eg:import static java.util.Arrays.*;
导入的是Arrays这个类中的所有静态成员。
使用时注意:
当类名重名时,需要指定具体的包名。
当方法重名时,需要指定所属的对象或类。
import static java.util.Arrays.*;
import java.util.*;
import static java.lang.System.*;//导入了System类中的所有静态成员
class StaticImport
{
public static void main(String[] args)
{
int[] arr={3,1,5};
sort(arr);
int index=binarySearch(arr,1);
out.println(Arrays.toString(arr));
out.println(index);
}
可变参数:…
其实就是数组参数的简写形式。不用每一次都手动建立数组对象。
只要将要操作的元素作为参数传递即可。隐式将这些参数封装成数组
方法的可变参数在使用时应注意:
1.可变参数一定要定义在参数列表的最后面。
2.不能与数组参数重载
即:
void show(int[] arr){}
void show(int... arr){}
不能出现在同一个类中