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

营口网站建设开发制作最新军事新闻12小时

营口网站建设开发制作,最新军事新闻12小时,wordpress调用当前分类名,给有后台的网站做网页1 泛型的引入 问题#xff1a;我们之前实现过的顺序表#xff0c;只能保存 int 类型的元素#xff0c;如果现在需要保存 指向 Person 类型对象的引用的顺序表#xff0c;请问应该如何解决#xff1f;如果又需要保存指向 Book 对象类型的引用呢#xff1f; 之前写的顺序表…1 泛型的引入 问题我们之前实现过的顺序表只能保存 int 类型的元素如果现在需要保存 指向 Person 类型对象的引用的顺序表请问应该如何解决如果又需要保存指向 Book 对象类型的引用呢 之前写的顺序表代码示例 import java.util.Arrays;public class MyArrayList {private int[] elem;private int usedSize;private static int capacity 10;public MyArrayList() {this.elem new int[capacity];}public boolean isFull() {if (this.usedSize capacity) {return true;}return false;}public void add(int pos, int data) {if (pos 0 || pos this.usedSize) {System.out.println(pos位置不合法);return;}if (isFull()) {this.elem Arrays.copyOf(this.elem, 2 * capacity);capacity * 2;}for (int i this.usedSize - 1; i pos; i--) {this.elem[i 1] this.elem[i];}this.elem[pos] data;this.usedSize;}public void display() {for (int i 0; i this.usedSize; i) {System.out.print(this.elem[i] );}System.out.println();}public boolean isEmpty() {if (this.usedSize 0) {return true;}return false;}public boolean contains(int toFind) {if (isEmpty()) {return false;}for (int i 0; i this.usedSize; i) {if (this.elem[i] toFind) {return true;}}return false;}public int search(int toFind) {if (isEmpty()) {return -1;}for (int i 0; i this.usedSize; i) {if (this.elem[i] toFind) {return i;}}return -1;}public int getPos(int pos) {if (isEmpty()) {throw new RuntimeException(顺序表为空);}if (pos 0 || pos this.usedSize) {throw new RuntimeException(pos不合法);}return this.elem[pos];}public int size() {return this.usedSize;}public void setPos(int pos, int value) {if (pos 0 || pos this.usedSize) {System.out.println(pos位置不合法);return;}this.elem[pos] value;}public void remove(int toRemove) {if (isEmpty()) {return;}int index search(toRemove);if (index -1) {System.out.println(没有你要删除的数字);}for (int i index; i this.usedSize - 1; i) {this.elem[i] this.elem[i 1];}this.usedSize--;}public void clear() {for (int i 0; i this.usedSize; i) {this.elem[i] 0;}this.usedSize 0;}public static void main(String[] args) {MyArrayList myArrayList new MyArrayList();myArrayList.add(0, 1);myArrayList.add(1, 2);myArrayList.add(2, 3);myArrayList.add(3, 4);System.out.println(myArrayList.size());myArrayList.display();System.out.println(myArrayList.contains(3));System.out.println(myArrayList.contains(2));System.out.println(myArrayList.search(5));System.out.println(myArrayList.search(2));System.out.println(myArrayList.getPos(0));System.out.println(myArrayList.usedSize);myArrayList.display();myArrayList.remove(1);myArrayList.remove(2);myArrayList.display();myArrayList.remove(4);myArrayList.display();myArrayList.clear();System.out.println();myArrayList.display();} } 首先我们在学习多态过程中已知一个前提父类的引用可以指向子类的对象。其次我们也已知 Object 是 java 中所有类的祖先类   那么要解决上述问题我们很自然的想到一个解决办法将我们的顺序表的元素类型定义成 Object 类型这样我们的 Object 类型的引用可以指向 Person 类型的对象或者指向 Book 类型的对象了。 因为代码改动较多现在指出主要代码 这样我们可以就可以很自由的存储指向任意类型对象的引用到我们的顺序表了。   改编后的代码  package test1;import java.util.Arrays;public class MyArrayList {private Object[] elem;private int usedSize;private static int capacity 10;public MyArrayList() {this.elem new Object[capacity];}public boolean isFull() {if (this.usedSize capacity) {return true;}return false;}public void add(int pos, Object data) {if (pos 0 || pos this.usedSize) {System.out.println(pos位置不合法);return;}if (isFull()) {this.elem Arrays.copyOf(this.elem, 2 * capacity);capacity * 2;}for (int i this.usedSize - 1; i pos; i--) {this.elem[i 1] this.elem[i];}this.elem[pos] data;this.usedSize;}public void display() {for (int i 0; i this.usedSize; i) {System.out.print(this.elem[i] );System.out.println();}System.out.println();}public boolean isEmpty() {if (this.usedSize 0) {return true;}return false;}public boolean contains(Object toFind) {if (isEmpty()) {return false;}for (int i 0; i this.usedSize; i) {if (this.elem[i] toFind) {return true;}}return false;}public int search(Object toFind) {if (isEmpty()) {return -1;}for (int i 0; i this.usedSize; i) {if (this.elem[i] toFind) {return i;}}return -1;}public Object getPos(int pos) {if (isEmpty()) {throw new RuntimeException(顺序表为空);}if (pos 0 || pos this.usedSize) {throw new RuntimeException(pos不合法);}return this.elem[pos];}public int size() {return this.usedSize;}public void setPos(int pos, int value) {if (pos 0 || pos this.usedSize) {System.out.println(pos位置不合法);return;}this.elem[pos] value;}public void remove(int toRemove) {if (isEmpty()) {return;}int index search(toRemove);if (index -1) {System.out.println(没有你要删除的数字);}for (int i index; i this.usedSize - 1; i) {this.elem[i] this.elem[i 1];}this.usedSize--;}public void clear() {for (int i 0; i this.usedSize; i) {this.elem[i] 0;}this.usedSize 0;}public static void main(String[] args) {MyArrayList books new MyArrayList();for (int i 0; i 10; i) {books.add(i,new Book(三国演义, 罗贯中, 15));}books.display(); // MyArrayList myArrayList new MyArrayList(); // myArrayList.add(0, 1); // myArrayList.add(1, 2); // myArrayList.add(2, 3); // myArrayList.add(3, 4); // System.out.println(myArrayList.size()); // myArrayList.display(); // System.out.println(myArrayList.contains(3)); // System.out.println(myArrayList.contains(2)); // System.out.println(myArrayList.search(5)); // System.out.println(myArrayList.search(2)); // System.out.println(myArrayList.getPos(0)); // System.out.println(myArrayList.usedSize); // myArrayList.display(); // myArrayList.remove(1); // myArrayList.remove(2); // myArrayList.display(); // myArrayList.remove(4); // myArrayList.display(); // myArrayList.clear(); // System.out.println(); // myArrayList.display();} } 遗留问题现在的 MyArrayList 虽然可以做到添加任意类型的引用到其中了但遇到以下代码就会产生问题。 写一个Person类 接下来我称之为牛马操作 编译竟然正确没有报红我们运行一下看看 运行时会抛出了异常   提示问题暴露的越早影响越小。编译期间的问题只会让开发者感觉到运行期间的错误会让所有的软件使用者承受错误风险。 所以我们需要一种机制可以 1. 增加编译期间的类型检查 2. 取消类型转换的使用 泛型就此诞生   2.泛型的分类 1. 泛型类2. 泛型方法 3 泛型类的定义的简单演示 注意 泛型类可以一次有多个类型变量用逗号分割。   4 泛型背后作用时期和背后的简单原理 1. 泛型是作用在编译期间的一种机制即运行期间没有泛型的概念。2. 泛型代码在运行期间就是我们上面提到的利用 Object 达到的效果这里不是很准确以后会做说明。   5 泛型类的使用 通过以上代码我们可以看到泛型类的一个使用方式只需要在所有类型后边跟尖括号并且尖括号内是真正的类型即 E 可以看作的最后的类型。   看具体代码 package test1;import java.util.Arrays;public class MyArrayListE {private Object[] elem;private int usedSize;private static int capacity 10;private E e;public MyArrayList() {this.elem new Object[capacity];this.e e;}public boolean isFull() {if (this.usedSize capacity) {return true;}return false;}public void add(int pos, E data) {if (pos 0 || pos this.usedSize) {System.out.println(pos位置不合法);return;}if (isFull()) {this.elem Arrays.copyOf(this.elem, 2 * capacity);capacity * 2;}for (int i this.usedSize - 1; i pos; i--) {this.elem[i 1] this.elem[i];}this.elem[pos] data;this.usedSize;}public void display() {for (int i 0; i this.usedSize; i) {System.out.print(this.elem[i] );System.out.println();}System.out.println();}public boolean isEmpty() {if (this.usedSize 0) {return true;}return false;}public boolean contains(E toFind) {if (isEmpty()) {return false;}for (int i 0; i this.usedSize; i) {if (this.elem[i] toFind) {return true;}}return false;}public int search(E toFind) {if (isEmpty()) {return -1;}for (int i 0; i this.usedSize; i) {if (this.elem[i] toFind) {return i;}}return -1;}public Object getPos(int pos) {if (isEmpty()) {throw new RuntimeException(顺序表为空);}if (pos 0 || pos this.usedSize) {throw new RuntimeException(pos不合法);}return this.elem[pos];}public int size() {return this.usedSize;}public void setPos(int pos, int value) {if (pos 0 || pos this.usedSize) {System.out.println(pos位置不合法);return;}this.elem[pos] value;}public void remove(E toRemove) {if (isEmpty()) {return;}int index search(toRemove);if (index -1) {System.out.println(没有你要删除的数字);}for (int i index; i this.usedSize - 1; i) {this.elem[i] this.elem[i 1];}this.usedSize--;}public void clear() {for (int i 0; i this.usedSize; i) {this.elem[i] 0;}this.usedSize 0;}public static void main(String[] args) {MyArrayListBook books new MyArrayListBook();books.add(0, new Book(红楼梦, 曹雪芹, 18));books.add(2, new Person(lisi, lll));// Book book1 new Book(红楼梦, 曹雪芹, 18); // for (int i 0; i 10; i) { // if (i 0) { // books.add(i, book1); // } else { // books.add(i, new Book(三国演义, 罗贯中, 15)); // } // } // books.display(); // Person person (Person) books.getPos(0); // MyArrayList myArrayList new MyArrayList(); // myArrayList.add(0, 1); // myArrayList.add(1, 2); // myArrayList.add(2, 3); // myArrayList.add(3, 4); // System.out.println(myArrayList.size()); // myArrayList.display(); // System.out.println(myArrayList.contains(3)); // System.out.println(myArrayList.contains(2)); // System.out.println(myArrayList.search(5)); // System.out.println(myArrayList.search(2)); // System.out.println(myArrayList.getPos(0)); // System.out.println(myArrayList.usedSize); // myArrayList.display(); // myArrayList.remove(1); // myArrayList.remove(2); // myArrayList.display(); // myArrayList.remove(4); // myArrayList.display(); // myArrayList.clear(); // System.out.println(); // myArrayList.display();} } 那么现在就会出现编译错误 这就是泛型的作用 注意 Book 只能想象成 E 的类型但实际上 E 的类型还是 Object。   6 泛型总结 1. 泛型是为了解决某些容器、算法等代码的通用性而引入并且能在编译期间做类型检查。2. 泛型利用的是 Object 是所有类的祖先类并且父类的引用可以指向子类对象的特定而工作。3. 泛型是一种编译期间的机制即 MyArrayListPerson 和 MyArrayListBook 在运行期间是一个类型。4. 泛型是 java 中的一种合法语法标志就是尖括号
http://www.eeditor.cn/news/120023/

