Java基础面试题2

1、用Java实现两个线程分别交替打印数字和字母,打印结果如:1 2 A 3 4 B …… Y 51 52 Z
public class TestThread {
    public static TestThread mHandler=new TestThread();
    public static TestThread getHander(){
        return mHandler;
    }
    public TestThread() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        TestThread t = new TestThread();
        ThreadA threadA = t.new ThreadA();
        ThreadB threadB = t.new ThreadB();
        threadA.start();
        threadB.start();

    }
    
    class ThreadA extends Thread{
        private int num = 1;
        @Override
        public void run() {
            // TODO Auto-generated method stub
            super.run();
            while(num<53){
                synchronized(TestThread.this){  //synchronized(TestThread.getHander()){
                    System.out.print(num+" "+(num+1)+" ");
                    num+=2;
                    TestThread.this.notify();
                    if (num < 52){
                        try {
                            TestThread.this.wait();
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
    
    class ThreadB extends Thread{

        private char ch = 'A';
        @Override
        public void run() {
            // TODO Auto-generated method stub
            super.run();
            while(ch<='Z'){
                synchronized(TestThread.this){  //synchronized(TestThread.getHander()){
                    System.out.print(ch+" ");
                    ch++;
                    TestThread.this.notify();
                    try {
                        TestThread.this.wait();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}

2、将1至100一百个随机自然数,放入数组a中。用java实现获取当中重复次数最多,而且数是最大的一个,并打印出来。

        Random ran = new Random();
        int [] a=new int[100];
        int [] b=new int[100];
        for(int i=0;i<100;i++){
            a[i]=Math.abs(ran.nextInt()%100);
            System.out.print(a[i]+",");
        }
        System.out.println("----");
        for(int i=0;i<100;i++){
            b[a[i]]++;
        }
        for(int i=0;i<100;i++){
            System.out.print(b[i]+",");
        }
        System.out.println("----");
        int k=b[0],max=0;
        for(int i=0;i<100;i++){
            if(b[i]>=k){
                k=b[i];
                max=i;
                System.out.println(max+"----"+k);
            }
        }
        System.out.println(max+"----"+k);



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

寒江蓑笠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值