会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132360个问题
JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 3541楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/Lambda表达式(旧) 3542楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO流技术 3543楼

package com.bjsxt;

/**
 * 基于单向链表实现元素存取的容器
 * @param <E>
 */
public class MySinglyLinkedList<E> implements MyList<E> {
    /**
     * 定义单向链表中的节点对象
     */
    class Node<E>{
        private E item;//存储元素
        private Node next;//存储下一个节点对象的地址
        Node(E item,Node next){
            //传递过来的参数传递给成员变量
            this.item = item;
            this.next = next;
        }
    }
    private Node head;//存放链表中的头节点。
    private int size;//记录元素个数
    @Override
    /**
     * 向链表中添加元素
     */
    public void add(E element) {
        //创建节点
        Node<E> node = new Node<>(element,null);
        //找到未节点
        Node tail = getTail();
        //节点的挂接
        if (tail == null){
            this.head = node;
        }else{
            tail.next=node;
        }
        //记录元素个数
        this.size++;

    }

    /**
     *找到尾节点
     */
    private Node getTail(){
        //头节点是否存在
        if (this.head == null){
            return null;
        }
        //查找尾节点
        Node node = this.head;
        while (true){
            if (node == null){
                break;
            }
            node  = node.next;//移动指针,指向下一个节点
        }
        return node;
    }

    @Override
    /**
     * 根据元素的位置获取元素
     */
    public E get(int index) {
        //校检Index的合法性
        this.checkIndex(index);
        //根据位置获取指定的节点
        Node<E> node = this.getNode(index);
        //将该节点中的元素返回
        return node.item;
    }

    /**
     * 对Index进行校验
     * @return
     */
    private void checkIndex(int index){
        if (!(index >=0 && index < this.size)){
            throw new IndexOutOfBoundsException("Index:"+index+"Size"+this.size);

        }
    }

    /**
     *根据位置获取指定的节点
     * @return
     */
    private Node getNode(int index){
        Node<E> node = this.head;
        for(int i=0;i<index;i++){
            node = node.next;
        }
        return node;
    }

    @Override
    /**
     * 获取元素个数
     */
    public int size() {

        return this.size;
    }

    @Override
    /**
     * 根据元素的位置删除元素
     * @param index
     * @return
     */

    public E remove(int index) {
        //校验Index的合法性
        this.checkIndex(index);
        //根据位置找到该节点对象
        Node<E> node = this.getNode(index);
        //获取该节点对象中的元素
        E item = node.item;
        //将该节点对象从单向表中移除
            //判断当前删除的节点是否为头节点
        if(this.head == node ){
            this.head = node.next;
        }else{
            Node<E> temp = this.head;
            for (int i = 0;i<index-1;i++){
                temp = temp.next;
            }
            temp.next = node.next;
        }
        node.next = null;
        //记录元素个数、
        this.size--;
        //将该元素返回
        return item;
    }

    public static void main(String[] args) {
        MySinglyLinkedList<String> mySinglyLinkedList = new MySinglyLinkedList<>();
        mySinglyLinkedList.add("a");
        mySinglyLinkedList.add("b");
        mySinglyLinkedList.add("c");
        mySinglyLinkedList.add("d");
        System.out.println("size:"+mySinglyLinkedList.size());
        System.out.println("删除:"+mySinglyLinkedList.remove(0));
        for (int i =0;i<mySinglyLinkedList.size();i++){
            System.out.println(mySinglyLinkedList.get(i));

        }
    }
}
package com.bjsxt;

/**
 * 基于链表结构存取元素的方法 API 定义
 * @param <E>
 */
public interface MyList<E> {
    void add(E element);
    E get(int index);
    int size();
    E remove(int index);
}

运行效果图:

老师,1:我明明删除的是0索引,对应的应该是“a”才对怎么给我返回了个“d”啊,

2:我遍历的时候怎么给我报异常啊,明明是对着老师的代码敲的啊

image.png


JAVA 全系列/第二阶段:JAVA 基础深化和提高/数据结构 3544楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/智能电话本项目实战 3545楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/常用类 3546楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/异常机制 3547楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO流技术 3548楼

/**
 *定义馒头类
 */ class ManTou{
    private int id;
    public ManTou(int id){
        this.id = id;
    }
    public int getId(){
        return this.id;

    }
}
/**
 *定义缓冲区类
 */ class SyncStack{
    //定义存放馒头的盒子
    private ManTou[] mt = new ManTou[10];
    //定义操作盒子的索引
    private int index;
    /**
     *放馒头
     */ public synchronized void push(ManTou manTou){
//判断盒子是否已满
        while(this.index == this.mt.length){
            try {
/**
 *语法:wait(),该方法必须要在synchronized块中调用。 * wait执行后,线程会将持有的对象锁释放,并进入阻塞状态,
 *其他需要该对象锁的线程就可以继续运行了。
 */ this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
//唤醒取馒头的线程
/**
 *语法:该方法必须要在synchronized块中调用。

 *该方法会唤醒处于等待状态队列中的一个线程。
 */ this.notify();
        this.mt[this.index] = manTou;
        this.index++;
    }
    /**
     *取馒头
     */ public synchronized ManTou pop(){
        while(this.index == 0){ try {
/**
 *语法:wait(),该方法必须要在synchronized块中调用。 * wait执行后,线程会将持有的对象锁释放,并进入阻塞状态,
 *其他需要该对象锁的线程就可以继续运行了。
 */ this.wait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        }
        this.notify();
        this.index--;
        return this.mt[this.index]; }
}
/**
 *定义生产者线程类
 */ class ShengChan extends Thread{
    private SyncStack ss;
    public ShengChan(SyncStack ss){
        this.ss = ss;
    }
    @Override
    public void run() { for(int i=0;i<10;i++){
        System.out.println("生产馒头:"+i);
        ManTou manTou = new ManTou(i);
        this.ss.push(manTou);
    }
    }
}
/**
 *定义消费者线程类
 */ class XiaoFei extends Thread{
    private SyncStack ss;
    public XiaoFei(SyncStack ss){
        this.ss = ss;
    }
    @Override public void run()
    { for(int i=0;i<10;i++){
        ManTou manTou = this.ss.pop();
        System.out.println("消费馒头:"+i);
    }
    }
}
public class ProduceThread {
    public static void main(String[] args) {
        SyncStack ss = new SyncStack();
        new ShengChan(ss).start();
        new XiaoFei(ss).start();
    }
}
image.png

复制的和自己敲的都这样,为什么?

JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 3551楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 3553楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 3554楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 3555楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637