Java入门基础5

1.二分查找

应用:

1.猜数字游戏,随机生成一个1-100的数字

2.求n 的平方根

2.冒泡排序、


例子:

public class java0519{
public static int binarySearch(int[] array, int key){
//二分
for (int i=0,j=array.length-1;j>i;){
	int half=(i+j)/2;
	//int half=i+(i+j)/2;
	if (array[half]>key){
	j=half-1;
	}
	else if (array[half]<key){
	i=half+1;
	}
	else {
		return half;
	}
}
return (-1);
}
public static void swap(int[] array1,int i,int j){
	int t=array1[j];
	array1[j]=array1[i];
	array1[i]=t;
}
public static void bubbleSort(int[] array1 ){
	int count=0;
	int leg=array1.length;
	for (int j=0;j<leg;j++){
		boolean isSwapped=false;//提升冒泡效率
		for (int i=0;i<leg-j-1;i++){
			if (array1[i]>array1[i+1]){
			//count=array1[j];
			//array1[j]=array1[i];
			//array1[i]=count;
			swap(array1,i,i+1);
			isSwapped=true;
			}
		}
		if(!isSwapped){
			return ;
		}
	}
}
public static void main (String[] args){
int [] array={1,3,5,7,9,15,20};
int [] array1=new int[]{9,8,7,6,5};
int index=binarySearch(array,7);
System.out.println("7所在的下标是:"+index);
bubbleSort(array1);

for(int i=0;i<array1.length;i++)
{
	System.out.println(array1[i]);
}

}
}

数组的长度以定义,不能改变

int[] a=null; 引用有但没有实际的数组

int[] a={1,2,3};静态初始化

int[] a=new int[] {1,2,3};更多的使用在匿名数组

int[] a=new int[数组长度];

 

2.Java把内存分为栈和堆

3.方法调用的过程中,无论是基本数据类型还是引用数据类型

实参到形参都是值传递, 在栈里copy一份

但,引用传递,如果如果更改的对象是内部的值,则实参能感受到变化

4.调用栈 的过程

1.作用域和生命周期


类和对象(class And Object)

1.语法     死记

2.内存模型    理解

3.面向对象的优点 

4.如何设计一个类

1.设计模式  23种

面向对象的三大特性

1.封装性    2.继承性    3.多态性

1.如何定义类

[限定符] class 类名称   [继承定义]   [接口实现]   {类体 class body}

public class  className  extends BaseClass implements  lnterface {

属性

方法

内部类

 

}

public class  className {

 

fields;         属性

methods;   方法

 

}

如何根据类创建对象

类名称  变量名;

变量名= new  构造方法 ();

                         和类同名的方法

注意构造方法

1.无返回值

2.方法名称必须是类名称

 

认识第一组限定符

public/private/什么都没有/protected

[限定符] class ClassName {}

public/什么都没有

public class Course 0519{}   //公开的

class Group{}           包(package)

class ClassName {

[限定符]  属性;

[限定符]  方法;

举一个例子

class Group {
	String name ;
	int num;
	Group (String n,int m){
		name=n;
		num=m;
	}
    void print(String welcom ){
	System.out.printf("%s %s 的%d 位同学们 %n",
	welcom,       //形参
	name,          //属性
	num
	);           //属性
	}
	}
	class B{
		int sum;
		B(int a,int b,int c){
			System.out.println(a+b+c);
			sum = a+b+c;
		}
		void print() {
			System.out.println(sum);
		}
	}
public class java20519 {//一个文件中只允许有一个public修饰类,这个类与文件名同名
	
	
	public static void main (String[] args){
	Group group =new Group("computer10",50);
	group.print("欢迎来到中国,");
	B b=new B(1,2,3);
	b.print();
}
}

 


访问限定符

类内部

保内保

不是同一包的其他类代码

public

Y

Y

Y

空白

Y

Y

N

private

Y

N

N

一个文件中只允许有一个public修饰类,这个类与文件名同名


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值