(For Final Exam)java 期末复习总结

Uncategorized
26k words

Unicode编码

  • Unicode 统一码,万国码
  • 目标是支持世界上所有的字符
  • 表示:在值前面加上前缀/u

耦合

  • 耦合也叫块间联系,指软件系统结构中各模块之间相互联系紧密程度的一种度量,模块之间联系越紧密,其耦合性就越强,模块之间越独立,耦合性就越差

package

  • 同一个包中类的名字是不同的,不同的包中类的名字可以相同,当同时调用两个不同包中相同类名的类时,应该加上包名加以区别,因此,包可以避免名字冲突

  • 包也限定了访问权限,拥有包访问权限的类才能访问其中某个包的类

继承

  • 继承就是子类继承父类的特征和行为,使得子类对象(实例)具有父类的实例和方法;或子类从父类继承方法,使得子类具有父类相同的行为

  • 一个Java类只能有一个父类

  • 继承关键字:extendsimplementsfinal

class father{
	
}
class children extends father{

}
  • java不支持多继承,但是支持多重继承(java中类只允许单一继承),但可以通过接口实现多继承

  • 继承后,子类拥有父类非public的属性,同时子类也可以拥有自己的属性和方法,及子类可以对父类进行扩展

  • 提高了类之间的耦合性(继承的缺点,耦合度越高就会造成代码之间的联系越密切,代码独立性越差)

修饰符

修饰符 当前类 当前包 子孙类 其他包
public
protected ×
private × × ×
default × ×

this关键字

  • 在方法中引用成员变量(尤其是区分同名的局部变量)

  • 在构造方法中调用其他构造方法(一定出现在第一句)

public class Main{
	public Main(){
	System.out.println( " 构造方法一被调用了" );
}
	public Main( int x ){
	this();
	System.out.println( "构造方法二被调用了" );
}
	public Main( boolean b ){
	System.out.println( "构造方法三被调用了" );
}
	public static void main( String[] args ){
	Main a = new Main( true );
}

}

<!-7-11 jmu-java-03面向对象基础-05-覆盖->

##抽象类和接口

  • 接口被子类实现,抽象类要被子类继承

  • 接口变量全为公共静态变量,抽象类中可有普通变量

  • 接口中全为方法的声明,抽象类中可以有方法的实现

  • 接口中不可以有构造函数,抽象类中可以有构造函数

  • 接口可多实现,而抽象类必须被单继承

  • 接口中方法全为抽象方法,而抽象类中可以有非抽象的方法

  • 抽象类不能被实例化

  • 接口的成员变量只能是常量,默认修饰符:public static final

  • 接口的成员方法只能是抽象方法,默认修饰符:public abstract

  • 使用interface声明一个接口时,只可以用public修饰符修饰该接口

public class Usertest implements UserDao{
	public void myPrint(){
	System.out.println("nihao");
}
	public void insert(A a){
	System.out.println("新增");
}
}

异常

public static boolean cathcFinallyTest1(){
	try{
	int i = 10 / 0;
	System.out.println("i value is :" + i );
	return true;
}
	catch ( Exception e ){
	System.out.println("-- Exception --");
	return catchMethod();
}
	finally{
	finallyMethod();
}
}
  • throwable是所有异常类的父类

  • throws 是方法可能抛出异常的声明(在声明方法时,表示该方法可能要抛出异常)

public void doA( int a ) throws Exception1 , Exception3{
	
}
  • throw是语句(抛出异常的语句)

try{
	...
} catch ( Exception e ){
	throw e;
}

数组

  • 不允许在前面的括号里面写元素个数

正确地定义一个二维数组
int a[][] = new int[3][];

eg: 对于int[] a = new int[10]; a[10] -= 1;会使其发生数组越界

类于对象

  • 构造函数是类的一种特殊函数,它的方法名必须与类名相同

  • 构造函数的主要作用是完成对类的c对象的初始化工作

  • 一般在创造对象时,系统会自动调用构造函数

  • String是final修饰的Java类

  • 基本数据类型:int long short boolean byte double float char

