第四章 常用类和包
1、常用类
1.1、String类
字符串:用双引号引起来的(单个字符、多个字符:数字、字母、汉字)
类型:引用类型 存储在栈中;
特点:不可变实例对象;一旦创建,实例就不会改变;每次的改变都是重新创建新的对象;
常用方法
| 方法名 | 示例 | 说明 |
|---|---|---|
| equals( ) | a.equals(b) | 比较字符串内容 |
| equalsIgnoreCase( ) | "abc".equalsIgnoreCase("AbC") 返回true | 忽略大小写进行比较 |
| toUpperCase( ) | 转换成大写 | |
| toLowerCase( ) | 转换小写 | |
| concat( ) | "a".concat("b") 返回 “ab” | 连接字符串 |
| startsWith( ) | 字符串1.stratsWith(字符串2) | 判断字符串1是否以字符串2开头 |
| endsWith( ) | 字符串1.endsWith(字符串2) | 判断字符串1是否以字符串2结束 |
| indexOf( ) | 字符串.indexOf(子字符串) | 查找一个子字符串在原始字符串第一次出现的索引 |
| lastIndexOf( ) | 字符串.indexOf(子字符串) | 查找一个子字符串在原始字符串最后一次出现的索引 |
| replace( ) | "happy".replace("y","h") 返回字符串 “happh” | 在原始字符串中的字符串1替换成字符串2 |
| substring(int 参数) | 从开始索引截取到字符串末尾 | |
| substring(int 索引1,int 索引2) | 从索引1开始截取,截取到索引2,不包括索引2位置上的字符 | |
| trim( ) | 清除字符串前后端的空格 | |
| length( ) | 获取字符串的长度 |
1.2、StringBuffer类
工具类:将字符串的值存入缓存中,去修改缓存中的字符串的值,不用新开辟内存;可变的实例对象
应用场景:需要频繁的去修改字符串时使用;
public class Prac3 {
public static void main(String[] args) {
String str1="字符串啊,咋办啊,不知道啊,完蛋了";
StringBuffer strBuffer=new StringBuffer(str1);
System.out.println(strBuffer.length());
strBuffer.append("我知道了");
strBuffer.append("你知道嘛");
strBuffer.append("我不知道");
strBuffer.append("他知道嘛");
strBuffer.append("他不知道");
strBuffer.append("那我知道了");
strBuffer.append("你知道了嘛");
System.out.println(strBuffer.toString());
System.out.println(strBuffer.length());
strBuffer.insert(0,"原来如此");
System.out.println(strBuffer.toString());
StringBuffer stringBuffer=new StringBuffer(10);
System.out.println(stringBuffer.capacity());
}
}
1.3、Date和格式化
public class Prac4 {
public static void main(String[] args) throws ParseException {
Date dt=new Date();
System.out.println(dt);
//格式化处理
SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
System.out.println(dateFormat.format(dt));
String string1="2099-09-23";
System.out.println(dateFormat.parse(string1));
}
}
2、包package
包的作用:如果有同名类,太多的类,为了便于管理;放在不同的文件夹,也就是包
包的命名:通常为域名的反转,小写字母
例如:www.baidu.com 的包名就是 :com.baidu.项目名.某模块.子模块
步骤:
1、创建包
右键src创建package

idea中包名默认合并,可以选择展开

