会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 134310个问题

package cn.bjsxt;

//基于双向链表实现元素存取的容器
public class MyDoubleLinkList<E> implements Myist<E> {


    //记录头结点
    private Node head;
    //记录尾节点
    private Node tail;
    //记录元素个数
    private int size;

    //添加尾节点
    private void lastLinjk(E ele){
        //获取尾节点
        Node t = this.tail;
        //创建节点对象
      Node<E> node = new Node<>(ele,t,null) ;
      //将新节点定义为尾节点
        this.tail = node;
        if(t == null){
            this.head = node;
        }else {
            t.next = node;
        }
        this.size++;
    }
    //定一双向链表的节点对象
    class Node<E>{
        E item;//记录元素
        Node<E> prve;//记录前一个节点对象
        Node<E> next;//记录后一个节点对象
        Node(E item,Node<E> prve,Node<E> next){
            this.item = item;
            this.prve = prve;
            this.next = next;
        }
    }
    //添加元素的方法
    @Override
    public void add(E e) {
        this.lastLinjk(e);
    }
//获取元素的方法
    @Override
    public E get(int index) {
        //对index进行校验
        this.checkIndex(index);
        //根据索引获取节点对象
        Node<E> node = this.getNode(index);
        return node.item;
    }
//获取元素的个数
    @Override
    public int size() {
        return this.size;
    }
//删除元素
    @Override
    public E remove(int index) {
        //对index进行合法校验
        checkIndex(index);
        //根据指定位置获取节点对象
        Node<E> node = this.getNode(index);
        //获取节点对象中的元素
        E e = node.item;
        //判断当前节点是否为头节点
        if(node.prve == null){
            this.head =node.next;
        }else {
            //完成当前节点的直接前驱节点与当前节点的直接后继节点的挂接
            node.prve.next = node.next;
        }
        //判断当前节点是否为子节点
        if(node.next ==null){
            this.tail = node.prve;
        }else {
            //完成当前节点的直接后继节点与当前节点的直接前驱节点的链接
            node.next.prve = node.prve;
        }
        //当前节点断掉与他直接前驱节点的链接
        node.prve = null;
        //当前节点断掉与他直接后继节点的链接
        node.next = null;
        node.item = null;
        //记录元素个数
        this.size--;
        return e;
    }
    //校验index的特性
    private void checkIndex(int index){
        if(!(index >= 0 && index < this.size)){
            throw new IndexOutOfBoundsException();
        }
    }

    //根据索引获得指定对象
    private Node getNode(int index){
        if(index <(this.size>>1)){
            Node node = this.head;
            for (int i=0;i<index;i++){
                node = node.next;
            }
            return node;
        }else{
            Node node = this.tail;
            for (int i = this.size-1; i > index ; i--) {
                node = node.prve;
            }
            return node;
        }
        //在双向链表的头添加元素

    }
    public void addFirst(E e){
        lastLinjk(e);
    }
    //在链表头添加元素
    private void linkFirst(E e){
        //获取头节点对象
        Node head = this.head;

        Node node = new Node(e,null,head);
        //将新节点定义为头结点
        this.head = node;
        //判断当前节点中是否有头结点,如果没有,那么该节点是头结点也是尾节点
        if(head == null){
            this.tail = node;
        }else {
            head.prve = node;
        }
        //记录元素个数
        this.size++;
    }
    //在尾节点添加元素
    public void addLastLink(E e){
        this.lastLinjk(e);
    }
    public static void main(String[] args) {
        Myist<String> myist = new MyDoubleLinkList<>();
        myist.add("a");
        myist.add("b");
        myist.add("v");
        myist.add("d");
        myist.remove(2);
        for (int i = 0; i < myist.size(); i++) {
            System.out.println(myist.get(i));
        }
        System.out.println(myist.size());

        System.out.println("==============================");
        MyDoubleLinkList<String> list = new MyDoubleLinkList<>();
        list.add("a");
        list.addFirst("A");
        list.addLastLink("b");
        for (int i = 0;i<list.size();i++){
            System.out.println(list.get(i));
        }
    }
}
添加头结点元素添加失败?
图片.png

JAVA 全系列/第二阶段:JAVA 基础深化和提高/数据结构 2191楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 2192楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧) 2195楼

GoodTCP.rar

另附上压缩包,

另附上压缩包,


JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 2197楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧) 2198楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/数据结构 2199楼

com.bjsxtjava.io.BufferedReaderjava.io.IOExceptionjava.io.InputStreamReaderjava.io.PrintWriterjava.net.InetSocketAddressjava.net.ServerSocketjava.net.Socketjava.util.ScannerSend1 Thread{
    Socket Scanner (Socket socketScanner scanner){
        .= socket.= scanner}
    () {
        .sendMsg()}
    (){
PrintWriter pw = {
            pw = PrintWriter(..getOutputStream())() {
                String msg = .nextLine()pw.println(msg)pw.flush()}
        }(Exception e){
            e.printStackTrace()}{
            (!= ) {
                .close()}
            (pw != ) {
                pw.close()}
            (.!= ) {
                {
                    ..close()} (IOException e) {
                    e.printStackTrace()}
            }
        }
    }
}
Receive1 Thread{
    Socket (Socket socket){
        .= socket}
    () {
        .receiveMsg()}
    (){
        BufferedReader br = {
            br = BufferedReader(InputStreamReader(..getInputStream()))() {
                String msg = br.readLine()System..println(+msg)}
        }(Exception e){
            e.printStackTrace()}{
            (br != ) {
                {
                    br.close()} (IOException e) {
                    e.printStackTrace()}
            }
            (.!= ) {
                {
                    ..close()} (IOException e) {
                    e.printStackTrace()}
            }
        }
    }
}
GoodTCP {
    (String[] args) {
        Scanner scanner = ServerSocket serverSocket = Socket socket = {
            scanner = Scanner(System.)System..println()String str = scanner.nextLine()String[] arr = str.split()(.equals(arr[])) {
                System..println(+arr[]+)serverSocket = ServerSocket(Integer.(arr[]))socket = serverSocket.accept()System..println()}{
                socket = Socket(arr[]Integer.(arr[]))System..println()}
            Send1(socketscanner).start()Receive1(socket).start()} (Exception e) {
            e.printStackTrace()}{
            (serverSocket != ) {
                {
                    serverSocket.close()} (IOException e) {
                    e.printStackTrace()}
            }
        }
    }
}

报错异常:


请输入:server,<port>或者:<ip>,<port>

127.0.0.1,8888

java.net.ConnectException: Connection refused: connect

at java.net.DualStackPlainSocketImpl.connect0(Native Method)

at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:75)

at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:476)

at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:218)

at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:200)

at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:162)

at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:394)

at java.net.Socket.connect(Socket.java:606)

at java.net.Socket.connect(Socket.java:555)

at java.net.Socket.<init>(Socket.java:451)

at java.net.Socket.<init>(Socket.java:228)

at com.bjsxt.GoodTCP.main(GoodTCP.java:129)


Process finished with exit code 0


JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 2200楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 2201楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 2203楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 2204楼

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 基础深化和提高/数据结构 2205楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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