java中static关键字修饰的方法可以直接通过类名来调用

  • 当类中没有定义任何构造方法时,java编译器会为这个类创建缺省构造方法

  • 局部变量可以与类中的成员l变量同名,用this区分即可

  • 局部变量未经赋值,不能使用

  • 若要定义一个类属性或者类方法,应该用static

  • 类的实例方法表示类对对象的行为

  • System不能实例化,即不能创建System类的对象

  • 类,及其属性,方法可以同时有一个以上的修饰符来修饰

  • 一个类的静态方法不可以访问该类的静态成员变量

  • 一个类的非静态方法可以访问该类的静态成员变量

  • 在实例方法或构造器中,this用来引用当前对象,通过使用this可引用当前对象的任何成员

局部内部类

定义在成员方法中的类就是局部类

  • 不能使用任何的访问修饰符

  • 会生成两个.class文件

  • 局部内部类只能访问方法中声明的final类型的变量

  • 局部内部类不能包含静态成员

  • 局部内部类可以访问外部类的所有成员

概述

  • 一个数组不能放不同类型的数值

  • 字节码文件是与平台无关的二进制码,执行时由解释器解释成本地机器码

  • 一个java源文件可以有多个类,但是只能有一个类是public的

  • java允许创建不规则数组,即java多维数组中各行的列数可以不同

  • 当输入为2时,以下方法的返回值是多少

public int getValue( int i ){
	int result = 0;
	switch( i ){
	case 1 :
		result = result + i;
	case 2 :
		result = result + i * 2;
	case 3 :
		result = result + i * 3;
}
	return result;
}

返回值是10,由于case后没有break,故从case2后一直运行

?

public static boolean isOdd( int data ){
	return data % 2 == 0 ? true : false;
}
  • 如果?前为真,则执行冒号前的语句,若为假,则执行冒号后的语句

集合

ArryList

public class Test_str{
	public static void main( String args[] ){
		ArryList arryList = new ArryList();
		arryList.add( "1" );
		arryList.add(2);
		arryList.add(3.3);
		
		//查看
		//方法一
		System.out.println( arryList.get( 0 ) );
		//方法二
		Iterator iterator = arryList.iterator();
		while( iterator.hasNext() ){
			System.out.println( iterator.next() );
		}
		
		//修改
		arryList.set( 0 , 1.1 );
		
		//删除
		arryList.remove( 0 ); //删除下标
		arryList.remove( "1.1" );

		//长度
		System.out.println( arryList.size() );

		//是否包含
		System.out.println( arryList.contains( 3.3 ) ); //是则返回true,否则返回false
	}
}

HashMap

public class Main{
	public static void main( String args[] ){
		HashMap hashMap = new HashMap();
		
		//add
		hashMap.put( "name" , "张三" );
		hashMap.put( "lover" , "自己" );
		
		//searhc
		String name = ( String )hashMap.get( "name" );
		System.out.println( name );
		
		//查询所有的Key
		Set set = hashMap.KeySet();
		System.out.println( set );
	}
}

HashSet

  • 不允许重复元素

  • 没有索引,没有带索引的方法,也不能用普通的for循环遍历

  • 是一个无序的集合,存储元素和取出元素的顺序可能不一致

  • 底层是一个哈希表结构(查询的速度非常的快)

程序题

是否偶数

public static boolean isOdd(int data)

    {

        return data % 2 == 0 ? true : false;
    }

求最大值

import java.util.Scanner;
public  class Main {
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int x=sc.nextInt();
        int y=sc.nextInt();
        int z=x>y?x:y;
        System.out.println(z);
    }
}

统计符合条件元素的个数

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner in=new Scanner(System.in);
        int n = in.nextInt();
        int count=0;
        int sum=0;
        int number=0;
        for(int i=1;i<=n;i++){
            if(i%3==0){
                sum=sum+1;
            }
            if(i%3==0&&i%2==0){
                count=count+1;
            }
        }
        System.out.println((sum-count)+","+count);
    }
}

闰年

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int year = in.nextInt();
        if(year%4==0 && year%100!=0)
            System.out.print("yes");
        else if(year%400==0)
            System.out.print("yes");
        else
            System.out.print("no");
    }
}

