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

日撸代码300行:第11天

代码来自闵老师”日撸 Java 三百行(11-20天)“,链接:https://blog.csdn.net/minfanphd/article/details/116974461

package datastructure.list;/*** Sequential list* * @author WX873**/public class SequentialList {/*** The maximal length of the list. It is a constant.*/public static final int MAX_LENGTH = 10 ;/*** The actual length not exceeding MAX_LENTH. Attention:length is not only * the member variable of sequential list, but also the member variable of*  Array. In fact, a name can be the member variable of different classes.*/int length;/*** The data stored in an Array.*/int data[];/******************************************************** Construct an empty sequential list* ******************************************************/public SequentialList() {length = 0;data = new int[MAX_LENGTH];}//of the first constructor/******************************************************** Construct a sequential list using an array.* * @param paraArray* The given array. Its length should not exceed MAX_LENGTH* FOR simplicity now we do not check it.* ******************************************************/public SequentialList(int[] paraArray) {length = paraArray.length;data = new int[MAX_LENGTH];//copy datafor (int i = 0; i < paraArray.length; i++) {data[i] = paraArray[i];}//of for i}//of the second constructor/*** *********************************************************************** Overrides the method claimed in object, the superclass of any class.* ***********************************************************************/public String toString() {String resultString = "";if (length == 0) {return "empty";}//of iffor (int i = 0; i < data.length - 1; i++) {resultString += data[i] + ",";}//of for i//resultString += data[length - 1];return resultString;}//of toString/*** **************************************************** Reset to empty.* ****************************************************/public void reset() {length = 0;}//of reset/*** *************************************************** The entrance of the program.* ***************************************************/public static void main(String args[]) {int[] tempArray = {1,4,6,9,5,10};SequentialList tempFirstList = new SequentialList(tempArray);System.out.println("Initialized, the list is: " + tempFirstList.toString());System.out.println("Again, the list is: " + tempFirstList);tempFirstList.reset();System.out.println("After reset, the list is: " + tempFirstList);}//of main}//of SequentialList

1.通过本篇博客,学习了构造函数。构造函数是一种特殊的方法,主要用来在创建对象时初始化对象,即为对象成员变量赋初始值。一个类可以有多个构造函数,根据参数个数和参数类型的不同来区分。
(1)构造函数的功能主要用于在类的对象创建时定义初始化的状态,命名必须和类名相同。构造函数没有返回值,也不能被void修饰。
(2)构造函数不能被直接调用,必须通过new运算符在创建对象时才会调用。
2.第二个是遇到的困惑是tostring()方法中,后面的resultString += data[length - 1]意义是什么?注释掉之后运行结果如下:
在这里插入图片描述
不注释这一句代码,会将tempArray = {1,4,6,9,5,10}的最后一个数附在最后,好像并没有实际价值。运行结果如下:
在这里插入图片描述

http://www.xdnf.cn/news/11110.html

相关文章:

  • Oracle数据库常用操作
  • 什么是消费增值!一篇文章带你看懂!
  • WPF语法高亮控件AvalonEdit
  • 目前流行的几种排课算法的介绍
  • zlatan的算法笔记1--回溯中的组合问题(力扣77题)
  • document.all用法(一)
  • chatgpt提示词
  • Debug 命令详解
  • 推荐8个值得每天一看的网站,值得收藏起来!
  • 【体系结构 三 流水线技术】
  • delay的几个函数说明
  • Linux Shell Shock漏洞利用和实战
  • php语法之计算1~100偶数之和
  • c语言-exit函数
  • 【C语言】Windows下的多线程编程-关键段(临界区)
  • LARGE_INTEGER 转换 __int64
  • 进化计算(八)——MOEA/D算法详解Ⅱ
  • 12036 抢票助手开源,星标 12.7K !
  • B2B2C电商app系统开发(java开源,快速搭建)
  • 【一文读懂】什么是磁带存储?为何焕发第二春?
  • 【Laravel系列4.3】模型Eloquent ORM的使用(一)
  • 5000字详解性能需求
  • 滚动条样式修改
  • 内网端口映射工具有哪些?
  • 蝶阀的作用介绍
  • 史上最全网络安全站点集合
  • Java面试宝典
  • 分享126个图片JS特效,总有一款适合您
  • Nios II嵌入式软处理器提升系统性能方式详解
  • Happytime RTSP可用于流式传输各种设备和文