一.Arrays工具类
arrays类中的方法(操作数组的工具类 方法都是静态的 )
public class Kll {
public static void main(String[] args) {
/*对一个字符串数组
String[] str={"nba","abc","cba","aaaa","z","qq"};
进从小到大的排序。*/
String[] strs={"nba","abc","cba","aaaa","z","qq"};
//使用系统的方法来排序 默认升序
Arrays.sort(strs);
System.out.println(Arrays.toString(strs));
//二分查找 前提有序数组
int[] array1 = {11, 22, 33, 44, 55};
int key = 25;
// -3 -(该数应该在该数组中的位置的下标 + 1)
int index = Arrays.binarySearch(array1, key);
System.out.println(index);
}
}
二.基本数据类型
基本数据类型的包装类
数据类型 对应的包装类名称
byte[-128,127] Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
基本数据类型 声明变量 ,只有一个功能保存数据
基本数据类型转化成包装类 ,增加了成员方法和成员变量
Integer(int的包装类) 类中 有进制转化的方法
例子:
public class Kll {
public static void main(String[] args) {
//fun1();
//byte[-128,127]
Integer.valueOf(10);
Integer num1 = 128;//超过该范围 返回的是一个新 new出来的对象
Integer num2 = 128;
// == 地址比较
System.out.println(num1 == num2);
//值是否相同 equals方法重写父类的
System.out.println(num1.equals(num2));
}
private static void fun1() {
//创建一个Integer对象
Integer i1 = new Integer(10);
//将字符串转化成Integer对象
//注意:必须是数字格式的字符串类型才能转化 否则NumberFormatException异常(格式异常)
Integer i2 = new Integer("123");
System.out.println(i1+" "+i2);
//将Integer类型转化为int类型
int num = i1.intValue();
System.out.println(num);
//将int类型 转化为Integer类型
Integer i3 = Integer.valueOf(i1);
System.out.println(i3);
//声明一个 Integerduixiang
//JDK1.5 自动装箱 和 自动拆箱
//自动装箱 系统帮调用 valueOf方法
Integer n = 20;//相当于 Integer n = Integer.valueOf(20);
System.out.println(n);
//自动拆箱 系统帮调用 intValue方法
int n1 = n + 15;//相当于 int n1 = n.intValue() + 15 n不能为空对象
System.out.println(n1);
}
}
三.StringBuffer类和StringBuilder类
字符串
String :线程不安全 ,不可变字符串.
StringBuffer :JDK1.0 ,线程安全(耗费系统资源) ,可变的字符串 .
StringBuilder :JDK1.5 ,线程不安全 ,可变的字符串.
StringBuffer 和StringBuilder的方法完全一致.
- 方法
- 1.拼接 append() 返回对象本身
- 2.插入insert 返回对象本身
- 修改 setCharAt(int index, char ch)
- 3.删除 delete(),
- 按角标删除 deleteCharAt()
- 获取stringBuffer中字符 charAt()
- 4.反转 reverse()键盘输入一个字符串 将字符串反转
- 5.替换 replace()
- 6.string 创建对象转化与StringBuffer .toString()方法的相互转换
- 字符数组转stringBuffer 新建对象转换
代码如下(全部是方法):
public static void fun1() {
//创建StringBuffer对象
StringBuffer sb1 = new StringBuffer();
//获取初始容量 默认16个
System.out.println(sb1.capacity());
// 拼接 (核心方法)
sb1.append(555).append("kll").append('o');
System.out.println(sb1);
//获取字符串长度
System.out.println(sb1.length());
//StringBuffer转化成字符串
String str1 = sb1.toString();
}
public static void fun2() {
//插入字符insert()
StringBuilder sb = new StringBuilder();
//拼接
sb.append("kll");
//插入字符
sb.insert(2, 'n').append(555);
System.out.println(sb);
//修改字符
sb.setCharAt(1, 'm');
System.out.println(sb);
}
public static void fun3() {
StringBuilder sb = new StringBuilder();
sb.append("konglinglei");
//字符串删除
//如果超出字符串长度 相当于清空字符串
//sb.delete(0, 20);
System.out.println(sb);
//删除索引处的字符
sb.deleteCharAt(3);
System.out.println(sb);
//获取字符
char c = sb.charAt(3);
System.out.println(c);
//反转reverse()
System.out.println("输入字符串:");
Scanner sc1 = new Scanner(System.in);
//接受输入
String str = sc1.nextLine();
//转为StringBuffer对象
StringBuffer s1 = new StringBuffer(str);
//反转
s1.reverse();
System.out.println(s1);
}
public static void fun4() {
//替换 replace
StringBuilder sb = new StringBuilder();
sb.append("konglinglei");
sb.replace(8, 11, "po");
System.out.println(sb);
//字符数组转stringBuffer
char[] arr1 = {'a', 'b', 'c', 'd'};
String str1 = new String(arr1);
StringBuffer sb1 =new StringBuffer(str1);
}
public static void fun5() {
/*
* * 需求
* 把int[] array = new int[]{1,2,3,4};
* 输出上面的 [1, 2, 3, 4]
* 要求:使用两种拼接方法(String 和 StringBuffer)
*/
//string方法
int[] array = {1,2,3,4};
String str = "[";
for (int i = 0; i < array.length; i++) {
if (i == array.length - 1) {
str = str + array[i] + "]";
break;
}
str = str + array[i] + ",";
}
//stringbuffer 方法
StringBuffer sb = new StringBuffer("[");
for (int i = 0; i < array.length; i++) {
if (i == array.length - 1) {
sb.append(array[i]).append("]");
break;
}
sb.append(array[i]).append(",");
}
System.out.println(sb);
}
四.集合
数组弊端
1.长度一旦确定就不能更改了
2.数组只能保存相同数据类型的元素
集合(容器)
1.长度可变
2.可保存任意类型元素
注意:只能保存引用数据类型(即只能保存对象)
集合
1.单列集合
Collection(所有单列集合的父接口)
接口 类之间的部分关系图:
2.双列集合
代码案例:
//主函数:
public class Kll {
public static void main(String[] args) {
//创建学生数组
Student[] students = new Student[3];
students[0] = new Student("k1", 22);
students[1] = new Student("k2", 25);
students[2] = new Student("k3", 24);
for (int i = 0; i < students.length; i++) {
System.out.println(students[i]);
}
}
}
//学生类:
public class Student {
/*
* 学生类(name age)
* 私有化成员变量 有参无参
* set/get方法
* toString方法
*/
private String name;
private int age;
//构造方法
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
//set/get方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
// TODO Auto-generated method stub
String str = "学生名字:"+name+" 学生年龄:"+age;
return str;
}
}
用Collection 实现以下功能的代码如下:
获取集合的长度 .size()
判断是否包含某个元素 contains()
从集合中删除一个元素 remove()
判断集合 是否为空 isEmpty()
清空数组 clear()
集合转数组 toArray();
import java.util.ArrayList;
import java.util.Collection;
//注解@SuppressWarnings(压制警告)
//rawtypes :保持原有类型
//不检查容器中储存的元素类型
@SuppressWarnings({"rawtypes","unchecked"})
public class Demo05 {
public static void main(String[] args) {
Collection collection = new ArrayList<>();
collection.add("a");
collection.add("b");
collection.add("c");
collection.add("d");
// 获取集合的长度
System.out.println(collection.size());
// 判断是否包含某个元素
boolean b1 = collection.contains("b");
System.out.println(b1);
// 从集合中删除一个元素
boolean b2 = collection.remove("b");
System.out.println(b2);
System.out.println(collection);
// 判断集合 是否为空
boolean b3 = collection.isEmpty();
System.out.println(b3);
// 清空数组
//collection.clear();//removeAll()
System.out.println(collection);
// 集合转数组
Object[] objs = collection.toArray();
System.out.println(objs);
//遍历集合
for (Object object : objs) {
System.out.println(object);
}
}
private static void fun1() {
//创建一个集合 测试ArrayList重写Collection中的方法
//当向集合中添加基本数据类型时 系统会进行自动装箱
//ArrayList添加元素 不可能返回false
//系统在父类中设计这个抽象方法 为什么要写带返回值的? 设计思想
//抽象方法要适用所有子实现类 该返回值是给Set用的(Set有可能添加失败)
Collection collection = new ArrayList();
boolean b1 = collection.add("a");
boolean b2 = collection.add("a");
boolean b3 = collection.add(new Student());
boolean b4 = collection.add(10);//实际是Integer对象
System.out.println(b1 +" "+b2 +" "+b3+" "+b4);
System.out.println(collection);
}
}
习题代码:
其中的注意点是:集合元素转对象
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
/*
* 多态形式创建一个集合
* 添加三个学生对象
* 遍历集合
* 只打印学生姓名(其他的不打印)
*/
@SuppressWarnings({"rawtypes", "unchecked"})
public class Kll {
public static void main(String[] args) {
// 多态形式创建一个集合
Collection col = new ArrayList<>();
//添加三个学生对象
col.add(new Student("k1", 22));
col.add(new Student("k2", 25));
col.add(new Student("k3", 24));
//向上转型 集合转数组
Object[] objs = col.toArray();
System.out.println(Arrays.toString(objs));
//向下转型(调用特有方法 getName())
//Student[] students = (Student[])objs;
//错误强转 只改变了容器的类型 容器内的元素并未变成Student类型
//ClassCastException 类型转换异常
for (int i = 0; i < objs.length; i++) {
//将数组中的每一个元素强转
Student stu = (Student)objs[i];
System.out.println(stu.getName());
}
}
}
//学生类:
public class Student {
/*
* 学生类(name age)
* 私有化成员变量 有参无参
* set/get方法
* toString方法
*/
private String name;
private int age;
//构造方法
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
//set/get方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
// TODO Auto-generated method stub
String str = "学生名字:"+name+" 学生年龄:"+age;
return str;
}
}
五.权限修饰符
权限修饰符
1.public (公开的)
2.protected (受保护的)
3.default (默认的) 什么权限修饰符都不写 就是默认的
4.private (私人的)
以下是对权限修饰符修饰下的成员变量的是否能访问到 做出的总结
本类 同包类 同包子类 不同包类 不同包子类
public ✔ ✔ ✔ ✔ ✔
protected️️ ✔ ✔ ✔ ❌ ✔
default ✔ ✔ ✔ ❌ ❌
private ✔ ❌ ❌ ❌ ❌