一个月总天数

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		int a=in.nextInt();
		int b=in.nextInt();
		if((b%4==0&&b%100!=0)||b%400==0) {
			switch(a){
				case 1:System.out.println("31"); break;
				case 2:System.out.println("29"); break;
				case 3:System.out.println("31"); break;
				case 4:System.out.println("30"); break;
				case 5:System.out.println("31"); break;
				case 6:System.out.println("30"); break;
				case 7:System.out.println("31"); break;
				case 8:System.out.println("31"); break;
				case 9:System.out.println("30"); break;
				case 10:System.out.println("31"); break;
				case 11:System.out.println("30"); break;
				case 12:System.out.println("31"); break;		
			}
		}
		else
		{
			switch(a){
			case 1:System.out.println("31"); break;
			case 2:System.out.println("28"); break;
			case 3:System.out.println("31"); break;
			case 4:System.out.println("30"); break;
			case 5:System.out.println("31"); break;
			case 6:System.out.println("30"); break;
			case 7:System.out.println("31"); break;
			case 8:System.out.println("31"); break;
			case 9:System.out.println("30"); break;
			case 10:System.out.println("31"); break;
			case 11:System.out.println("30"); break;
			case 12:System.out.println("31"); break;		
		}
		}
		
		in.close();

	}

}

统计正数和负数的个数然后计算这些数的平均值

import java.util.Scanner;
public class Main {
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        int positiveNum=0;
        int negativeNum=0;
        int sum=0;
        while(true){
            int x= input.nextInt();
            if(x==0)
                break;
            if(x>0)
                positiveNum++;
            else
                negativeNum++;
            sum+=x;
        }
        if(positiveNum+negativeNum!=0){
            System.out.println(positiveNum);
            System.out.println(negativeNum);
            System.out.println(sum);

            double average=0.0;
            average=1.0*sum/(positiveNum+negativeNum);
            System.out.println(average);
        }
    }
}

程序-直角三角

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int a,b,c;
    a=in.nextInt();
    b=in.nextInt();
    c=in.nextInt();

int aSquare = a*a;
int bSquare = b*b;
int cSquare = c*c;

if ((aSquare + bSquare) == cSquare)
    System.out.println(a*b*1.0/2);
    
else if ((aSquare + cSquare) == bSquare)
    System.out.println(a*c*1.0/2);
    
else if ((bSquare + cSquare) == aSquare)
    System.out.println(b*c*1.0/2);
else   System.out.println("0.0");

	in.close();
	
}
}

输出所有大于平均值的数

import java.util.Scanner;

public class Main {
	public static void main (String[] args) {
		int n,sum=0,count=0;
		float ave=0;
		Scanner in=new Scanner(System.in);
		n=in.nextInt();
		if(n>=1&&n<=10) {
			int i=0;
			int j=0;
			int []a=new int[n];
			for( ;i<n;i++) {
				a[i]=in.nextInt();
				sum+=a[i];
			}
			ave=(float)sum/n;
			System.out.printf("%.2f\n",ave);
			for(;j<n;j++) {
				if(a[j]>ave) {count++;System.out.printf(a[j]+" ");}
			}
			if(count==0) System.out.printf("");}
		else System.out.printf("Invalid.");
		
		}
	}

设计一个矩形类Rectangle

class Rectangle{
    private double width = 1,height = 1;
    public Rectangle(){}
    public Rectangle(double a,double b){
        width = a;
        height = b;
    }
    public double getArea(){
        return width * height;
    }
    public double getPerimeter(){
        return (width + height) * 2;
    }
}

构造函数与toString

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int n = cin.nextInt();
		Person[] p = new Person[n];
		for(int i = 0;i < n;i++) {
			String name = cin.next();
			int age = cin.nextInt();
			boolean gender = cin.nextBoolean();
			p[i] = new Person(name,age,gender);
		}
		for(int i = n - 1;i >=0;i--) {
			System.out.println(p[i].toString());
		}
		
		Person q = new Person();
		System.out.println(q.toString());
	}
}
class Person{
    private String name;
    private int age;
    private boolean gender;
    private int id;
    
    public Person(){
        System.out.println("This is constructor");
        System.out.println(name + "," + age + "," + gender + "," + id);
    }
    public Person(String name,int age,boolean gender){
        this.name = name;
        this.age = age;
        this.gender = gender;
    }
    @Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + ", gender=" + gender + ", id=" + 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 boolean isGender() {
		return gender;
	}
	public void setGender(boolean gender) {
		this.gender = gender;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
    
    
    
}

构造方法

public class Main {
    public Main(){
        System.out.println("构造方法一被调用了");
    }
    public Main(int x){
        System.out.println("构造方法二被调用了");
    }
    public Main(boolean b){
        System.out.println("构造方法三被调用了");
    }
    public static void main(String[] args) {
        Main m = new Main();
        Main d = new Main(4);
        Main v = new Main(true);
    }
}

根据派生写出基类

public String getId(){
	    return id;
	}
public String setId(String id){
	    return this.id=id;
	}

	public void setName(String name){
	    this.name=name;
	}

	public String getName(){
	    return name;
	}

	public People(){
		
	}
	public People(String id,String name) {
		this.id=id;
		this.name=name;
	}
	public void say(){
	    System.out.print("I'm a person! My name is " + this.name + ".");
	    }

写出派生类构造方法

super(id,name);
this.sid =sid;
this.score =score;

重写equals父类

public boolean equals( Object obj ){
	if( this == obj )
	return true;
	if( obj == null )
	return false;
	if( getClass() != obj.getClass() )
	return false;
	Student other = ( Student ) obj;
	if( id == other.id )
	return true;
	else
	return false;
}

覆盖

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Objects;
import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
       Scanner sc=new Scanner(System.in);
       int n1= sc.nextInt();
       boolean f=true;
       sc.nextLine();
       ArrayList<PersonOverride> ap=new ArrayList<PersonOverride>();
        for (int i=0;i<n1;i++){
            ap.add(new PersonOverride());
        }
        int n2= sc.nextInt();
        sc.nextLine();
        for (int i=0;i<n2;i++){
            f=true;
            PersonOverride a=new PersonOverride(sc.next(),sc.nextInt(), sc.nextBoolean());
            sc.nextLine();
            for (PersonOverride aa:ap) {
                if (aa.equals(a)){
                    f=false;
                    break;
                }
            }
            if (f)ap.add(a);
        }

        for(PersonOverride a:ap){
            System.out.println(a.toString().replace("[","").replace("]",""));
        }
        System.out.println(ap.size()-n1);
        System.out.println(Arrays.toString(PersonOverride.class.getConstructors()));
    }

}

class PersonOverride{
    private String name;
    private  int age;
    private boolean gender;

    public PersonOverride() {
        this("default",1,true);
    }

    public PersonOverride(String name, int age, boolean gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
    }


    @Override
    public String toString() {
        return name + "-" + age + "-" + gender;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        PersonOverride that = (PersonOverride) o;
        return age == that.age &&
                gender == that.gender &&
                name.equals(that.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age, gender);
    }
}

从抽象类shape扩展出一个圆形

class Circle extends shape{
	double radius;
	public Circle( double radius ){
	this.radius = radius ;
	}
	@Override
	public double getArea(){
	return Math.PI * radius * radius ;
	}
	public double getPerimeter(){
	return Math.PI  * 2 * radius ;
}
}

创建一个直角三角形类实现IShape接口

class RTriangle implements IShape{
	double a,b;
	
	public RTriangle(double a, double b) {
		super();
		this.a = a;
		this.b = b;
	}
	public  double getArea() {
		return 0.5*a*b;
	}
	public double getPerimeter() {
		return a+b+Math.sqrt(a*a+b*b);
	}
}

面向对象基础-04-形状-继承

import java.util.Scanner;
 
abstract class Shape {
	double PI = 3.14;
 
	public abstract double getPerimeter();
 
	public abstract double getArea();
}
 
class Rectangle extends Shape {
	int wide, len;
 
	Rectangle(int a, int b) {
		wide = a;
		len = b;
	}
 
	@Override
	public double getPerimeter() {
		// TODO Auto-generated method stub
		return 2 * (wide + len);
	}
 
	@Override
	public double getArea() {
		// TODO Auto-generated method stub
		return wide * len;
	}
	
	public String toString(){
		return "[width=" + wide + ", length=" + len + "]";
	}
}
 
class Circle extends Shape {
	int radius;
 
	Circle(int _radius) {
		radius = _radius;
	}
 
