mirror of https://github.com/Black-Gold/Learn
parent
4f352b421c
commit
7f22d5ed10
@ -0,0 +1,31 @@
|
|||||||
|
public class Jb02_VarDemo1
|
||||||
|
{
|
||||||
|
public static void main(String[] args)
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
byte 1字节
|
||||||
|
boolean 1字节
|
||||||
|
short 2字节
|
||||||
|
char 2字节
|
||||||
|
int 4字节
|
||||||
|
float 4字节
|
||||||
|
long 8字节
|
||||||
|
double 8字节
|
||||||
|
*/
|
||||||
|
//数据类型 变量名 = 初始化值;
|
||||||
|
byte b = 1; //定义变量b,初始化值为1
|
||||||
|
short s = 2;
|
||||||
|
int i = 3;
|
||||||
|
long l = 4;
|
||||||
|
float f = 5.5f;
|
||||||
|
double d = 6.6;
|
||||||
|
char c = 'c';
|
||||||
|
boolean bo = true;
|
||||||
|
bo = false;
|
||||||
|
{
|
||||||
|
int x = 9;
|
||||||
|
System.out.println("x"); //局部变量x
|
||||||
|
}
|
||||||
|
System.out.println(c);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,58 @@
|
|||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class Jb18_ArrSort
|
||||||
|
{
|
||||||
|
public static void main(String[] args)
|
||||||
|
{
|
||||||
|
int[] arr = {71,15,4,50,54,55,5,45,454,496,78,};
|
||||||
|
ergodic(arr);
|
||||||
|
//selectSort(arr); //调用选择排序
|
||||||
|
//bubbleSort(arr); //调用冒泡排序
|
||||||
|
Arrays.sort(arr); //调用Java定义好的排序功能
|
||||||
|
ergodic(arr);
|
||||||
|
}
|
||||||
|
//遍历数组
|
||||||
|
public static void ergodic(int[] arr)
|
||||||
|
{
|
||||||
|
for (int x=0; x<arr.length; x++)
|
||||||
|
{
|
||||||
|
if (x!=arr.length-1)
|
||||||
|
System.out.print(arr[x]+",");
|
||||||
|
else
|
||||||
|
System.out.println(arr[x]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*选择排序
|
||||||
|
public static void selectSort(int[] arr)
|
||||||
|
{
|
||||||
|
for (int x=0;x<arr.length-1 ;x++ )
|
||||||
|
{
|
||||||
|
for (int y=x+1;y<arr.length ; y++)
|
||||||
|
{
|
||||||
|
if (arr[x]>arr[y])
|
||||||
|
{
|
||||||
|
int temp = arr[x];
|
||||||
|
arr[x] = arr[y];
|
||||||
|
arr[y] = temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
//冒泡排序
|
||||||
|
public static void bubbleSort(int[] arr)
|
||||||
|
{
|
||||||
|
for (int x=0; x<arr.length-1; x++)
|
||||||
|
{
|
||||||
|
for (int y=0;y<arr.length-x-1 ;y++ ) //-x代表
|
||||||
|
{
|
||||||
|
if (arr[y]>arr[y+1])
|
||||||
|
{
|
||||||
|
int temp = arr[y];
|
||||||
|
arr[y] = arr[y+1];
|
||||||
|
arr[y+1] = temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,59 @@
|
|||||||
|
public class Jb19_ArrSearch
|
||||||
|
{
|
||||||
|
public static void main(String[] args)
|
||||||
|
{
|
||||||
|
int[] arr = {0,1,2,3,4,5,6,7,8,9};
|
||||||
|
int index = binarySearch_2(arr,7);
|
||||||
|
System.out.println(index);
|
||||||
|
}
|
||||||
|
/*数组查找功能
|
||||||
|
public static int getIndex(int[] arr,int key)
|
||||||
|
{
|
||||||
|
for (int x=0;x<arr.length ;x++ )
|
||||||
|
{
|
||||||
|
if (arr[x]==key)
|
||||||
|
{
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
//二分查找法
|
||||||
|
public static int binarySearch(int arr[],int key)
|
||||||
|
{
|
||||||
|
int max,min,mid;
|
||||||
|
min = 0;
|
||||||
|
max = arr.length-1;
|
||||||
|
mid = (max+min)/2;
|
||||||
|
while (arr[mid]!=key)
|
||||||
|
{
|
||||||
|
if (key>arr[mid])
|
||||||
|
min = mid + 1;
|
||||||
|
else if (key<arr[mid])
|
||||||
|
max = mid - 1;
|
||||||
|
if (max<min)
|
||||||
|
return -1;
|
||||||
|
mid = (max+min)/2;
|
||||||
|
}
|
||||||
|
return mid;
|
||||||
|
}
|
||||||
|
//第二种写法
|
||||||
|
public static int binarySearch_2(int[] arr,int key)
|
||||||
|
{
|
||||||
|
int max,mid,min;
|
||||||
|
min = 0;
|
||||||
|
max = arr.length-1;
|
||||||
|
while (min<=max)
|
||||||
|
{
|
||||||
|
mid = (max+min)>>1; //右移一位相当于除以2的一次幂
|
||||||
|
if(key>arr[mid])
|
||||||
|
min = mid + 1;
|
||||||
|
else if(key<arr[mid])
|
||||||
|
max = mid - 1;
|
||||||
|
else
|
||||||
|
return mid;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
public class Jb21_DataTransmit
|
||||||
|
{
|
||||||
|
//基本数据类型参数传递
|
||||||
|
public static void main(String[] args)
|
||||||
|
{
|
||||||
|
int x = 1;
|
||||||
|
show(x);
|
||||||
|
System.out.println(x); //输出1
|
||||||
|
}
|
||||||
|
public static void show(int x)
|
||||||
|
{
|
||||||
|
x = 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//引用数据类型参数传递
|
||||||
|
class Demo
|
||||||
|
{
|
||||||
|
int x = 1;
|
||||||
|
public static void main(String[] args)
|
||||||
|
{
|
||||||
|
Demo data = new Demo();
|
||||||
|
data.x = 3;
|
||||||
|
show(data);
|
||||||
|
System.out.println(data.x); //输出为2
|
||||||
|
}
|
||||||
|
public static void show(Demo data)
|
||||||
|
{
|
||||||
|
data.x = 2;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
public class Jb23_ArrToolDemo
|
||||||
|
{
|
||||||
|
public static void main(String[] args)
|
||||||
|
{
|
||||||
|
int[] arr = {0,1,32,5,8,4,9};
|
||||||
|
int index = Jb23_ArrTool.getIndex(arr,43); //获取数字43在数组中的角标
|
||||||
|
System.out.println(index);
|
||||||
|
|
||||||
|
int max = getMax(arr);
|
||||||
|
System.out.println("数组最大值是"+max);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
public class Jb24_Single
|
||||||
|
{
|
||||||
|
//单例设计模式
|
||||||
|
//解决问题:确保一个类在内存中的对象唯一性
|
||||||
|
public static void main(String[] args)
|
||||||
|
{
|
||||||
|
//Single sDemo = Single.getInstance();
|
||||||
|
Single sDemo = Single.s; //等同于Single sDemo = Single.getInstance();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Single
|
||||||
|
{
|
||||||
|
static Single s = new single();
|
||||||
|
private Single(){};
|
||||||
|
public static Single getInstance()
|
||||||
|
{
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,48 @@
|
|||||||
|
public class Jb28_AbstractTest
|
||||||
|
{
|
||||||
|
public static void main(String[] args)
|
||||||
|
{
|
||||||
|
|
||||||
|
System.out.println("Hello World!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract class Staff
|
||||||
|
{
|
||||||
|
private String name;
|
||||||
|
private String id;
|
||||||
|
private double pay;
|
||||||
|
Staff(String name,String id,double pay)
|
||||||
|
{
|
||||||
|
this.name = name; //³õʼ»¯
|
||||||
|
this.id = id;
|
||||||
|
this.pay = pay;
|
||||||
|
}
|
||||||
|
public abstract void work();
|
||||||
|
}
|
||||||
|
|
||||||
|
class Programmer extends Staff
|
||||||
|
{
|
||||||
|
Programmer(String name,String id,double pay)
|
||||||
|
{
|
||||||
|
super(name,id,pay);
|
||||||
|
}
|
||||||
|
public void work()
|
||||||
|
{
|
||||||
|
System.out.println("´ò´úÂë");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Manager extends Staff
|
||||||
|
{
|
||||||
|
private int bonus;
|
||||||
|
Manager(String name,String id,double pay,int bonus)
|
||||||
|
{
|
||||||
|
super(name,id,pay);
|
||||||
|
this.bonus = bonus;
|
||||||
|
}
|
||||||
|
public void work()
|
||||||
|
{
|
||||||
|
System.out.println("¹ÜÀíÈËÔ±");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
public class Jb30_Interface2
|
||||||
|
{
|
||||||
|
//实现电脑USB接口功能
|
||||||
|
public static void main(String[] args)
|
||||||
|
{
|
||||||
|
useUSB(new Upan());
|
||||||
|
}
|
||||||
|
public static void useUSB(USB u) //接口类型的引用,用于接收(指向)接口的子类对象
|
||||||
|
{
|
||||||
|
u.open();
|
||||||
|
u.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
interface USB
|
||||||
|
{
|
||||||
|
public void open();
|
||||||
|
public void close();
|
||||||
|
}
|
||||||
|
|
||||||
|
class Upan implements USB
|
||||||
|
{
|
||||||
|
public void open()
|
||||||
|
{
|
||||||
|
System.out.println("开启");
|
||||||
|
}
|
||||||
|
public void close()
|
||||||
|
{
|
||||||
|
System.out.println("关闭");
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue