1.String练习
字符串反转,给定字符串,给定起始位置和结束位置,对字符串进行反转
StringBuffer stringBuffer=new StringBuffer("hellojava");
StringBuffer newstr=new StringBuffer();
char[]arrys=stringBuffer.substring(1,9).toCharArray();
for (int i=arrys.length-1;i>=0;i--){
newstr.append(arrys[i]);
}
System.out.println(newstr);
获取一个字符串再另一个字符串中出现的次数。比如“abc”在“abcbbaaabccabddddabeeeeaaaaab“中出现的次数
String str="abcbbaaabccabddddabeeeeaaaaab";
Pattern pattern=Pattern.compile("ab");
Matcher matcher= pattern.matcher(str);
int count=0;
while (matcher.find()){
count+=1;
}
System.out.println("ab出现的次数为"+count);
查找最大相同字串”aabbccadeahellocadfasfdas“,与字符串”aaellocc“最大相同字串是”elloc“。
String str1="aabbccadeahellocadfasfdas";
String str2="aaellocc";
for (int s=0;s<str2.length();s++){
int a = 0;
for(int i=0,j=str2.length()-s;j<str2.length();i++,j++) {
String newstr2 = str2.substring(i,j+1);
if (str1.contains(newstr2)) {
System.out.println(newstr2);
a++;
break;
}
}
if(a!=0){
break;
}
}
日期类型:打印日历,输入年月 ,打印本月的日历
Scanner scanner=new Scanner(System.in);
System.out.println("输入年份和月份");
int year=scanner.nextInt();
int month=scanner.nextInt();
LocalDate localDate=LocalDate.of(year,month,1);
DayOfWeek d=localDate.getDayOfWeek();
int day=d.getValue();
System.out.println("星期一\t星期二\t星期三\t星期四\t星期五\t星期六\t星期天");
for (int i=1;i<day;i++){
System.out.print("\t\t");
}
while (localDate.getMonthValue()==month){
d=localDate.getDayOfWeek();
System.out.print(" "+localDate.getDayOfMonth()+"\t\t");
if(d.getValue()==7){
System.out.println();
}
localDate=localDate.plusDays(1);
创建学生类(包含编号,姓名,性别,年龄,成绩),按照年龄排序输出,按照 年龄排序输出,按照成绩排序输出
private int id;
private String name;
private String sex;
private int age;
private double garder;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getGarder() {
return garder;
}
public void setGarder(double garder) {
this.garder = garder;
}
@Override
public int compareTo(Object o){
if(o instanceof Student){
Student s=(Student) o;
if (this.age>s.age){
return 1;
}
else if (this.age==s.age){
return 0;
}
}
return -1;
}