	@Override
	public double getPerimeter() {
		// TODO Auto-generated method stub
		return radius * 2 * PI;
	}
 
	@Override
	public double getArea() {
		// TODO Auto-generated method stub
		return radius * radius * PI;
	}
 
	public String toString(){
		return "[radius=" + radius + "]";
	}
 
}
 
public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		in.nextLine();
		Shape A[] = new Shape[n];
		int k = 0, j = 0;
		double sumAllArea = 0, sumAllPerimeter = 0;
		for (int i = 0; i < n; i++) {
			String S = in.next();
			if (S.equals("rect")) {
				int wide = in.nextInt(), len = in.nextInt();
				in.nextLine();
				A[i] = new Rectangle(wide, len);
			} else if (S.equals("cir")) {
				int radius = in.nextInt();
				in.nextLine();
				A[i] = new Circle(radius);
			}
			sumAllArea += A[i].getArea();
			sumAllPerimeter += A[i].getPerimeter();
		}
 
		System.out.println(sumAllPerimeter);
		System.out.println(sumAllArea);
		System.out.print("[");
		for (int i = 0; i < n; i++) {
			if(i != 0)
				System.out.print(", ");
			if (A[i] instanceof Rectangle) {
					System.out.print("Rectangle ");
				System.out.print(A[i].toString());
			}
			else {
					System.out.print("Circle ");
				System.out.print(A[i].toString());	
			}
		}
 
		System.out.println("]");
 
		for(int i = 0;i < n;i++) {
			if(A[i] instanceof Rectangle) {
				System.out.println("class Rectangle,class Shape");
			}else {
				System.out.println("class Circle,class Shape");
 
			}
		}
		in.close();
	}
}

面向对象进阶-04-嵌套类-静态嵌套类

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		int n=in.nextInt();
		
		
		double s[]=new double[n];//为什么不用ArrayUtils[]s=new ArrayUtils[n];因为是double数组
		for(int i=0;i<s.length;i++) {
			double m=in.nextDouble();
			s[i]=m;
		}
		ArrayUtils l=new ArrayUtils();
		double max=s[0];
		double min=s[0];
		
		System.out.println(l.findMinMax(s).toString());
		System.out.println(ArrayUtils.PairResult.class);
	}

}
class ArrayUtils{
	
	static PairResult findMinMax(double[] s)
	{
		PairResult b=new PairResult();
		double min=s[0];
		double max=s[0];
		for(int i=0;i<s.length;i++) {
			if(s[i]>max) {
				max=s[i];
			}
			else if(s[i]<min) {
				min=s[i];
			}
		}
		b.setMax(max);
		b.setMin(min);
		return b;
	}

	static class PairResult{
		private double min;
		private double max;
		@Override
		public String toString() {
			return "PairResult [min=" + min + ", max=" + max + "]";
		}
		public void setMin(double min) {
			this.min = min;
		}
		public void setMax(double max) {
			this.max = max;
		}
		
		
	}
}

集体评分2

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int[] grade = new int[5];
        for(int i=0; i<grade.length; i++){
            grade[i] = in.nextInt();
        }
 
        RR rr = new RT(grade);
        double dd = rr.mark();
        System.out.printf("%.2f",dd);
    }
}
abstract class RR{
    int[] grade;
    public RR(int[] grade){
        this.grade = grade;
    }
    public abstract double mark();
}
class RT extends RR{
    public RT(int[] grade) {
        super(grade);
    }
 
    @Override
    public double mark() {
        double sum=0;
        for (int i=1;i<grade.length-1;i++){
            sum+=grade[i];
        }
        return sum/3;
    }
}

程序改错题2

public class Main {
	 public static void main(String[] args) {
	        Animal animal = new Dog();
	        animal.shout();
	        ((Dog) animal).run();
	    }

}
class Animal {
    void shout() {
        System.out.println("animal shout!");
    }
}

class Dog extends Animal {
    void shout() {
        super.shout();
        System.out.println("wangwang……");
    }

    void run() {
        System.out.println("Dog is running");
    }
}

程序填空题3

public class Main {
	public static void main(String[] args) {
        Son son = new Son();
        son.method();
    }

}
class Parent {
    Parent() {
        System.out.println("Parent's Constructor without parameter");
    }

