Java异常链表throw结构assert详细解读

目录

  • 1、异常的捕获与处理
    • 1.1 处理异常
    • 1.2 异常处理的流程
    • 1.3 throw关键字
    • 1.4 异常处理模型
    • 1.5 RuntimeException
    • 1.6 断言:assert
    • 1.7 自定义异常
  • 2、链表
    • 2.1 链表基本的结构
    • 2.2 链表实现结构说明
    • 2.3 增加链表数据—public void add(数据)
    • 2.4 增加多个数据—public void addAll(数据数组)
    • 2.5 统计数据个数—public int size()
    • 2.6 链表数据转换为对象数组—public Object[] toArray()
    • 2.7 链表查询数据—public boolean contains(查找对象)
    • 2.8 根据索引取得数据—public Object get(int index)
    • 2.9 修改指定索引数据—public void set(int index,Object newData)
    • 2.10 删除数据—public void remove(数据)
  • Link链表类模板
    • 综合案例

      1、异常的捕获与处理 异常时导致程序中断执行的一种指令流。程序出现异常没用合理处理,就会导致程序终止执行。
      观察没有异常产生的程序
      public class TestDemo1{ public static void main(String args[]){System.out.println("1、除法程序开始"); int result =10/2; System.out.println("2、除法程序结果:"+result); System.out.println("3、除法程序结束"); }}/*1、除法程序开始2、除法程序结果:53、除法程序结束*/

      产生异常的程序
      public class YiChang{ public static void main(String args[]){System.out.println("1,除法程序开始"); int result =10/0; //会出现错误System.out.println("2,除法程序结果:"+result); System.out.println("3,除法程序结束"); }}/*Exception in thread "main" java.lang.ArithmeticException: / by zeroat YiChang.main(YiChang.java:5)*/

      这个时候程序出现了错误,那么出现错误之后程序不执行了,而是直接进行了错误信息的输出,并且直接结束了程序,但是,出现了错误,应该去处理才对,但是现在没有处理。

      1.1 处理异常
      现在,如果希望程序出现了异常之后,程序依然可以正常的完成的话,那么就可以使用如下的格式进行异常的处理;
      try{可能出现异常的语句}[catch(异常类型 异常对象){处理异常;}catch(异常类型 异常对象){处理异常;}…][finally{异常统一出口不管是否出现异常,都执行此代码;}]

      使用该操作进行异常的处理
      public class TestDemo1{ public static void main(String args[]){System.out.println("1、除法程序开始"); try{int result =10/0; System.out.println("2、除法程序结果:"+result); }catch(ArithmeticException e){System.out.println("异常被正确的处理了"); }System.out.println("3、除法程序结束"); }}/*1、除法程序开始异常被正确的处理了3、除法程序结束*/

      可以发现加入了异常处理之后,程序之中即使有了异常,程序也可以正常执行完毕,但是现在发现,异常处理时的错误输出信息和之前相比,出错的信息不明确了,那么为了让错误的信息更加完整,一般而言,都会调用printStackTrace()方法进行异常信息的打印
      这个方法(printStackTrace)打印的异常信息是最完整的:
      try{ int x = 10/0; //异常 System.out.println("2,除法程序结果:"+x); }catch(ArithmeticException e){ e.printStackTrace(); }

      try catch finallly 操作
      public class TestDemo1{ public static void main(String args[]){System.out.println("1,除法程序开始"); try{int x = 10/0; //异常System.out.println("2,除法程序结果:"+x); }catch(ArithmeticException e){e.printStackTrace(); }finally{System.out.println("不管是否异常都会执行"); }System.out.println("3,除法程序结束"); }}

      但是,对于之前的程序现在又有了问题:现在执行数学计算的两个参数,都是由程序默认提供,那么如果说现在两个计算的参数通过初始化参数传递呢?
      public class TestDemo2{ public static void main(String args[]){System.out.println("1,除法程序开始"); try{int x = Integer.parseInt(args[0]); //接收参数int y = Integer.parseInt(args[1]); //接收参数int result = x/y; System.out.println("2,除法程序结果:"+result); }catch(ArithmeticException e){e.printStackTrace(); }finally{System.out.println("不管是否异常都会执行"); }System.out.println("3,除法程序结束"); }}

      时候发现,数据由外部传送,那么在这种情况下,就有可能出现一下几类问题:
      ? 执行时不输入参数,ArrayIndexOutOfBoundsException,未处理。
      ? 输入的参数不是数字,NumberFormatException,未处理。
      ? 被除数为0,ArithmeticException,已处理。
      可以发现,以上的程序实际上是存在三种异常,而程序之中只能处理一种,而对于不能处理的异常,发现程序依然会直接中断执行。
      public class TestDemo2{ public static void main(String args[]){System.out.println("1,除法程序开始"); try{int x = Integer.parseInt(args[0]); //接收参数int y = Integer.parseInt(args[1]); //接收参数int result = x/y; System.out.println("2,除法程序结果:"+result); }catch(ArithmeticException e){e.printStackTrace(); }catch(ArrayIndexOutOfBoundsException e){e.printStackTrace(); }catch(NumberFormatException e){e.printStackTrace(); }finally{System.out.println("不管是否异常都会执行"); }System.out.println("3,除法程序结束"); }}

      此时,问题就来了,如果按照以上的方式一次一次的测试来进行异常类型的推断,还不如直接编写if…else。

      1.2 异常处理的流程
      以上已经完成了异常的基本处理流程,但是也可以发现问题,所有的异常都像之前那样一条条的判断似乎是一件不可能完成的任务,因为以后肯定会接触到一些不常见的异常信息,那么下面就必须首先研究异常的流程和结构。
      首先查看两个异常的继承结构
      ArithmeticExceptionjava.lang.Objectjava.lang.Throwablejava.lang.Exception java.lang.RuntimeExceptionjava.lang.ArithmeticExceptionArrayIndexOutOfBoundsExceptionjava.lang.Objectjava.lang.Throwablejava.lang.Exceptionjava.lang.RuntimeExceptionjava.lang.IndexOutOfBoundsExceptionjava.lang.ArrayIndexOutOfBoundsException

      可以发现所有的异常类型最高的继承类是Throwable,通过doc文档可以发现Throwable下有两个子类:
      ? Error:指的是JVM错误,这个时候的程序并没有执行,无法处理;
      ? Exception:指的是程序之中出现了错误信息,可以进行异常处理,主要关心Exception。
      ? 那么通过继承关系可以发现,肯定在进行日常处理的时候是以Exception为主,而这个时候就可以形成以下的异常处理流程。

      1.3 throw关键字
      throws关键字主要是在方法定义上使用的,表示的是此方法之中不进行异常处理,而交给被调用处处理。
      class MyMath{//现在的div()方法之中抛了一个异常出来,表示的是,所有的异常交给被调用处进行处理。 public int div(int x,int y) throws Exception{returnx/y; }}public class TestDemo3{ public static void main(String args[]){try{System.out.println(new MyMath().div(10,0)); }catch(Exception e){e.printStackTrace(); } }}

      在调用throws声明方法的时候,一定要使用异常处理操作进行异常的处理,这是是属于强制性的处理,而现在主方法本身也属于方法,那么实际上在主方法上也可以继续使用throws进行异常的抛出。
      class MyMath{//现在的div()方法之中抛了一个异常出来,表示的是,所有的异常交给被调用处进行处理。 public int div(int x,int y) throws Exception{returnx/y; }}public class TestDemo3{ public static void main(String args[]) throws Exception{try{System.out.println(new MyMath().div(10,0)); }catch(Exception e){e.printStackTrace(); } }}

      这个时候表示的是将异常继续向上抛,交给JVM进行异常的处理。
      请解释throw和throws区别?
      throw用于方法内部表示进行手工的抛出throws主要用于方法声明上使用,明确的告诉本方法可能产生的异常,同时该方法可能不处理异常。

      1.4 异常处理模型
      现在觉得有两个内容实在没用finally,throw。
      现在要求定义一个div()方法,而这个方法有如下一些要求:
      ? 在进行除法操作之前,输出一行提示信息。
      ? 在除法操作执行完毕后,输出一行提示信息。
      ? 如果中间产生了异常,则应该交给被调用处来进行处理。
      class MyMath{ public static int div(int x, int y) throws Exception{int result = 0; //不写catch语句的执行流程//首先进行try代码块的执行后执行finally代码块执行完成后执行//throws进行异常捕获,捕获完成后再主方法catch语句中进行执行try{System.out.println("before进行除法计算"); result = x / y; }finally{System.out.println("after进行除法计算"); }return result; }}public class TestDemo4{ public static void main(String args[]){try{System.out.println(MyMath.div(10,0)); }catch(Exception e){e.printStackTrace(); } }}


      1.5 RuntimeException
      public class TestDemo4{ public static void main(String args[]){String str = "123"; int num = Integer.parseInt(str); System.out.println(num*num); }}

      这个方法就是将一个字符串变为了基本数据类型,而后执行乘法操作,但是下面来看一下parseInt()方法的定义:
      Public static int parseInt(String s) throws NumberFomatException

      发现这个方法上抛出了一个NumberFomatException的异常,按照之前所讲,如果存在了throw,则必须使用try….catch进行处理,可是现在去没有强制要求处理,来观察一下NumberFomatException的继承结构。
      java.lang.Objectjava.lang.Throwablejava.lang.Exceptionjava.lang.RuntimeExceptionjava.lang.IllegalArgumentExceptionjava.lang.NumberFormatException

      发现NumberFormatException属于RuntimeException的子类,而在Java之中明确规定了,对于RuntimeException的异常类型可以有选择型的来进行处理,在开发之中,如果,没有处理,那么出现异常之后将交给JVM默认进行处理。
      Exception和RuntimeException的区别?请你举出常见的RuntimeException?
      • Exception是RuntimeException的父类,使用Exception定义的异常必须使用异常处理
      • RuntimeException可以由用户选择性的异常处理
      • 常见的Exception:NumberFormatException,ClassCastException,NullPointException,ArithmeticException,ArrayIndexOutBoundException。

      1.6 断言:assert
      断言指的是程序执行到某行之后,其结果一定是预期的结果,而在JDK1.4之后增加了一个assert关键字。
      public class DuanYan{ public static void main(String args[]){int x = 10; //假设经过了很多操作assert x == 30:"x的内容不是三十"; System.out.println(x); }}

      默认情况下,Java之中的断言不会在正常执行的代码中出现,如果想要启用断言,则应该早呢更加一些选项。

      1.7 自定义异常
      在Java之中本省已经提供了大量的异常类型,但是在开发之中,这些异常类型根本就不能满足开发的需要,所以在一些系统架构之中往往会提供一些新的异常类型,来表示一些特殊的错误,而这种操作就称为自定义异常类,而要想实现这种自定义异常类,那么可以让一个类继承Exception或RuntimeException。
      class MyException extends Exception{ public MyException(String str){super(str); }}public class TestDemo5{ public static void main(String args[]) throws Exception{throw new MyException("自己的异常类"); }}

      如果以后见到了一些没见过的异常类型,那么基本都是自定义的异常类。

      2、链表 链表是一种基本的数据结构,但好似对于数据结构的部分,强调一下几点:
      在整个Java开发领域之中,没有一本书去真正讲解数据结构的书,只能去看C语言的数据结构:在所有开发之中,都会存在数据结构的身影,可以这样去解释:数据结构的精通与否,完全决定于以后。数据结构的核心:引用数据类型操作。
      链表实际上可以理解为遗传数据,或者按照专业性的说法,可以理解为动态的对象数组,对象数组的最大优点:是表示“多”的概念,例如:多个雇员。但是传统的对象数组有一个最大的问题在于,里面保存的数据长度是固定的。思考:如果现在想要扩大一个对象数组的范围?
      【Java异常链表throw结构assert详细解读】建立一个新的对象数组,而后将原本的内容拷贝到新的数组之中,再改变原数组的引用方式。
      public class TestLinkDemo{ public static void main(String args[]){Object ob[] = new Object [3]; }}

      但是再实际的开发之中,要面临的一个问题是:数组是一个定长的线性结构,也就是说虽然以上代码可以满足于存放多个内容,但是一旦我们呢的内容不足或者是内容过多,可能会导致资源的浪费。要想解决此类问题最好的做法就是不定义一个固定长度的数组 ,有多少数据就保存多少数据。

      2.1 链表基本的结构
      class Node{//因为只有Node类才可以在保存数据的同时设置数据 private Object data; //真正要保存的数据 private Node next; //定义下一个节点 public Node(Object data){this.data = https://www.it610.com/article/data; } public void setData(Object data){this.data = data; } public Object getData(){return this.data; } public void setNext(Node next){this.next = next; } public Node getNext(){return this.next; }}public class TestLinkDemo{ public static void main(String args[]){//1.封装几个节点Node root = new Node("火车头"); Node n1 = new Node("车厢1"); Node n2 = new Node("车厢2"); Node n3 = new Node("车厢3"); //2.设置节点关系root.setNext(n1); n1.setNext(n2); n2.setNext(n3); //3.输出链表print(root); } public static void print(Node node){if(node != null){//表示当前是有节点的System.out.println(node.getData()); print(node.getNext()); //继续向下取出} }}

      在整个链表的实现过程中,Node类的作用:保存数据和保存下一个节点,但是我们发现客户端需要自己来进行节点的创建操作以及关系的配置。所谓的链表就是需要有一个单独的类,假设叫Link,通过Link类来实现Node的数据保存和关系处理。

      2.2 链表实现结构说明
      通过之前的分析,可以发现链表的最大作用类就是Node,但是以上程序都是由用户自己去匹配节点关系的,但是这些节点的匹配工作不应该由用户完成,应该由一个程序专门去负责。
      那么专门负责几点操作的类,就成为链表类——Link,负责处理几点关系,而用户不用关心节点的问题,只需关心Link的处理操作即可。
      Java异常链表throw结构assert详细解读
      文章图片

      真实开发——标准过程
      Java异常链表throw结构assert详细解读
      文章图片

      class Link{//负责对链表的操作 //将Node定义内部类,表示Node类只能为Link类提供服务 private class Node{//负责数据与节点的关系匹配private Object data; //真正要保存的数据private Node next; //定义下一个节点public Node(Object data){this.data = https://www.it610.com/article/data; }public void setData(Object data){this.data = data; }public Object getData(){return this.data; } } //以下为Link类}public class TestLinkDemo{ public static void main(String args[]){ }}


      2.3 增加链表数据—public void add(数据)
      通过上面程序的分析,可以发下,对于链表的实现,Node类是整个操作的关键,但是首先来研究一下之前程序的问题:Node是一个单独的类是可以被用户直接使用的,但是这个类由用户直接去使用,没有任何意义,即:这个类有用,但不能让用户去使用,让Link类去使用。
      class Link{//负责对链表的操作 //将Node定义内部类,表示Node类只能为Link类提供服务 private class Node{//负责数据与节点的关系匹配private Object data; //真正要保存的数据private Node next; //定义下一个节点public Node(Object data){this.data = https://www.it610.com/article/data; }public void setData(Object data){this.data = data; }public Object getData(){return this.data; }public void setNext(Node next){this.next = next; }public Node getNext(){return this.next; }//第一次调用:this = Link.root//第二次调用:this = Link.root.next//第三次调用:this = Link.root.next.nextpublic void addNode(Node newNode){//处理节点关系if(this.next == null){ //当前节点下一个为空this.next = newNode; }else{//当前节点的下一个不为空this.next.addNode(newNode); }}public void nodePrint(){System.out.println(this.getData()); if (this.getNext()==null){return; }else{this.getNext().nodePrint(); }} } //以下为Link类------------------------------------------------ private Node root; //属于根节点,没有根节点就无法数据的保存 //增加数据 public void add(Object data){if(data == null){//人为追加规定,不允许存放null值return ; //结束方法调用}//如果要想进行数据的保存,那么必须将数据封装在Node类里面//如果没有封装,则无法确认好节点的先后顺序Node newNode = new Node(data); if(this.root == null){this.root = newNode; //第一个节点设置为根节点}else{//根节点存在了this.root.addNode(newNode); } } //输出数据 public void print(){if (this.root == null){return; }System.out.println(this.root.getData()); if (this.root.getNext()==null){return; }else{this.root.getNext().nodePrint(); } }}public class TestLinkDemo1{ public static void main(String args[]){Link link = new Link(); link.add("Hello"); link.add("World"); link.print(); }}


      2.4 增加多个数据—public void addAll(数据数组)
      public void addAll(String date[]){ for(int x = 0; x

      2.5 统计数据个数—public int size()
      在Link类中定义
      private int count; //统计个数

      在增加数据的最后一行添加count++
      public void add(Object data){if(data =https://www.it610.com/article/= null){//人为追加规定,不允许存放null值return ; //结束方法调用}//如果要想进行数据的保存,那么必须将数据封装在Node类里面//如果没有封装,则无法确认好节点的先后顺序Node newNode = new Node(data); if(this.root == null){this.root = newNode; //第一个节点设置为根节点}else{//根节点存在了this.root.addNode(newNode); }count++; }


      2.6 链表数据转换为对象数组—public Object[] toArray()
      对于链表的这种数据结构,最为关键的是两个操作:删除和取得全部数据。
      在Link类中定义一个操作数组的脚标:
      private int foot = 0;

      要把数据保存的数组,Link类和Node类都需要使用,那么可以在Link类中定义返回数组,必须以属性的形式出现,只有这样,Node类才可以访问这个数组并进行操作。
      private Object [] retData ; //返回类型

      在Link类中增加toArray()方法:
      //链表数据转换为对象数组public Object[] toArray(){if(this.count == 0){return null; }this.retData = https://www.it610.com/article/new Object[this.count]; this.root.toArrayNode(); this.foot = 0; //下表清零操作return this.retData; }

      在Node中增加toArrayNode()方法:
      public void toArrayNode(){Link.this.retData[Link.this.foot++] = this.data; if(this.next != null){this.next.toArrayNode(); }}

      不过按照以上的方式进行开发,每一次调用toArray()方法,都要重复的进行数据的的遍历,如果在数据没有修改的情况下,这种做法是一种低效的做法,最好的方法是增加一个修改标记,如果发现数据增加了或删除的话,表示要重新遍历数据。
      Java异常链表throw结构assert详细解读
      文章图片


      2.7 链表查询数据—public boolean contains(查找对象)
      现在如果想查询某个数据是否存在,那么基本的操作原理:逐个盘查,盘查的具体对象应该交给Node去完成,前提是:有数据存在。
      在Link类之中,增加查询操作:
      //查找链表的指定数据是否存在public boolean contains(Object search){if(search == null && this.root == null)return false; return this.root.containsNode(search); }

      在Node类中,完成具体查询,查询流程为:
      ? 判断当前节点的内容是否满足于查询内容,如果满足返回ture;
      ? 如果当前节点内容不满足,则向后继续查询,如果没有后续节点了,则返回false。
      public boolean containsNode(Object search){if(search.equals(this.data))return true; else{if(this.next != null){//判断下一个节点是否为空return this.next.containsNode(search); }return false; }}


      2.8 根据索引取得数据—public Object get(int index)
      在一个链表之中会有多个节点保存数据,现在要求可以取得指定节点的数据。但是在进行这一操作的过程之中,有一个小问题:如果要取得数据的索引超过了数据的保存个数,那么是无法取得的。
      ? 在Link类之中增加一个get(int index)方法:
      //根据索引取得数据public Object get(int index){if(index >= this.count){return null; }this.foot = 0; return this.root.getNode(index); }

      在Node类之中增加一个getNdoe(int index)方法:
      //第一次this == Link.root//第二次this == Link.root.nextpublic Object getNode(int index){if(Link.this.foot++ == index){return this.data; }else{return this.next.getNode(index); }}


      2.9 修改指定索引数据—public void set(int index,Object newData)
      如果修改数据只需要进行数据的替换。
      在Link类之中增加一个set(int index,Object newData)方法:
      //修改指定索引数据public void set(int index,Object newData){if(index >= this.count){return ; }this.foot = 0; this.root.setNode(index,newData); }

      在Node类之中增加一个getNode(int index)方法:
      public void setNode(int index, Object newData){if(Link.this.foot ++ == index){//索引相同this.data = https://www.it610.com/article/newData; }else{if(this.next != null){this.next.setNode(index,newData); }}}


      2.10 删除数据—public void remove(数据)
      对于链表之中的内容,之前完成的是增加操作和查询操作,但是从链表之中也会存在删除数据的操作,可是删除数据的操作要分为两种情况讨论:
      ? 情况一:删除的数据不是根节点,待删节点的上一个next指向待删节点的next。
      ? 所有的处理操作应该交给Node进行处理。
      ? 情况二:删除的数据是根节点,下一个节点保存为跟节点。
      ? 如果删除的是根节点,意味着Link中的根节点的保存需要发生变化,该操作主要在Link中处理。
      Java异常链表throw结构assert详细解读
      文章图片

      在Link中增加一个删除remove(Object data)方法
      //删除数据public void remove(Object data){if(this.contains(data)){//如果数据存在则进行数据处理if(this.root.data.equals(data)){//首先需要判断要删除的数据是否为根节点数据this.root = this.root.next; //根节点变为下一个节点}else{//不是根节点this.root.next.removeNode(this.root,data); }this.count --; }}

      在Node类之中增加一个removeNode(Node previous, Object data)方法:
      //第一次:this = Link.root.next、previous= Link.root; //第二次:this = Link.root.next.next、previous= Link.root.next; public void removeNode(Node previous, Object data){if(this.data.equals(data)){//当前节点为要删除的节点previous.next = this.next; }else{this.next.removeNode(this,data); }}


      Link链表类模板
      class Link{//负责链表的操作//将Node定义内部类,表示Node类只能为Link类提供服务 private class Node{//负责数据与节点的关系匹配private Object data; //真正要保存的数据private Node next; //定义下一个节点public Node(Object data){this.data = https://www.it610.com/article/data; }public void setData(Object data){this.data = data; }public Object getData(){return this.data; }public void setNext(Node next){this.next = next; }public Node getNext(){return this.next; }//第一次调用:this = Link.root//第二次调用:this = Link.root.next//第三次调用:this = Link.root.next.nextpublic void addNode(Node newNode){//处理节点关系if(this.next == null){ //当前节点下一个为空this.next = newNode; }else{//当前节点的下一个不为空this.next.addNode(newNode); }}public void nodePrint(){System.out.println(this.getData()); if (this.getNext()==null){return; }else{this.getNext().nodePrint(); }} public void toArrayNode(){Link.this.retData[Link.this.foot++] = this.data; if(this.next != null){this.next.toArrayNode(); }}public boolean containsNode(Object search){if(search.equals(this.data))return true; else{if(this.next != null){//判断下一个节点是否为空return this.next.containsNode(search); }return false; }}//第一次this == Link.root//第二次this == Link.root.nextpublic Object getNode(int index){if(Link.this.foot++ == index){return this.data; }else{return this.next.getNode(index); }}public void setNode(int index, Object newData){if(Link.this.foot ++ == index){//索引相同this.data = newData; }else{if(this.next != null){this.next.setNode(index,newData); }}}//第一次:this = Link.root.next、previous= Link.root; //第二次:this = Link.root.next.next、previous= Link.root.next; public void removeNode(Node previous, Object data){if(this.data.equals(data)){//当前节点为要删除的节点previous.next = this.next; }else{this.next.removeNode(this,data); }} } //以下为Link类------------------------------------------------ private Object [] retData ; //返回类型 private int foot = 0; //操作下标 private int count; //统计个数 private Node root; //属于根节点,没有根节点就无法数据的保存 //增加数据 public void add(Object data){if(data == null){//人为追加规定,不允许存放null值return ; //结束方法调用}//如果要想进行数据的保存,那么必须将数据封装在Node类里面//如果没有封装,则无法确认好节点的先后顺序Node newNode = new Node(data); if(this.root == null){this.root = newNode; //第一个节点设置为根节点}else{//根节点存在了this.root.addNode(newNode); }count++; } //判断链表是否为空 public boolean isEmpty(){this.count=0; return false; } //增加多个数据 public void addAll(String date[]){for(int x = 0; x= this.count){return null; }this.foot = 0; return this.root.getNode(index); } //修改指定索引数据 public void set(int index,Object newData){if(index >= this.count){return ; }this.foot = 0; this.root.setNode(index,newData); } //删除数据 public void remove(Object data){if(this.contains(data)){//如果数据存在则进行数据处理if(this.root.data.equals(data)){//首先需要判断要删除的数据是否为根节点数据this.root = this.root.next; //根节点变为下一个节点}else{//不是根节点this.root.next.removeNode(this.root,data); }this.count --; } }}


      综合案例 建立宠物商店,包括销售宠物上架、下架、关键字查询,要求程序的关系即可,对于宠物的信息只要有三项:名字、年龄、颜色。
      对应的关系:一个宠物商店有多种宠物,如果按照表设计应该属于一对多关系映射,但是现在问题,一方是宠物商店,多方是宠物,但是宠物又分为猫、狗、猪、驴、鱼等。
      Java异常链表throw结构assert详细解读
      文章图片

      1、建立宠物标准
      interface Pet{//定义宠物 public String getName(); public String getColor(); public int getAge(); }

      2、对于宠物商店,只关注于宠物的标准,而不关心具体是那种宠物
      class PetShop{ private Link pets = new Link(); //开辟一个链表,保存宠物信息 public void add(Pet pet){//上架宠物this.pets.add(pet); } public void delete(Pet pet){//下架宠物this.pets.delete(pet); } public Link getPets(){ //得到全部宠物return this.pets; } public Link search(String keyword){//关键字查找Link result = new Link(); Object [] data = https://www.it610.com/article/this.pets.toArray(); for(int i = 0; i < data.length ; i++){Pet pet = (Pet) data[i]; if(pet.getName().contains(keyword) || pet.getColor().contains(keyword)){result.add(pet); //满足查询结果}}return result; }}

      3、定义宠物狗
      class Dog implements Pet{ private String name; private String color; private int age; public String getName(){return this.name; } public String getColor(){return this.color; } public boolean equals(Object obj){if(obj == null){return false; }if(this == obj){return false; }if(!(obj instanceof Dog)){return false; }Dog pet = (Dog) obj; return this.name.equals(pet.name) && this.color.equals(pet.color) && this.age.equals(pet.age); } public int getAge(){return this.age; } public Dog(String name, String color, int age){this.name = name ; this.color = color; this.age = age; } public String toString(){return "【狗】名字 = " + this.name +"颜色 = " + this.color + "年龄 = " +this.age; }}

      4、定义宠物猫
      class Cat implements Pet{ private String name; private String color; private int age; public String getName(){return this.name; } public String getColor(){return this.color; } public boolean equals(Object obj){if(obj == null){return false; }if(this == obj){return false; }if(!(obj instanceof Cat)){return false; }Cat pet = (Cat) obj; return this.name.equals(pet.name) && this.color.equals(pet.color) && this.age.equals(pet.age); } public int getAge(){return this.age; } public Cat(String name, String color, int age){this.name = name ; this.color = color; this.age = age; } public String toString(){return "【猫】名字 = " + this.name +"颜色 = " + this.color + "年龄 = " +this.age; }}

      5、测试类
      public class Pets{ public static void main(String args[]){PetShop ps = new PetShop(); ps.add(new Dog("小黑","黑色",1)); ps.add(new Dog("金毛","金色",2)); ps.add(new Dog("拉布拉多","白色",3)); ps.add(new Dog("萨摩耶","白色",2)); ps.add(new Cat("加菲猫","黄色",3)); ps.add(new Dog("波斯猫","金色",4)); ps.delete(new Dog("萨摩耶","白色",2)); Link all = ps.search("白"); Object [] data = https://www.it610.com/article/all.toArray(); for(int i = 0 ; i < data.length ; i++){System.out.println(data[i]); } }}

      6、完整代码
      class Link{//负责链表的操作//将Node定义内部类,表示Node类只能为Link类提供服务 private class Node{//负责数据与节点的关系匹配private Object data; //真正要保存的数据private Node next; //定义下一个节点public Node(Object data){this.data = https://www.it610.com/article/data; }public void setData(Object data){this.data = data; }public Object getData(){return this.data; }public void setNext(Node next){this.next = next; }public Node getNext(){return this.next; }//第一次调用:this = Link.root//第二次调用:this = Link.root.next//第三次调用:this = Link.root.next.nextpublic void addNode(Node newNode){//处理节点关系if(this.next == null){ //当前节点下一个为空this.next = newNode; }else{//当前节点的下一个不为空this.next.addNode(newNode); }}public void nodePrint(){System.out.println(this.getData()); if (this.getNext()==null){return; }else{this.getNext().nodePrint(); }} public void toArrayNode(){Link.this.retData[Link.this.foot++] = this.data; if(this.next != null){this.next.toArrayNode(); }}public boolean containsNode(Object search){if(search.equals(this.data))return true; else{if(this.next != null){//判断下一个节点是否为空return this.next.containsNode(search); }return false; }}//第一次this == Link.root//第二次this == Link.root.nextpublic Object getNode(int index){if(Link.this.foot++ == index){return this.data; }else{return this.next.getNode(index); }}public void setNode(int index, Object newData){if(Link.this.foot ++ == index){//索引相同this.data = newData; }else{if(this.next != null){this.next.setNode(index,newData); }}}//第一次:this = Link.root.next、previous= Link.root; //第二次:this = Link.root.next.next、previous= Link.root.next; public void removeNode(Node previous, Object data){if(this.data.equals(data)){//当前节点为要删除的节点previous.next = this.next; }else{this.next.removeNode(this,data); }} } //以下为Link类------------------------------------------------ private Object [] retData ; //返回类型 private int foot = 0; //操作下标 private int count; //统计个数 private Node root; //属于根节点,没有根节点就无法数据的保存 //增加数据 public void add(Object data){if(data == null){//人为追加规定,不允许存放null值return ; //结束方法调用}//如果要想进行数据的保存,那么必须将数据封装在Node类里面//如果没有封装,则无法确认好节点的先后顺序Node newNode = new Node(data); if(this.root == null){this.root = newNode; //第一个节点设置为根节点}else{//根节点存在了this.root.addNode(newNode); }count++; } //判断链表是否为空 public boolean isEmpty(){this.count=0; return false; } //增加多个数据 public void addAll(String date[]){for(int x = 0; x= this.count){return null; }this.foot = 0; return this.root.getNode(index); } //修改指定索引数据 public void set(int index,Object newData){if(index >= this.count){return ; }this.foot = 0; this.root.setNode(index,newData); } //删除数据 public void remove(Object data){if(this.contains(data)){//如果数据存在则进行数据处理if(this.root.data.equals(data)){//首先需要判断要删除的数据是否为根节点数据this.root = this.root.next; //根节点变为下一个节点}else{//不是根节点this.root.next.removeNode(this.root,data); }this.count --; } }}interface Pet{//定义宠物 public String getName(); public String getColor(); public int getAge(); }class PetShop{ private Link pets = new Link(); //开辟一个链表,保存宠物信息 public void add(Pet pet){//上架宠物this.pets.add(pet); } public void delete(Pet pet){//下架宠物this.pets.remove(pet); } public Link getPets(){ //得到全部宠物return this.pets; } public Link search(String keyword){//关键字查找Link result = new Link(); Object [] data = https://www.it610.com/article/this.pets.toArray(); for(int i = 0; i < data.length ; i++){Pet pet = (Pet) data[i]; if(pet.getName().contains(keyword) || pet.getColor().contains(keyword)){result.add(pet); //满足查询结果}}return result; }}class Dog implements Pet{ private String name; private String color; private int age; public String getName(){return this.name; } public String getColor(){return this.color; } public boolean equals(Object obj){if(obj == null){return false; }if(this == obj){return false; }if(!(obj instanceof Dog)){return false; }Dog pet = (Dog) obj; return this.name.equals(pet.name) && this.color.equals(pet.color) && this.age == pet.age; } public int getAge(){return this.age; } public Dog(String name, String color, int age){this.name = name ; this.color = color; this.age = age; } public String toString(){return"【狗】名字 = " + this.name +",颜色 = " + this.color + ",年龄 = " +this.age; }}class Cat implements Pet{ private String name; private String color; private int age; public String getName(){return this.name; } public String getColor(){return this.color; } public boolean equals(Object obj){if(obj == null){return false; }if(this == obj){return false; }if(!(obj instanceof Cat)){return false; }Cat pet = (Cat) obj; return this.name.equals(pet.name) && this.color.equals(pet.color) && this.age == pet.age; } public int getAge(){return this.age; } public Cat(String name, String color, int age){this.name = name ; this.color = color; this.age = age; } public String toString(){return "【猫】名字 = " + this.name +",颜色 = " + this.color + ",年龄 = " +this.age; }}public class Pets{ public static void main(String args[]){PetShop ps = new PetShop(); ps.add(new Dog("小黑","黑色",1)); ps.add(new Dog("金毛","金色",2)); ps.add(new Dog("拉布拉多","白色",3)); ps.add(new Dog("萨摩耶","白色",2)); ps.add(new Cat("加菲猫","黄色",3)); ps.add(new Dog("波斯猫","金色",4)); ps.delete(new Dog("萨摩耶","白色",2)); Link all = ps.search("白"); Object [] data = https://www.it610.com/article/all.toArray(); for(int i = 0 ; i < data.length ; i++){System.out.println(data[i]); } }}

      Java异常链表throw结构assert详细解读
      文章图片

      到此这篇关于Day10基础不牢地动山摇-Java基础的文章就介绍到这了,更多相关Java基础内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

        推荐阅读