会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 133658个问题
JAVA 全系列/第二阶段:JAVA 基础深化和提高/数据结构 2251楼

老师,请问为什么会报错啊

package com.bjsxt;

import java.util.Arrays;
import java.util.EmptyStackException;

/*
自定义栈容器
 */

public class MyStack<E> {
    private Object[] arr;       //存放元素的物理结构

    private int stackLength = 4;   //数组的默认长度

    private int size;          //记录栈容器的元素个数

    private int index = -1;       //操作数组下标的指针


    //判断栈容器是否为空
    public boolean empty() {
        return this.size==0;

    }


    //获取栈顶元素
    public E pop() {
        //如果栈容器中没有元素则抛出异常
        if (this.index == -1) {
            throw new EmptyStackException();
        }
        //记录元素个数
        this.index--;
        //如果有元素,则返回栈顶元素
        return (E) this.arr[index--];
    }


    //向栈顶添加元素
    public E push(E item) {
        //初始化数组
        this.capacity();
        //向数组中添加元素
        this.arr[++index] = item;
        //记录元素个数
        this.size++;
        return item;
    }


    //数组初始化或者以1.5倍容量对数组扩容
    private void capacity() {
        //数组初始化
        if (this.arr == null) {
            this.arr = new Object[this.stackLength];
        }
        //以1.5倍对数组扩容
        if (this.size - this.stackLength >= 0) {
            this.stackLength = this.stackLength + this.stackLength >> 1;   //>>右位移一位等于除以2
            this.arr = Arrays.copyOf(this.arr, this.stackLength);
        }
    }


    public static void main(String[] args) {
        MyStack<String> myStack=new MyStack<>();
        myStack.push("a");
        myStack.push("b");
        myStack.push("c");

        System.out.println(myStack.size);

        System.out.println(myStack.pop());
        System.out.println(myStack.pop());



    }

}


JAVA 全系列/第二阶段:JAVA 基础深化和提高/数据结构 2252楼

老师,请问为什么会报错啊

package com.bjsxt;

/*
基于单向链表
 */
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.next == 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进行校验
     */
    private void checkIndex(int index) {
        if (!(index > 0 && index < size)) {
            throw new IndexOutOfBoundsException();
        }
    }

    /*
    根据位置获取节点
     */

    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 size;
    }

    //根据元素位置删除元素
    @Override
    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> my=new MySinglyLinkedList<>();
        my.add("a");
        my.add("b");
        my.add("c");
        my.add("d");
        System.out.println(my.size());
        System.out.println(my.get(3));
        System.out.println(my.remove(2));
        for(int i=0;i<my.size();i++){
            System.out.println(my.get(i));
        }
    }


}
package com.bjsxt;
/*
基于链表结构存取元素的方法API定义

 */


public interface MyList<E> {
    void add(E element);
    E get(int index);
    int size();
    E remove(int index);


}


JAVA 全系列/第二阶段:JAVA 基础深化和提高/数据结构 2253楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 2254楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 2255楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/异常机制 2258楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 2259楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/XML 技术(旧) 2260楼

MyInteger {
    ;
    MyInteger[] ;

    = -;
    = ;

    {
        (i=;i<=;i++){
            [i+] = MyInteger.(i);
        }
    }

    MyInteger valueOf(i){
        (i>=&&i<=){
            [i+];
        }
        MyInteger(i);
    }

    MyInteger(i){
        .= i;
    }

    main(String[] args) {
        MyInteger m = MyInteger.();

    }
}

我按照老师代码一个不差的敲得,上面是我的代码,但是运行的时候报错了(视频中老师并没有运行这个代码)

运行报错:

image.png

我仔细研究了一下代码,想问一下:

1.静态方法

public static MyInteger valueOf(int i){
    if (i>=LOW&&i<=HIGH){
        return cache[i+128];
    }
    return new MyInteger(i);
}

方法中能new对象吗?关于这个问题我百度了一下,但是并没有查到关于static方法中能不能调用构造函数的问题解答,但是就我理解来讲是不能的,希望老师解答XD。

2.i在【-128,127】区间里可以直接cache【i+128】 =MuInteger.valueOf(i) = cache【i+128】吗,这样不就是一个死循环吗,只是有个256个数组空间但是没有并没有赋东西进去啊,有点搞不懂这个

3.我将

(i=;i<=;i++){
    [i+] = MyInteger.(i);
}

改为

(i=;i<=;i++){
    [i+] = MyInteger(i);
}

运行报错变为:image.png

我不知道是不是问题2的代码问题导致的,还是我对数组赋值部分理解有问题,希望老师可以解答一下,帮忙找下代码错误,我按照老师的代码比对很久了,但是没找到不同的地方,也可能是我自己看自己的代码很难看出错,希望老师看下,辛苦老师了XD

JAVA 全系列/第二阶段:JAVA 基础深化和提高/常用类 2261楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/XML 技术(旧) 2263楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/智能电话本项目实战 2264楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 2265楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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