    Parent(boolean b) {
        System.out.println("Parent's Constructor with a boolean parameter");
    }

    public void method() {
        System.out.println("Parent's method()");
    }
}

class Son extends Parent {
    //补全本类定义
	private static boolean b;//为什么要写static
	Son() 
    {//为什么不写public
		super(b);
		System.out.println("Son's Constructor without parameter");
	}
	public void method() {
		System.out.println("Son's method()");
		System.out.println("Parent's method()");
        
    }
}

接口–四则计算器

import java.util.Scanner;

public class Main{

	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		int n=in.nextInt();
		int m=in.nextInt();
		Add l=new Add();
		Sub j=new Sub();
		System.out.println(l.computer(n, m));
		System.out.println(j.computer(n, m));
		
		in.close();

	}

}
interface IComputer{
	abstract public int computer(int n ,int m);
}
class Add implements IComputer
{
	int m,n;
	public int computer(int n ,int m) {
		return n+m;
	}
}
class Sub implements IComputer
{
	int m,n;
	public int computer(int n ,int m) {
		return n-m;
	}
}

异常

catch(Exception e){
                if (choice.equals("number"))
                    System.out.println ("number format exception");
                if (choice.equals("illegal"))
                    System.out.println ("illegal argument exception");
                if (choice.equals("except"))
                    System.out.println ("other exception");
                System.out.println (e);
            }

成绩录入时的及格与不及格人数统计

import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int count=sc.nextInt();
        int pass=0;
        for (int i=0;i<count;i++){
            int grade=sc.nextInt();
            if (grade<0||grade>100){
                System.out.println(grade+"invalid!");
            }else if (grade>=60){
                pass++;
            }
        }
        System.out.println(pass);
        System.out.println(count-pass);
 
    }
}

object
 int n = sc.nextInt();
        Object [] o = new Object[n];
        
        for( int i = 0; i < n; i ++ ){
            String str = sc.next();
            switch( str ){
                case "c":
                    o[i] = new Computer();
                    break;
                case "d":
                    o[i] = new Double(sc.nextDouble());
                    break;
                case "i":
                    o[i] = new Integer(sc.nextInt());
                    break;
                case "s":
                    o[i] = new String(sc.next());
                    break;
                default:
                    o[i] = null;
            }
        }
        
        for( int i = n - 1; i >= 0; -- i ){
            if( o[i] != null ){
                System.out.println(o[i]);
            }
        }

人口统计

public static int numofHan(String data[]){
	String str = "汉族";
	int ans = 0;
	for(String s: data){
		if(s.indexOf(str, s.lastIndexOf(",")) != -1) {
			++ans;
		}
	}
	return ans;
}

StringBuilder

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		while(scan.hasNext()) {
			int n = scan.nextInt();
			int begin = scan.nextInt();
			int end = scan.nextInt();
			
			StringBuilder string = new StringBuilder();
			for (int i = 0; i < n; i++) {
				string.append(i);
			}
			System.out.println(string.substring(begin,end));
		}
		scan.close();
		
	}
}

单词替换

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        String str1 = sc.nextLine();
        String str2 = sc.nextLine();
        String[] s = str.split(" ");
        for (int i = 0; i < s.length; i++) {
            if (s[i].equals(str1)){
                s[i] = str2;
            }
        }
         String s1 = "";
        for (int i = 0; i < s.length; i++) {
            s1 += s[i] + " ";
        }
        System.out.println(s1.trim());
    }
}

随机数

import java.util.*;
public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		long seed = input.nextLong();
        int n = input.nextInt();
		Random r = new Random(seed);
		int count = 0;
		for (int i=0;i<n;i++) {
			double x = r.nextDouble()*2-1;
			double y = r.nextDouble()*2-1;
			if ( x*x + y*y <=1) count++;
		}
		System.out.println(4*(double)count/n);	
	}
}

List指定元素删除

public static List<String> convertStringToList(String line) {
		List<String> a=new ArrayList<String>();
		String s[]=line.split("\\s+");
		for(int i=0;i<s.length;i++) {
			a.add(s[i]);
		}
		return a;
	}
	public static void remove(List<String> list, String str) {
		for(int i=0;i<list.size();)
        {
			if(list.get(i).equals(str))
            {
				list.remove(i);
				i=0;
			}
            else 
            {
				i++;
			}
		}
	}

