JAVA基础

数组 & 面向对象概述

###图形输出

输出三角形
public class Demo1 {

	public static void main(String[] args) {
		int row = 4;
		for(int i = 1;i <= row;i++){
			for(int a = row - i;a >= 0;a--){
				System.out.print("0");
			}
			for(int b = 1;b <= 2*i-1;b++){
				System.out.print("*");
			}
			System.out.println();
		}
		
	}
}
输出菱形
public class Demo2 {

	public static void main(String[] args) {
		int row = 4;
		for(int i = 1;i <= row;i++){
			for(int a = row - i;a >= 0;a--){
				System.out.print(" ");
			}
			for(int b = 1;b <= 2*i-1;b++){
				System.out.print("*");
			}
			System.out.println();
		}
		for(int i = row;i >= 0;i--){
			for(int a = row;a > i;a--){
				System.out.print(" ");
			}
			for(int b = 2*i;b >= 0;b--){
				System.out.print("*");
			}
			System.out.println();
		}

	}

}

###排序算法(选择)

public class Demo3 {

	public static void main(String[] args) {
		int[] a = {100,40, 60, 87, 34, 11, 56, 0};
		
		for(int i = 0;i<a.length;i++){
			for(int j = i + 1;j < a.length;j++){
				if(a[i] > a[j]){
					int temp = a[i];
					a[i] = a[j];
					a[j] = temp;
				}
			}
			
		}
		for(int n:a){
			System.out.print(n+" ");
		}		

	}

}

对于排序的实现,JDK中自带的Arrays类可以轻松解决:

public class Demo4 {

	public static void main(String[] args) {
		
		int[] a = {100,40, 60, 87, 34, 11, 56, 0};
		Arrays.sort(a);
		for (int i : a) {
			System.out.print(i+" ");
		}
		
	}

}

###折半查找

public class Demo5 {

	public static void main(String[] args) {
		
		int[] a = {0,11,34,40,56,60,87,100};
		int t = 87;
		
		//起始的检索位置
		int start = 0;
		//结束的检索位置
		int end = a.length;
		//中间数位置
		int mid = -1;
		
		while(start <= end){
			//计算获取中间数位置
			mid = (start + end) / 2;
			if(t > a[mid]){
				start = mid;
			}else if(t < a[mid]){
				end = mid;
			}else{
				System.out.println("目标数在数组的位置是:"+mid);
				break;
			}
		}

	}

}

同理,JDK中Arrays类也可以轻松实现折半查找

public class Demo6 {

	public static void main(String[] args) {
		
		int[] a = {0,11,34,40,56,60,87,100};
		int t = 87;
		
		int pos = Arrays.binarySearch(a, t);
		System.out.println(pos);
	}

}

###约瑟夫环

有100个人围成一个圈(编号0-99),从第0号的人开始从1报数,凡报到3的倍数的人离开圈子,然后再数下去,直到最后只剩一个人为止,问此人原来的位置是多少号?

public class Demo7 {

	public static void main(String[] args) {
		//声明布尔类型数组,用于标记这个人是否在圈中
		boolean[] b = new boolean[100];
		//初始化所有人都位于圈中(设置true)
		for(int i = 0;i < b.length;i++){
			b[i] = true;
		}
		
		//初始化圈中的总人数
		int len = b.length;
		//声明计数器,统计是否到达3
		int count = 0;
		//初始化索引,记录当前数到圈中的位置
		int index = 0;
		
		//开始循环报数
		while(len > 1){
			//判断当前位置的人是否在圈中
			if(b[index]){
				//计数器递增
				count++;
				//判断是否到达3
				if(count == 3){
					//人数减少
					len--;
					//标记此人离开圈子
					b[index]=false;
					//计数器归零
					count = 0;
				}
			}
			//数组索引递增
			index++;
			if(index == b.length){
				index = 0;
			}
		}
		
        //循环判断,数组中为true的元素的位置(即为剩下的人原来的位置)
		for(int i = 0;i<b.length;i++){
			if(b[i]){
				System.out.println(i);
				break;
			}
		}
	}
}

多维数组

java中的多维数组即数组中的数组(数组中的每一个元素还是一个数组对象)。

多维数组声明

静态初始化
String[][] citys = {
    {"武汉","荆门","孝感","黄石"},
    {"长沙","湘潭","岳阳"},
    {"广州","东莞","韶关"}
};
动态初始化
//二维数组的动态初始化(只指定行数)
int[][] i = new int[3][];//{}
//为指定的元素赋值(指定列空间)
i[0] = new int[2];
i[0][0] = 1;
二维数组遍历
String[][] citys = {
    {"武汉","荆门","孝感","黄石"},
    {"长沙","湘潭","岳阳"},
    {"广州","东莞","韶关"}
};

for(int i = 0;i < citys.length;i++){
    for(int j = 0;j < citys[i].length;j++){
        System.out.print(citys[i][j]+" ");
    }
    System.out.println();
}

面向对象概述

类与对象

类(Class)

类(Class)是对一类具备相同特征(属性:数据/行为:操作)事物的抽象描述。

类的语法

[修饰符] 类名称{

​ [属性的声明]

​ [方法的声明]

}

public class People{
    
    String id;
    String name;
    String sex;
    int age;
    boolean marray;
    
    public void speak(){
        
    }
    
    public void eat(){
        
    }
    
}
对象(Object)

对象是类中的一个实例,如果说类是抽象的,则对象是具体的.

对象的创建语法

类名称 引用名 = new 构造器();

Student s = new Student();
People p = new People("肉丝")
构造器(Constractor)

在对象创建时,用于完成对于对象的一些初始化操作(赋值,资源的开启)。

语法

[修饰符] 类名([参数列数]){

​ [执行体]

}

public Emp(){
		
}

public Emp(int empno,String job2){
    System.out.println("构造方法被执行");
    eno = empno;
    job = job2;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值