当前位置: 首页 > news >正文

东莞中堂网站建设百度网盘破解版

东莞中堂网站建设,百度网盘破解版,网站前端交互功能案例分析,展台设计搭建1.数组 相同数据类型的集合 数组的长度一旦定义就不能改变 数组中的每一个元素可以用下标表示位置,如果一个数组中有n个元素,那么下标的取值范围是0~n-1. 数组格式 数据类型[] 数组名 new 数据类型[长度]; 数据类型[] 数组名 new 数据类型{元素1&…

1.数组

相同数据类型的集合

数组的长度一旦定义就不能改变

数组中的每一个元素可以用下标表示位置,如果一个数组中有n个元素,那么下标的取值范围是0~n-1.

数组格式

数据类型[] 数组名 = new 数据类型[长度];
数据类型[] 数组名 = new 数据类型{元素1,元素2,...}

一个格式

int[]a = new int[5];

下一个数组定义格式

int [] a =new int[]{111,222,333};

length属性会返回数组长度

 System.out.println(a.length);

全部打印

package com.kjxy.array;public class Test {public static void main(String[] args) {//int[]a = new int[5];//定义一个数组,开辟一个内存时间为10的//数组new出来的也应该在堆内存中int [] a =new int[]{111,222,333};//获取数据长度System.out.println(a.length);}
}

用for语句循环遍历数组长度

package com.kjxy.array;public class Test {public static void main(String[] args) {//int[]a = new int[5];//定义一个数组,开辟一个内存时间为10的//数组new出来的也应该在堆内存中int [] a =new int[]{111,222,333};//获取数据长度for (int i = 0; i <a.length ; i++) {System.out.println(a[i]);}}
}

数组如何在内存当中的实现

接下来我们用实例来演示一下

写一个person类

package com.kjxy.array;public class Person {private String name;private int age;public Person(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}

然后创建一个主程序

package com.kjxy.array;public class Test {public static void main(String[] args) {Person[] ps = new Person[3];ps[0] = new Person("张三",18);ps[1] = new Person("李四",19);ps[2] = new Person("王五",20);for (int i = 0; i < ps.length; i++) {System.out.println(ps[i]);}}
}

输出打印,以为是他们的名字,结果发现是他们的地址,这是调用了toStirng方法

如何实现只输出姓名加年龄呢,此时我们就需要修改一下代码

在person类当中添加一下tostring即可

@Override
public String toString() {return this.name+" " + this.age;
}

发现已经成功打印出姓名加方法了

接下来我们画图来表示其在内存中的演示情况

2.异常

异常是Java用来处理在程序执行期间出现问题的一种机制。有了异常,可以帮助定位程序执行期间出现的问题

1.抛出(throw)与捕获(catch)

抛出:当出现异常时,Java会自动生成一个异常类的对象,该对象中封装着当前出现的问题信息,然后会将该对象交给Java运行 时系统,这个过程叫做抛出(throw)

捕获:Java运行时系统接受异常类的对象,会根据对象的实际情况做出相应处理,这个过程叫做捕获

api文档演示

2.try...catch

try中写的是可能会出现异常的代码

一个try可以对应多个catch

如果执行期间出现异常,会终止后面代码的执行,直接执行对应catch中的代码,如果没有异常,正常顺序执行代码

其中finally当中的代码可写可不写

实例代码

不用try...catch语句

package com.kjxy.ex;public class Test1 {public static void main(String[] args) {int a =5 ;int b =0 ;System.out.println(a / b);}
}

用了语句

package com.kjxy.ex;public class Test1 {public static void main(String[] args) {int a =5 ;int b =0 ;try {System.out.println(a / b);}catch (ArithmeticException e){System.out.println("分母不能为0");}}
}

然后这样可以抛出错误

示例代码

package com.kjxy.ex;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;public class Test2 {public static void main(String[] args) {try {InputStream in = new FileInputStream("abc.txt");} catch (FileNotFoundException e) {throw new RuntimeException(e);}}
}

运行结果,发现系统找不到指定的文件

但是后面继续写代码的话,就不会抛出异常,代码会往下走,只有抛出异常的会出现报错,发现hello world会执行

package com.kjxy.ex;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;public class Test2 {public static void main(String[] args) {try {InputStream in = new FileInputStream("abc.txt");} catch (FileNotFoundException e) {e.printStackTrace();}System.out.println("hello world");}
}

进行演示finally关键字的使用

package com.kjxy.ex;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;public class Test2 {public static void main(String[] args) {try {InputStream in = new FileInputStream("Z:\\Desktop\\All\\Java\\20250612\\src\\com\\kjxy\\ex/abc.txt");} catch (FileNotFoundException e) {throw new RuntimeException(e);}finally {System.out.println("最后一定要执行的内容");}System.out.println("hello world");}
}

利用Java反射机制反射出当前的包

3.throw与throws

throws:声明某个方法可能会抛出哪些异常

throw:抛出某个异常类的对象

package com.kjxy.ex;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;public class Test3 {public static void main(String[] args) {fun1();}public static void fun1(){fun2();}public static void fun2(){fun3();}public static void fun3() throws FileNotFoundException {InputStream in =new FileInputStream("abc");}
}

发现fun3不报错了,但是fun2报错了,谁调用我,谁就去解决问题,或者使用try...catch语句直接使用直接在本行处理也行

发现错误抛出给fun3了

层层向上抛出,抛到Main函数选择用try...catch处理,这样就不会报错了

package com.kjxy.ex;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;public class Test3 {public static void main(String[] args) {try {fun1();} catch (FileNotFoundException e) {throw new RuntimeException(e);}}public static void fun1() throws FileNotFoundException {fun2();}public static void fun2() throws FileNotFoundException {fun3();}public static void fun3() throws FileNotFoundException {InputStream in =new FileInputStream("abc");}
}

此时我们再自定义一个类进行尝试

package com.kjxy.ex;public class Test4 {public static void main(String[] args) {fun(3,5);}public static void fun(int a,int b){if (a<b){throw new MyException("哈哈");}}
}

自定义的异常

package com.kjxy.ex;public class MyException extends Exception{public MyException(String message){super(message);}
}

发现有报错情况

所以这时候我们应该有两种情况,一种try...catch,一种向上抛出

所以我们选择向上抛出

抛出到main函数时我们选择使用try...catch捕获异常

更多的是看Java的api文档则更好解决问题

http://www.zhongyajixie.com/news/28726.html

相关文章:

  • 做的网站怎么上传图片制作网站软件
  • 网站公司怎么做运营网站入口
  • 中国网站备案信息查询一个新的app如何推广
  • 祭奠祭祀网站开发功能需求新浪网今日乌鲁木齐新闻
  • 怎么做网页聊天室南宁seo平台标准
  • 源码出售网站seo快排优化
  • 快速做自适应网站营销策略都有哪些
  • 东台专业做网站的公司品牌营销理论有哪些
  • 域名备案通过后怎么做网站条友网
  • 怎样自己做公司网站百seo排名优化
  • 做大数据和网站开发的前景关键一招
  • 小鱼在线网站建设国家卫生健康委
  • 嘉兴地区有人做网站吗公司网络营销推广方案
  • 阿里云的网站建设好不好黑帽seo技巧
  • 格拉苏蒂手表网站软媒win7优化大师
  • 专业网站优化公司排名女教师遭网课入侵直播录屏曝光视频
  • 网站建设资料企业官网搭建
  • 网站策划报价模板长沙做搜索引擎的公司
  • 建企业网站行业网seo综合查询接口
  • 给公司做网站需要多少钱专业搜索引擎seo服务
  • 邢台营销型网站制作怎么seo关键词优化排名
  • dedecms图片网站模板关键词排名点击软件
  • 变装WordPress关键词优化包含
  • 建设银行的财务网站情感营销
  • 东莞vi设计公司排名seo网络推广哪家专业
  • 宣传类的网站有哪些内容河北百度代理公司
  • 青海公司网站建设哪家快线上宣传渠道
  • 广州网站建设海珠信科中国最新消息
  • 做土建资料有什么网站没英文seo外链发布工具
  • seo公司厦门百度搜索引擎优化的方法