2、程序中声明包
package com.aaa.qy106.mypackage;//第一行代码
3、引入包
import com.aaa.qy106.mypackage.Phone;//引入其他包的类
3、访问修饰符
| 访问修饰符 | 说明 |
|---|---|
| public | 在任意类中访问 |
| 默认 | 在同一个包中的不同类中访问 |
| private | 只能在本类中访问 |
| protected | 同一个包中,或者是子类中 |
4、static
| static | 非静态(实例) | |
|---|---|---|
| 属性 | 静态属性 | 实例属性 |
| 方法 | 静态方法 | 实例方法 |
| 调用方式 | 类.属性 类.方法() | 对象.属性 对象.方法() |
| 隶属 | 类 | 对象 |
静态和实例属性:
-
静态属性初始化时间:jvm加载类时完成初始化;
-
实例属性初始化时间:在创建对象时进行初始化;
方法访问过程:
-
静态方法能直接访问:静态属性和方法,不能直接访问实例的属性和方法;
-
实例方法能直接访问:静态属性和方法,能直接访问实例的属性和方法;
应用场景:
static:调用的时候方便;
工具类中定义的静态方法比较多,如果方法不依赖于实例属性,一般定义成静态的;
5、常量
常量:定义之后不能改变的量,值不能改变。用final关键字修饰
1、局部变量
public static void main(String[] args){
final double PI=3.14;
System.out.print(PI);
}
2、成员常量:实例属性
public class Test2{
final int MAX_NUMBER=1000;
public static void main(String[] args)
{
Test2 test2=new Test2();
System.out.println(test2.MAX_NUMBER);
}
}
3、类(静态)常量:
public class Test2{
final static int MIN_NUMBER=0;
public static void main(String[] args)
{
System.out.print(MIN_NUMBER);
}
}
6、基本数据类型和其对应的包装类
| 基本数据类型 | 包装类 |
|---|---|
| byte | Byte |
| short | Short |
| int | Integer |
| long | Long |
| float | Float |
| double | Double |
| char | Character |
| boolean | Boolean |
第五章 数组
1、数组基本概念
数组:一 系列 相同数据类型的集合,(包括引用类型)。
特点:一旦创建,长度固定;
创建数组语法:
数据类型[] 数组名=new 数据类型[长度];
数据类型 数组名[] =new 数据类型[长度];
public class Test1{
public static void main(String[] args)
{
//声明并赋值
int[] ary1=new int[3];
int ary22[]=new int[4];
String[] ary2=new String[3];
int[] ary3=new int[]{10,20,30};
int[] ary4={10,20,30};
//先声明后赋值
int []ary5;
ary5=new int[5];
ary5=new int[]{1,2,3};
//length:长度属性
//注意:字符串的长度为方法,数组的长度为属性。
String str="123";
str.length();
ary1[0]=10;
for(int i=0;i<ary1.length;i++)
{
System.out.println("请输入数字:");
ary[i]=scaner.nextInt();
}
//取数据
/* for(int i=0;i<ary1.length;i++){
System.out.print(ary1[i]+"\t");
}*/
//foreach:依次遍历每一个元素
// 语法: 变量 :数组或者集合
for (int i : ary1) {
System.out.println(i+"\t");
}
System.out.println();
}
}
for(int i:ary1){} 一般用来遍历显示数据
2、数组的应用
2.1查找
public static void main(String[] args)
{
int[] score={90,50,60,20,30};
int key=60;
int index=-1;
for(int i=0;i<score.length;i++)
{
if(score[i]==key)
{
index=i;
}
}
if(index==i)
{
System.out.println("未找到");
}else
{
System.out.println("找到了,下标是"+index);
}
}
2.2 删除
// 步骤:// 1、查找对应的下标// 2、删除元素,从找到的下标开始,后续的元素依次往前移动
public class Delete {
public static int[] del(int[] score,int key){
int index=-1;//记录下标
int temp;//声明中间变量
for (int i=0;i<score.length;i++)//用来查找要删除的元素索引
{
if (score[i]==key)
{
index=i;
break;//找到第一个要删除的元素后就中断循环
}
}
for (int j=index;j<score.length;j++)//将元素向前移动
{
if (j+1<=score.length-1)
{
temp=score[j];
score[j]=score[j+1];
score[j+1]=temp;
}
}
score[score.length-1]=0;//将最后一个元素设为0 模拟删除
return score;
}
}
2.3 插入
给定一个有序数组:{10,20,30,40,50}
插入一个元素:35
步骤:
1、查找第一个比当前插入元素大的元素对应的下标
2、后续的元素要依次往后移动:先移动后面的元素;
public class Insert {
public static int[] Ins(int[] a,int value)
{
int index=-1;//记录下标
int newa[]=new int[a.length+1];//声明新的数组比原来有序数组的长度多一
for (int i=0;i<a.length;i++)//用来查找要插入的位置
{
if (value<a[i])
{
index=i;
break;//找到位置后就中断循环
}
}
for (int k=0;k<newa.length-1;k++)
{
newa[k]=a[k];
}
for (int j=newa.length-1;j>=index;j--)
//将元素向后移动为即将插入的元素腾出位置
{
newa[j]=newa[j-1];
}
newa[index]=value;//将元素插入到指定位置
return newa;
}
}
2.4排序
1、冒泡排序
public static void main(String[] args)
{
int [] ary=new int[]{20,1,4,52,30,15};
/*
* 思想:两两相比,小靠前
* 1轮:52
* 1,4,20,30,15,52 :5
* 2轮 :30,52
* 1,4,20,15,30,52 :4
* 3轮: 20,30,52
* 1,4,15,20,30,52 :3
* 4轮:
* 1,4,15,20,30,52 :2
* 5轮:
* 1,4,15,20,30,52 :1
* */
//外层循环控制轮数
for(int i=1;i<=arry.length-1;i++)
{
for(int j=0;j<ary.length-i;j++)
{
if(ary[j]>ary[j+1])
{
int temp=ary[j];
ary[j]=ary[j+1];
ary[j+1]=temp;
}
}
}
}
2、选择排序
public static void main(String[] args)
{
int[] a=new int[]{20,1,4,52,30};
for (int i=0;i<a.length-1;i++)
{
int minIndex=i;//假定第一个是最小值
for(int j=i;j<a.length;j++)
{
if (a[j]<a[minIndex]) {//找到最小值下标
minIndex = j;
}
}
if (a[i]!=a[minIndex])//交换最小值到已派好序列的末尾
{
int temp=a[minIndex];
a[minIndex]=a[i];
a[i]=temp;
}
System.out.println(Arrays.toString(a));
}
}
3、插入排序
public class InsertSort {
public static int[] insort(int a[]) {
int temp;
for (int i = 1; i <= a.length - 1; i++) {
for (int j = 0; j <= i; j++) {
if (a[j] > a[i]) {
temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
}
return a;
}
}
未使用交换的插入排序
public class MyArrays {
//排序
public static void sort1(int[] ary){
if(ary==null||ary.length==0) {
return;
}
for(int i=1;i<ary.length;i++){
//有序数组:20
int tmp=ary[i];//待插入的元素
int j;
for( j=i-1;j>=0;j--){
if(ary[j]>tmp){
ary[j+1]=ary[j];
}else{
break;
}
}
ary[j+1]=tmp;
}
}
//显示
public static void show(int[] ary){
for (int i : ary) {
System.out.print(i+"\t");
}
}
}
数组 工具类:Arrays 提供了一系列常用的方法
例如Arrays.toString(数组名)
3、二维数组
二维数组:由多个一维数组构成的数组称为二维数组。
public static void main(String[] args) {
int[] ary0=new int[3];
//长度1:行数,几个一维数组
//长度2:每个一位数组的长度
int[][] ary=new int[2][3];
int[][] ary1={{1,2,3},{4,5,6},{7,8,9}};
//不规则二维数组
String[][] ary2=new String[][]{{"林冲","潘金莲"},{"扈三娘","阎婆惜","武松"}};
//循环赋值
for(int i=0;i<ary.length;i++){
//ary[i]:一维数组
for(int j=0;j<ary[i].length;j++){
ary[i][j]=i+j;
}
}
//遍历显示
for(int i=0;i<ary.length;i++){
//ary[i]:一维数组
for(int j=0;j<ary[i].length;j++){
System.out.print(ary[i][j]+"\t");
}
System.out.println();
}
System.out.println();
for (String[] tmpAry : ary2) {
for (String s : tmpAry) {
System.out.print(s+"\t");
}
System.out.println();
}
}
4、对象数组
数组每个位置上存上对象 称为对象数组。
public static void main(String[] args) {
//存储学生信息
int[] ary=new int[5];
Student student=new Student();
student.name="张三";
student.age =18;
student.sex='男';
Student student1=new Student();
student1.name="李白";
student1.age =18;
student1.sex='男';
Student student2=new Student();
student2.name="小云";
student2.age =18;
student2.sex='女';
Student[] stus=new Student[3];
stus[0]=student;
stus[1]=student1;
stus[2]=student2;
for (Student s1 : stus) {
System.out.println(s1.name+":"+s1.sex+":"+s1.age);
}
}
318

被折叠的 条评论
为什么被折叠?



