System类
- 常用方法----静态方法–通过类名调用
- public static long currentTimeMillis()---->用来测试程序效率(执行前后相减)
- public static void arraycopy(…):数组数据拷贝
StringBuilder类
- 字符串缓冲区,可以提高字符串的操作效率
可看作是一个长度可以变化的字符串 - 和String的比较
- 构造方法:
- StringBuilder()
构造一个没有字符的字符串构建器,初始容量为16个字符。 - StringBuilder(String str)
构造一个初始化为指定字符串内容的字符串构建器
- 常用方法:
- StringBuilder append(…) ----重写了多个,参数可以是各种类型
- StringBuilder reverse()-----将内容反转
- String toString() 返回表示此顺序中的数据的字符串。 --将StringBuilder对象 变为String对象
包装类
概念:
- 装箱拆箱
eg:Integer
- 自动装箱和拆箱
JDK1.5以后
eg:ArrayList—不能为整数int类ing,可以为其包装类
list.add(1)----自动装箱liat.add(new Integer(1))
list.get(1)----自动拆箱list.get()–intValue() - 基本类型与字符串之间的转换:
eg:
- 基本类型转String
eg:34+" " - String 转基本类型
Collection< E >集合(接口)
- 备注:集合重写了toString方法可直接输出,数组需要循环遍历或者使用Arrays的toString方法输出
使用多态创建对象()即new实现类 - 概念以及区别:
- 集合框架
- Collection常用功能
- 常用方法
集合的遍历
两种方式均不能对集合的内部元素进行增伤操作
1.Iterator< E >迭代器(接口)
作用:完成Collection集合的遍历
多态完成创建对象
Collection coll=new 实现类();
Iterator i=coll.iterator();
- 迭代:取之前先判断有无元素(hasNext()方法),有则取(next()方法),继续重复操作。
- 迭代器的使用:
public class Test {
public static void main(String[] args) {
System.out.println("HelloWorld");
//集合
Collection collection=new ArrayList( );
//为集合赋值
for (int i = 0; i < 10; i++) {
collection.add( i +1);
}
//获取迭代器对象
Iterator iterator = collection.iterator();
//判断并取值
while (iterator.hasNext()){
System.out.println("Collection的元素为"+iterator.next());
}
}
}
2.增强for循环
使用:
注意事项:
eg:
public class Test {
public static void main(String[] args) {
System.out.println("HelloWorld");
//集合
Collection collection=new ArrayList( );
//为集合赋值
for (int i = 0; i < 10; i++) {
collection.add( i +1);
}
//2.增强For循环实现集合的遍历
for (Object o : collection) {
System.out.println("Collection的元素为"+o);
}
}
}
可变参数
方法参数类型确定,参数个数不确定时使用
eg:
public class Test {
public static void main(String[] args) {
System.out.println( "HelloWorld" );
//可变参数
int i = new Test().addMore( 1, 2, 3, 4, 5, 6, 7, 8, 9 );
System.out.println( i );
}
//求n个数的和
int addMore(int... arr) {
int sum = 0;
System.out.println( "参数传递了" + arr.length + "个" );
for (int i : arr) {
sum += i;
}
return sum;
}
}
Collections工具类
此类仅由静态方法组合或返回集合—类名调用来操作集合
eg:
eg:
泛型
是一种未知的数据类型,当我们不知道使用什么类型的数据,可以使用泛型
- 对比:
- 使用泛型:
- 不使用泛型–默认为object,不安全,会引发异常
- 定义包含泛型的类
泛型定义在类后
eg:
- 定义包含泛型的方法
泛型定义在修饰符和返回类型之间
eg:
- 含有泛型的接口:
使用方法
- 实现类实现接口,制定接口的泛型
interface A<E>{viod method(E e){sout(e)}}
public class B implements A<String>{
public void method(String e){sout(e)}
}
- 实现类也使用和接口一样的泛型,创建对象时来确定具体的类型
interface A<E>{viod method(E e){sout(e)}}
public class B<E> implements A<String>{
public void method(E e){sout(e)}
}
mian(){
B<String> b=new B<>();
b.method("字符串");
}
泛型通配符
传递参数时可以用
定义时不可以用
使用泛型通配后,只能使用Object类中的共性方法
eg:
- 泛型通配符的高级使用:
eg: