ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

测试开发面经(一)

2021-12-15 22:31:20  阅读:238  来源: 互联网

标签:Java lock 面经 next 开发 双亲 测试 public 加载


文章目录

但行好事,莫问前程

测试开发面经(一)

​ 阿里的测试开发面试难度较高。

1.Java代码实现LinkedList的add() 和 remove()方法。

PS:要求自行设计LinkedList数据结构,不要外部类库和辅助函数来处理。

LinkedList采用链表存储。

简单的模拟 ,小白写法,其他大佬有更完善的写法。

package DataTest;

public class LinkedListTest {
    Node head;
    Node tail;

    public int counter = 0;
    class Node{
        Node next;
        Integer val;

        public Node(Integer val){
            this.val = val;
        }
    }

    public void add(Integer val){
        if(head == null){
            head = new Node(val);
            tail = head;
            counter++;

        }else{
            Node node = new Node(val);
            tail.next = node;
            tail = tail.next;
            counter++;
        }


    }

    public void remove(int index){
        if(head ==null) return;
        if(index>=counter||index<0){
            System.out.println("数组越界");
            return;
        }
        Node temp = head;
        int num = 0;
//      删除首节点
        if(index == 0){
            head = head.next;
            return;
        }
//      删除指定下标节点
        while (temp.next != null){

            if(num ==index-1){
//                删除尾巴节点
                if(temp.next.next==null){
                    temp.next = null;
                    counter--;
                    return;
                }else{
                    temp.next = temp.next.next;
                    counter--;
                    return;
                }
            }
            num++;


        }
    }


    public static void main(String[] args) {
        LinkedListTest listTest = new LinkedListTest();
        listTest.add(1);
        listTest.add(2);
        listTest.add(3);

        listTest.remove(2);
        listTest.remove(2);
        listTest.remove(3);

    }
}
//console
//数组越界
//数组越界

2. 如何打破Java双亲委派?

2.1双亲委派模型介绍

​ 每⼀个类都有⼀个对应它的类加载器。系统中的 ClassLoder 在协同⼯作的时候会默认使⽤ 双亲委派 模型 。即在类加载的时候,系统会⾸先判断当前类是否被加载过。已经被加载的类会直接返回,否则 才会尝试加载。加载的时候,⾸先会把该请求委派该⽗类加载器的 loadClass() 处理,因此所有的 请求最终都应该传送到顶层的启动类加载器 BootstrapClassLoader 中。当⽗类加载器⽆法处理 时,才由⾃⼰来处理。当⽗类加载器为null时,会使⽤启动类加载器 BootstrapClassLoader 作为 ⽗类加载器

2.2 双亲委派模型带来了什么好处呢?

​ 双亲委派模型保证了Java程序的稳定运⾏,可以避免类的重复加载(JVM 区分不同类的⽅式不仅仅根据 类名,相同的类⽂件被不同的类加载器加载产⽣的是两个不同的类),也保证了 Java 的核⼼ API 不 被篡改。如果不⽤没有使⽤双亲委派模型,⽽是每个类加载器加载⾃⼰的话就会出现⼀些问题,⽐如我 们编写⼀个称为 java.lang.Object 类的话,那么程序运⾏的时候,系统就会出现多个不同的 Object 类。

2.3 如果我们不想⽤双亲委派模型怎么办?

​ 为了避免双亲委托机制,我们可以⾃⼰定义⼀个类加载器,然后重载 loadClass() 即可。

2.4 如何⾃定义类加载器?

​ 除了 BootstrapClassLoader 其他类加载器均由 Java 实现且全部继承⾃ java.lang.ClassLoader 。如果我们要⾃定义⾃⼰的类加载器,很明显需要继承 ClassLoader

3.ConcurrentHashMap 是线程安全的嘛?

​ ConcurrentHashMap的size()方法都是线程安全的,都是准确的计算出实际的数量,但是这个数据在并发场景下是随时都在变的。

4. 用Java实现ArrayBlockingQueue

  1. ArrayBlockingQueue 是阻塞式的队列,底层以循环数组的形式保存数据。常用操作有 add、offer、put、remove、poll、take、peek。

  2. 它有边界,意味着要指定一个队列的大小。

  3. 先进先出。

// 	入队 
	public boolean offer(E e) {
        checkNotNull(e);
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            if (count == items.length)
                return false;
            else {
                enqueue(e);
                return true;
            }
        } finally {
            lock.unlock();
        }
    }

    public void put(E e) throws InterruptedException {
        checkNotNull(e);
        final ReentrantLock lock = this.lock;
        lock.lockInterruptibly();
        try {
            while (count == items.length)
                notFull.await();
            enqueue(e);
        } finally {
            lock.unlock();
        }
    }
//	出队
    public E poll() {
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            return (count == 0) ? null : dequeue();
        } finally {
            lock.unlock();
        }
    }

5. Java实现,有1,2,3,4 这4个数字

5.1 能组成多少个互不相同且无重复数字的三位数?

5.2 都是多少?

package DataTest;

public class NumTest {

    public int numCollection(){
        int[] nums = {1,2,3,4};
        int res =0;
        for(int i =0;i<nums.length;i++){
            for(int j = 0;j<nums.length;j++){
                for(int z = 0;z<nums.length;z++){
                    if(i!=j&&i!=z&&j!=z){
                        System.out.println(nums[i]*100+nums[j]*10+nums[z]);
                        res++;
                    }
                }
            }
        }
        return res;
    }

    public static void main(String[] args) {
        NumTest numTest = new NumTest();
        System.out.println(numTest.numCollection());
    }
}

标签:Java,lock,面经,next,开发,双亲,测试,public,加载
来源: https://blog.csdn.net/gao1213977085/article/details/121963297

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有