1、子串出现次数
public class ZiChuanCiShu {
public static void main(String[] args) {
String s = "abcbbaaabccabddddabeeeeaaaaab";
String aim = "abc";
int count = 0;
while(s.contains(aim)){
count++;
s= s.replaceFirst(aim,"");
}
System.out.println(count);
}
}
2、最大子串
public class maxString {
public static void main(String[] args) {
String str1 = "aabbccadeahellocadfasfdas";
String str2 = "aaellocc";
String maxString = maxSubString(str1,str2);
System.out.println("最大的相同子字符串是:" + maxString);
}
public static String maxSubString(String str1, String str2) {
String str = new String();
if (str1.length() < str2.length()) {
String temp = str1;
str1 = str2;
str2 = temp;
}
if (str1.contains(str2))
return str2;
for (int i = str2.length() - 1; i >= 0; i--) {
for(int j = 0; (j + i) <str2.length() ; j ++) {
// if match successfully, return it
str = str2.substring(j, j+i);
if(str1.contains(str))
return str;
}
}
return str;
}
}
3、日历
package changyonglei;
import java.util.Calendar;
import java.util.Scanner;
public class RiLi {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入年和月");
int year = input.nextInt();
int month = input.nextInt();
int days= 31;
if(month>=1&&month<=12){
switch (month){
case 2:
if(year%4==0&&year%100!=0||year%400==0){
days=29;
}else {
days=28;
}
break;
case 4:
case 6:
case 9:
case 11:
days=30;
break;
}
}
//
Calendar calendar = Calendar.getInstance();
calendar.set(year,month-1,1);
int dw = calendar.get(Calendar.DAY_OF_WEEK);//这个月的第一天是周几
System.out.println("日\t一\t二\t三\t四\t五\t六");
int count = 0;
for(int i=1;i<dw;i++){
System.out.print("\t");
count++;
}
for(int i=1;i<=days;i++){
System.out.print(i+"\t");
count++;
if(count%7==0){
System.out.println();
}
}
}
}
4、数组保存对象
package ioliu;
public class Person implements Comparable<Person>{
private String name;
private int chinese;
private int math;
private int egnlish;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getChinese() {
return chinese;
}
public void setChinese(int chinese) {
this.chinese = chinese;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getEgnlish() {
return egnlish;
}
public int getsum(){
return chinese+math+egnlish;
}
public void setEgnlish(int egnlish) {
this.egnlish = egnlish;
}
public Person() {
}
public Person(String name, int chinese, int math, int egnlish) {
this.name = name;
this.chinese = chinese;
this.math = math;
this.egnlish = egnlish;
}
@Override
public int compareTo(Person o) {
if(this.getsum()==o.getsum()){
return 0;
}else if(this.getsum()<o.getsum()){
return -1;
}else{
return 1;
}
}
}
package ioliu;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.Arrays;
public class PernTest {
public static void main(String[] args) {
Person[] arr = new Person[5];
arr[0] = new Person("张一",78,89,98);
arr[1] = new Person("张二",89,98,75);
arr[2] = new Person("张三",66,78,90);
arr[3] = new Person("张四",55,67,89);
arr[4] = new Person("张五",66,67,56);
Arrays.sort(arr);
System.out.println("姓名\t语文\t英语\t数学\t总分");
for(Person p:arr){
System.out.println(p.getName()+"\t"+p.getChinese()+"\t"+p.getEgnlish()+"\t"+p.getMath()+"\t"+p.getsum());
}
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter("D:/A/person.txt"));
writer.write("姓名\t语文\t英语\t数学\t总分");
writer.newLine();
for(Person p:arr){
String s = p.getName()+"\t"+p.getChinese()+"\t"+p.getEgnlish()+"\t"+p.getMath()+"\t"+p.getsum();
writer.write(s);
writer.newLine();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(writer!=null){
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}