相关文章:

  • 网站添加悬浮二维码品牌网站解决方案
  • 易语言 网站开发推广普通话手抄报简单又好看
  • 苏州企业建站公司wordpress添加悬浮按钮
  • 回龙观做网站网络营销的内容主要包括哪些方面
  • 关于销售网站有哪些内容asp企业网站开发技术
  • 免费网站模板大全网站建设能力
  • 秦皇岛做网站的公司百度浏览器广告怎么投放
  • 厦门专业网站设计微信公众平台网页
  • 建设企业学习网站pc网站建设的优势是什么
  • 注册公司去哪个网站做网站编辑的时候没保存怎么
  • 网站建设培训资料江门公司做网站
  • 企业网站维护建设ppt东莞外发加工货源网
  • 越南的网站建设传奇手游源码网
  • 伍佰亿营销型网站注册的空间网站吗
  • 长春网站开发招聘网站的策划做推广
  • 怎么做提卡密网站苏州做手机网站
  • 衡阳建网站技术支持 鼎维重庆网站建设专家
  • 微网站二级页面怎么做洛阳做网站公司汉狮价格
  • 个人博客网站建设业务网站设计 cdc
  • 龙岗做网站公司苏州网络营销网站建设平台
  • 北京网站优化服务有限公司织梦批量修改网站源代码
  • 智能建站系统怎么更换网站模板创新的企业网站开发
  • 请人做游戏的网站英文官网建设
  • 浙江省建设局网站wordpress双击图片放大
  • 临沂网站优化互联网营销师报名入口官网
  • 网站开发语言格式化标记语言公司网站建设项目目的
  • 零食网站色调搭配怎麽做怎样做网络推广渠道
  • 公司网站怎么优化深圳建设交易集团
  • aspit网站源码带手机版xx集团门户网站建设策划方案
  • 建设部网站查询造价师证件长垣县建站塔山双喜