排序
//**冒泡
int[] arr=new int[]{1,34,3,5};
int n=arr.length;
int temp=0;
for(int i=0;i
//**选择排序
function selectSort(int[] arr){
int n =arr.length;
for(int i=0;i
//*插入排序
function InsertSort(int[] arr){
int n=arr.length;
for(int i=1;i=0 && arr[j]>tmp){
arr[j+1]=arr[j];
j--;
}
if(j+i==i){
continue;
}
arr[j+1]=tmp;
}
}
生产者消费者模式---- 传统
class ShareData{
private int number=0;
private Lock lock = new ReentrantLock();
private Condititon condition = lock.newCondition();
public void increment() throws Exception{
lock.lock();
try{
while(number != 0){
condition.await();//等待,不能生产
}
number++;
System.out.println(Thread.currentThread()+"\t"+number);
condition.signlAll();
}catch(Exception e){
e.printStackTrace();
}finally{
lock.unlock();
}
}
public void decrement() throws Exception{
lock.lock();
try{
while(number == 0){
condition.await();//等待,不能生产
}
number--;//干活
System.out.println(Thread.currentThread()+"\t"+number);
condition.signlAll();//通知唤醒
}catch(Exception e){
e.printStackTrace();
}finally{
lock.unlock();
}
}
}
public static void main(String[] args){
ShareData shareData=new ShareData();
new Thread(()->{
for(int i=1;i<=5;i++){
try{
shareData.increment();
}catch(Eception e){
e.printStackTrace();
}
}
},name:"AA").start();
new Thread(()->{
for(int i=1;i<=5;i++){
try{
shareData.decrement();
}catch(Eception e){
e.printStackTrace();
}
}
},name:"BB").start();
}
生产者消费者模式----阻塞队列 ProdConsumer_blockQueueDemo
class MyResource{
private volatile boolean FLAG = true;
private AtomicInteger atomicInteger = new AtomicInteger();
BlockingQueue blockingQueue = null;
public MyResource(BlockingQueue blockingQueue){
this.blockingQueue = blockingQueue;
System.out.println(blockingQueue.getClass().getName());
}
public void myProd() throws Exception{
String data = null;
boolean retValue;
while(FLAG){
data = atomicInteger.incrementAndGet()+"";
retValue = blockingQueue.offer(data,2L,TimeUnit.SECONDS);
if (retValue){
System.out.println(Thread.currentThread().getName()+"\t 插入队列"+data+"成功");
}else{
System.out.println(Thread.currentThread().getName()+"\t 插入队列"+data+"失败");
}
}
TimeUnit.SECONDS.sleep(timeout:1);
}
System.out.println(Thread.currentThread().getName()+"\t 叫停,表示FLAG=false,生产动作结束");
}
public void myConsumer() throws Exception{
String result = null;
while(FLAG){
result= blockingQueue.poll(2L,TimeUnit.SECONDS);
if(null == result || result.equalsIgnoreCase(anotherString:"")){
FLAG = false;
System.out.println(Thread.currentThread().getName()+"\t 超过2秒没有取到,消费退出”);
System.out.println();
return;
}
System.out.println(Thread.currentThread().getName()+"\t 消费队列"+result+“成功”);
}
}
public void stop()throws Exception{
this.FLAG = false;
}
public static void main(String[] args) throws Exception{
MyResource myResource = new MyResource(new ArrayBlockingQueue<>(10));
new Thread(()->{
System.out.println(Thread.currentThread().getName()+"\t 生产线程启动");
try{
myResource.myProd();
}catch(Exception e){
e.printStackTrace();
}
},name:"Prod").start();
new Thread(()->{
System.out.println(Thread.currentThread().getName()+"\t 生产线程启动");
try{
myResource.myProd();
}catch(Exception e){
e.printStackTrace();
}
},name:"Consumer").start();
myResource.stop();
}
//二分法查找
/**
* 测试二分法查找
* 二分法适用于已经排好序的数组
*/
public class TestBinarySearch {
public static void main(String[] args) {
int[] arr= {30,20,50,10,80,9,7,12,100,40,8};
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
System.out.println(myBinarySearch(arr,40));
}
public static int myBinarySearch(int[] arr,int value) {
int low=0;
int high=arr.length-1;
while(low<=high) {
int mid=(low+high)/2;
if(value==arr[mid]) {
return mid;
}
if(value>arr[mid]) {
low=mid+1;
}
if(value
数组的复制、反转、查找(线性查找、二分法查找)
array2=new int[array1.length];
for(int i=0;i
求数值型数组中元素的最大值,最小值,平均数、总和等。
public static void main(){
int[] arr = new int[10];
for(i=0;i arr[i]){
minValue = arr[i];
}
}
System.out.println("最小值:"+minValue);
//平均数、
int avgValue = sum/arr.length;
//总和等
int sum=0;
for(int i=0;i
写一个函数,给你一个字符串 倒序输出来
public String getString(String str){
if(str!=null){
String newStr = "";
for(int i=0;i
不使用中间变量 把两个变量的值互换
int a=10;
int b=100;
a=a*b;
b=a/b;
a=a/b;
System.out.print("a="+a+" b="+b);
输出9*9口诀。
public static void main(String[] args) {
for(int i=1; i<10; i++) {
for(int j=1; j<=i; j++) {
System.out.print(j + "*" + i + "=" + j*i + " " );
if(j*i<10){System.out.print(" ");}
}
System.out.println();
}
}
求1+2!+3!+...+20!的和
public static void main(String[] args) {
long sum = 0;
long fac = 1;
for(int i=1; i<=20; i++) {
fac = fac * i;
sum += fac;
}
System.out.println(sum);
}
题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
1.程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去 掉不满足条件的排列。
public static void main(String[] args){
int i=0;
int j=0;
int k=0;
int t=0;
for(i=1;i<=4;i++)
for(j=1;j<=4;j++)
for(k=1;k<=4;k++)
if(i!=j && j!=k && i!=k)
{t+=1;
System.out.println(i*100+j*10+k);
}
System.out.println (t);
}