2021-05-29

学习java第十七天

String类

String 类代表字符串。Java 程序中的所有字符串字面值(如 “abc” )都作为此类的实例实现。
字符串是常量;它们的值在创建之后不能更改。字符串缓冲区支持可变的字符串。因为 String 对象是不可变的,所以可以共享

常用的构造方法

1、public String() 创建一个空白的字符串,不包含任何内容
2、public String(char[] array) 根据字符数组来创建字符串
3、public String(byte[] array) 根据字节数组来创建字符串

最直接的方式:
String str=“class5”;
例1

package demo01;
public class Demo01String {
   public static void main(String[] args) {
   	String str1 = new String();//小括号留空,没有内容。
   	System.out.println("第一个字符串:"+str1);
   	System.out.println("====================");
   	
   	//字符数组
   	char[] chararray = new char[10];
   	System.out.println(chararray);
   	String str2 = new String(chararray);
   	System.out.println(str2);
   	System.out.println("===================="); 
   	//字节数组
   	byte[] bytearray = {10,20,30};
   	System.out.println(chararray);
   	String str3 = new String(chararray);
   	System.out.println(str3);
   	
   	//直接创建字符串
   	//String str4 = new
   	String str4 = "class5";
   	System.out.println(str4);
   }
}

字符串之间的比较
方法一:public Boolean equals(Object obj);(参数可以是任何对象,只有参数是一个字符串并且内容相同时才会是true,否则返回false)
方法二:Public Boolean equalsIgnoreCase(String str);忽略大小写,进行内容比较

package demo1;
/*
 * 字符串常量池
 * int a=5
 * int b=6
 * 
 * 对于基本类型==比较的是值
 * 对于引用类型==比较的是地址值
 */
public class Stringpool {
public static void main(String[] args) {
	String str1="abc";
	String str2="abc";
	System.out.println(str2);
	char[] chararray= {'a','b','c'};
	String str3=new String(chararray);
	System.out.println(str1==str2);
	System.out.println(str1==str3);
	System.out.println(str2==str3);
	
	str2="cde";
	System.out.println(str2);
}
}

public boolean equal(Object obj) 方法
public boolean equals(Object obj)指示一些其他对象是否等于此。 该equals类方法Object实现对象上差别可能性最大的相等关系; 也就是说,对于任何非空的参考值x和y ,当且仅当x和y引用相同的对象( x == y具有值true )时,该方法返回true 。 但在一些类库当中这个方法被覆盖掉了,如String,Integer,Date在这些类当中equals有其自身的实现,而不再是比较类在堆内存中的存放地址了,他们中的equals方法用于比对相同包装类中的值是否相等。

请注意,无论何时覆盖该方法,通常需要覆盖hashCode方法,以便维护hashCode方法的通用合同,该方法规定相等的对象必须具有相等的哈希码。

package demo02;
public class Demo01StringEquals {
  public static void main(String[] args) {
  	String str1 = "Why";
  	String str2 = "Why";
  	System.out.println(str2);
  	char[] chararray = {'W','h','y'};
  	String str3 = new String(chararray);
  	
  	System.out.println(str1.equals(str2));
  	System.out.println(str2.equals(str3));
  	System.out.println (tr1.equals(str3));
  	
  	System.out.println(str1.equals("Why"));
  	System.out.println("Why".equals(str1));//建议这样写
  	System.out.println("====================");
  	String str4 = null;
  	// System.out.println(str1.equals("Why"));
  	System.out.println("Why".equals(str4));
  	System.out.println("====================");
  	
  	String str5 = "why";
  	// System.out.println(str1.equals("Hio"));
  	System.out.println("why".equals(str5));
  	System.out.println("====================");
  	
  	System.out.println("Why");
  }

public int length ( ) 方法

package demo2;

public class StringGet {
public static void main(String[] args) {
	//字符串长度
	int length="ewifewnferiejg4fgjgh".length();
	System.out.println("字符串长度"+length);
	
	//字符串拼接
	String str1="Hello";
	String str2="class5";
	String str3=str1.concat(str2);
	System.out.println(str1);
	System.out.println(str2);
	System.out.println(str3);
	
	//获得指定索引位置的单个字符
	char ch="Hello".charAt(4);
	System.out.println("0号索引位置的字符是:"+ch);
	//查找某个字符串第一次出现的位置
	String origanl="HelloworldHelloworldHelloworld";
	int index=origanl.indexOf("llo");
	System.out.println(index);
	
	System.out.println(origanl.indexOf("class"));
	
}
}

分割字符串
public String[] split(String)

package demo2;
/*
 * 分割字符串
 * public String[] split (String)
 */
public class StringSplit {
public static void main(String[] args) {
	String str1="aaa bbb ccc";
	String [] array1=str1.split(" ");
	for(int i=0;i<array1.length;i++) {
		System.out.println(array1[i]);
	}
}
}

SubString方法

public class Demo03SubString {
    public static void main(String[] args) {
        String str1= "helloclass5";
        String str2=str1.substring(5);
        System.out.println(str1);
        System.out.println(str2);
        String str3=str1.substring(2,5);
        System.out.println(str3);
    }
}

Arrays类
一、Arrays类的定义
Arrays类位于 java.util 包中,主要包含了操纵数组的各种方法

使用时导包:import java.util.Arrays

二、Arrays常用函数(都是静态的)

1.void Arrays.sort()

void Array.sort(Object[] array)

功能:对数组按照升序排序

Arrays.sort(Object[] array, int from, int to)

功能:对数组元素指定范围进行排序(排序范围是从元素下标为from,到下标为to-1的元素进行排序)

2…Arrays.fill(Object[] array,Object object)

功能:可以为数组元素填充相同的值

Arrays.fill(Object[] array,int from,int to,Object object)

功能:对数组的部分元素填充一个值,从起始位置到结束位置,取头不取尾

3.Arrays.toString(Object[] array)

功能:返回数组的字符串形式

4.Arrays.deepToString(Object[][] arrays)

功能:返回多维数组的字符串形式

package demo03;

import java.util.Arrays;
public class Demo01Arrays {
   public static void main(String[] args) {
   	int[] intArray = {10,20,30};
   	System.out.println(intArray);
   	String intStr = Arrays.toString(intArray);
   	System.out.println(intStr);
   	
   	int[] array1 = {1,2,3,4,5};
   	System.out.println(Arrays.toString(array1));
   	Arrays.sort(array1);
   	System.out.println(Arrays.toString(array1));
   	
   	String[] array2 = {"zzz","xxx","ccc"};
   	Arrays.sort(array2);
   	System.out.println(Arrays.toString(array2));
   }
}

Math

public class Demo03Math {
    public static void main(String[] args) {
        //绝对值
        System.out.println(Math.abs(3.14));
        System.out.println(Math.abs(0));
        System.out.println(Math.abs(-3.14));
        System.out.println("========");

        //向上取整
        System.out.println(Math.ceil(3.14));
        System.out.println(Math.ceil(3.91));
        System.out.println(Math.ceil(3.00001));
        System.out.println("========");

        //向下取整
        System.out.println(Math.floor(3.14));
        System.out.println(Math.floor(3.91));
        System.out.println(Math.floor(3.00001));
        System.out.println("========");

        //四舍五入
        System.out.println(Math.round(3.14));
        System.out.println(Math.round(3.91));
    }
}

StaticFiled

public class Student {
    private  int id;
    private String name;
    private  int age;
    private static String room;

    public Student(){}
    public Student(int id, String name, int age, String room){
        super();
        this.id=id;
        this.name=name;
        this.age=age;
        this.room=room;

    }
    public Student(String name,int age){
        super();
        this.name=name;
        this.age=age;

    }
    public void setId(int id) {
        this.id = id;
    }
    public int getId() {
        return id;
    }
    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;
    }
    public String getRoom() {
        return room;
    }
    public void setRoom(String room) {
        this.room = room;
    }
}
package demo04;
/*
 * 如果一个成员变量用static关键字修饰,那么这个内容不再属于对象自己,而是属于类的。所以凡是本类的对象,都共享一份。
*/
public class Demo01StaticFiled {
	public static void main(String[] args) {
		Student one = new Student("廖磊",19);
		one.setRoom("3306教室");
		System.out.println(one.getName()+"  "+one.getAge()+"  "+one.getRoom());
		
		Student two = new Student("juju",18);
		System.out.println(two.getName()+"  "+two.getAge()+"  "+two.getRoom());
	}
}

StaticMethod

public class MyClass {
        int num;
        static int numstatic;

        public void method() {
            System.out.println("这是成员方法");
        }
        public static void methodStatic() {
            System.out.println("这是静态方法");
        }
}

如果用static关键字修饰成员方法 , 变成了静态方法,静态方法不属于对象,属于类。

可以通过对象来调用 也可以通过类直接调用

package demo04;
public class Demo02StaticMethod {
	public static void main(String[] args) {
		MyClass obj = new MyClass();
		obj.method();//普通方法,必须通过对象类调用
		
		obj.methodStatic();//可以通过对象类调用      正确 不推荐
		MyClass.methodStatic();//可以通过类直接调用  正确 推荐
	}
}
以下是一个可能的Java实现: ```java import java.time.LocalDate; import java.time.temporal.ChronoUnit; import java.util.ArrayList; import java.util.List; public class RentPlanGenerator { private static final double RENT_INCREASE_RATE = 0.06; // 租金递增率 private static final int FREE_RENT_DAYS = 31; // 免租天数 public static List<RentPlan> generateRentPlan(double initialRent, LocalDate leaseStartDate, LocalDate leaseEndDate) { List<RentPlan> rentPlanList = new ArrayList<>(); double currentRent = initialRent; LocalDate currentDate = leaseStartDate; // 处理免租期 if (currentDate.isBefore(leaseStartDate.plusDays(FREE_RENT_DAYS))) { currentDate = leaseStartDate.plusDays(FREE_RENT_DAYS); } while (currentDate.isBefore(leaseEndDate)) { LocalDate nextIncreaseDate = currentDate.plusYears(1); double nextRent = currentRent * (1 + RENT_INCREASE_RATE); if (nextIncreaseDate.isBefore(leaseStartDate.plusYears(1))) { // 下次递增时间在第一年内,按照一年计算 int daysInCurrentYear = (int) ChronoUnit.DAYS.between(currentDate, nextIncreaseDate); rentPlanList.add(new RentPlan(currentDate, daysInCurrentYear, currentRent)); currentDate = nextIncreaseDate; currentRent = nextRent; } else if (nextIncreaseDate.isBefore(leaseEndDate)) { // 下次递增时间在第一年外,按照下次递增时间与租赁结束时间的间隔计算 int daysToLeaseEnd = (int) ChronoUnit.DAYS.between(currentDate, leaseEndDate); rentPlanList.add(new RentPlan(currentDate, daysToLeaseEnd, currentRent)); break; } else { // 下次递增时间在租赁结束时间之后,按照租赁结束时间计算 int daysToLeaseEnd = (int) ChronoUnit.DAYS.between(currentDate, leaseEndDate); rentPlanList.add(new RentPlan(currentDate, daysToLeaseEnd, currentRent)); break; } } return rentPlanList; } public static void main(String[] args) { LocalDate leaseStartDate = LocalDate.of(2021, 3, 1); LocalDate leaseEndDate = LocalDate.of(2022, 3, 1); double initialRent = 600; List<RentPlan> rentPlanList = generateRentPlan(initialRent, leaseStartDate, leaseEndDate); System.out.printf("%-12s%-12s%-12s%n", "时间", "天数", "租金"); for (RentPlan rentPlan : rentPlanList) { System.out.printf("%-12s%-12d%-12.2f%n", rentPlan.getStartDate(), rentPlan.getDays(), rentPlan.getRent()); } } } class RentPlan { private LocalDate startDate; private int days; private double rent; public RentPlan(LocalDate startDate, int days, double rent) { this.startDate = startDate; this.days = days; this.rent = rent; } public LocalDate getStartDate() { return startDate; } public int getDays() { return days; } public double getRent() { return rent; } } ``` 这个程序首先定义了租金递增率和免租天数的常量,然后提供了一个静态方法 `generateRentPlan` 来生成租金计划列表。该方法接受三个参数:初始月租金、租赁开始时间和租赁结束时间。 具体实现时,我们使用循环来逐月生成租金计划。在每次循环,我们首先计算下次递增租金的时间和金额。然后根据下次递增时间与租赁开始时间的间隔,决定本次循环处理的天数和租金金额。最后将这些信息保存到一个 `RentPlan` 对象,并添加到租金计划列表。 在主函数,我们使用 `generateRentPlan` 方法生成租金计划列表,并以表格形式输出输出结果如下: ``` 时间 天数 租金 2021-04-01 30 600.00 2021-05-01 31 636.00 2021-06-01 30 674.16 2021-07-01 31 713.57 2021-08-01 31 754.29 2021-09-01 30 796.39 2021-10-01 31 840.94 2021-11-01 30 887.02 2021-12-01 31 934.72 2022-01-01 31 984.12 2022-02-01 28 1035.30 ``` 可以看到,程序正确地根据递增周期和递增率生成了每个月的租金计划,并且考虑了免租期的影响。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值