ArryList入门

import java.util.*;
public class Main{
 public static void main(String[] args) {
  ArrayList strList = new ArrayList();
  Scanner in = new Scanner(System.in);
  while (true) {
   String s = in.next();
   if (!s.equals("!!end!!")) {
    strList.add(s);
   } 
   else
    break;
  }
  strList.add(0, "begin");
  strList.add("end");
  System.out.println(strList);
  String str = new String();
  str = in.next();
  System.out.println(strList.contains(str));
  System.out.println(strList.indexOf(str));
  System.out.println(strList.lastIndexOf(str));
  System.out.println(strList.get(0));
  strList.remove(0);
  System.out.println(strList);
  str = in.next();
  strList.set(1, str);
  System.out.println(strList);
  ArrayList strList1 = new ArrayList();
  str = in.next();
  in.close();
  for (int j = 0; j < strList.size(); j++) {
   if (((String) strList.get(j)).contains(str)) {
    strList1.add(strList.get(j));
   }
  }
  System.out.println(strList1);
  strList.remove(str);
  System.out.println(strList);
  strList.clear();
  System.out.println(strList + "," + strList.size() + ","+ strList.isEmpty());
 }
}

出勤最多的人

import java.util.*;

class Main{
    public static void main(String[] args) {
       Scanner cin = new Scanner(System.in);
       Map<String, Integer> map = new TreeMap();
       TreeSet ts = new TreeSet();
       int max = 0;

       String str_name = cin.nextLine();
       String []name_str = str_name.split(" ");

       for( int i = 0; i < name_str.length; i ++ ){
           int t = ts.size();
           ts.add(name_str[i]);
           if( t == ts.size() ){ // 表示有重复
               if ( map.get(name_str[i]) + 1 > max){
                   max = map.get(name_str[i]) + 1;
               }
               map.put( name_str[i], map.get(name_str[i]) + 1); // 更新map中的值
           }
           else{
               map.put(name_str[i], 1 );
           }
       }
       String []cnt_name_people = new String[ts.size()];
       int cnt = 0;
       for( Map.Entry<String, Integer> entry: map.entrySet()){
           String key = entry.getKey();
           int value = entry.getValue();

           if( max == 0){// 全部出勤1次,特殊情况
               cnt_name_people[cnt ++ ] = key;
           }
           else if( value == max ){
                cnt_name_people[cnt ++ ] = key;
           }
       }
       for( int i = 0; i < cnt - 1; i ++ ){
           System.out.print(cnt_name_people[i] + " ");
       }
        System.out.println(cnt_name_people[cnt -1 ]);
       cin.close();
    }
}

Thread
class MyThread extends Thread{
	int i;
	public MyThread(int i){
		this.i=i;
	}
	public void run () {
		for(int r=0;r<i;r++) {
			System.out.println(r);
		}
		System.out.println(Thread.currentThread().getName()+" "+isAlive());
	}
	
}

PrintTask
class PrintTask implements Runnable
{
	private int n;
	public PrintTask(int n)
	{
		this.n=n;
	}
	public void run()
	{
		for(int i=0;i<n;i++)
		{
			System.out.println(i);
		}
		System.out.println(Thread.currentThread().getName());
	}
}

倒数计时线程

import java.util.Scanner;

class PrintTask implements Runnable {
	private int n;
	public PrintTask(int n)
	{
		   this.n=n;
	}
	public void run() {
		for(int i=n;i>=0;i--) {
		for(int l=0;l<500;l++);
		System.out.println(i);
	    }
	}
}
public class Main{
	    public static void main(String[] args) {
	    	Scanner in=new Scanner(System.in) ;
	    	PrintTask ta=new PrintTask(in.nextInt());
	    	Thread t=new Thread(ta);
	    	t.start();
	    }
}

程序改错题4

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Thread t = new Thread(new RunHandler());
		t.start();
	}
}

class RunHandler implements Runnable  {
	public void run() {
		Scanner in = new Scanner(System.in);
		int x = in.nextInt();
		for(int i=0;i<x;i++) {
			System.out.println("run");
		}
	}
}