当前位置:网站首页>Daily code 300 lines learning notes day 11
Daily code 300 lines learning notes day 11
2022-07-01 05:26:00 【Leeyz_ one】
1.Final Variable
①final Keywords can be used for variable declarations , Once the variable is set , You can't change the value of this variable any more . Usually , from final The variables defined are constants . for example , Defined in class PI value , You can use the following statement :
final int x = 1;② stay Java Define global constants in , Usually use public static final modification , Such constants can only be assigned when the definition is .
public static final int A_Data = 3;standard : Is defined as final When defining constants of, you need to name them with uppercase letters , And use underline in the middle to connect .
2. The order sheet
2.1 In the sequence table , There is a member variable called length. The program also has length Represents the length of an integer array . actually , The same variable name can be used by different classes , for example : People have weight , Watermelon also has weight . Because it defines different classes 、 Different objects , There will be no conflict between them . Zhang San's weight 、 Li Si's weight , The association is strange . This description is strange , Obviously, this is the case in real life . But this also reflects the characteristics of object-oriented : It fits our human cognition better than process oriented programming , It is also far away from the bottom of the machine .
2.2 toString This method is very special , It covers Object Class . You can see , stay println It uses tempFirstList in , Because you add another string to it , The system will call it automatically tempFirstList.toString().
Quote from RI Lu Java Three hundred lines (11-20 God , Linear data structure )
3. Code
package demo1;
public class SequentialList {
public static final int MAX_LENGTH = 10;
int length;
int[] data;
public SequentialList() {// Construction method without parameters
length = 0;
data = new int[MAX_LENGTH];
}// of the first constructor
public SequentialList(int[] paraArray) {// Construction method with parameters , take paraArray Assign a value to data
data = new int[MAX_LENGTH];
length = paraArray.length;
// Cope data.
for (int i = 0; i < paraArray.length; i++) {
data[i] = paraArray[i];
} // Of for i
}// Of the second constructor
public String toString() {// rewrite toString Method
String resultString = "";
if (length == 0) {
return "empty";
} // Of if
for (int i = 0; i < length - 1; i++) {
resultString += data[i] + ",";
} // Of for i
resultString += data[length - 1];
return resultString;
}// Of toString
public void reset() {
length = 0;
}// Of reset
public static void main(String args[]) {
int[] tempArray = { 1, 4, 6, 9 };
SequentialList tempFirstList = new SequentialList(tempArray);// Create an object
System.out.println("Initialized,the list is: " + tempFirstList.toString());
System.out.println("Again,the list is: " + tempFirstList);// call println Automatically called toString Method
tempFirstList.reset();
System.out.println("After reset,the list is: " + tempFirstList);
}// of main
}// of class SequentialList
4. summary
In this toString In the cycle of , If you add an equal sign to the original code , And remove the last single output , The output content will be added with a comma .
public String toString() {// rewrite toString Method
String resultString = "";
if (length == 0) {
return "empty";
} // Of if
for (int i = 0; i <= length - 1; i++) {
resultString += data[i] + ",";
} // Of for i
//resultString += data[length - 1];
return resultString;
}// Of toString
Tested repeatedly , If you want to achieve the output of all content , And without one more comma , Just write another sentence to give the last value
resultString += data[length - 1];边栏推荐
- Usage and principle of synchronized
- CockroachDB 分布式事务源码分析之 TxnCoordSender
- QT等待框制作
- 如何开始学剪辑?零基础详细解析
- Distributed architecture system splitting principles, requirements and microservice splitting steps
- 数字金额加逗号;js给数字加三位一逗号间隔的两种方法;js数据格式化
- tar命令
- Data consistency between redis and database
- Day 05 - file operation function
- Leetcode1497- check whether array pairs can be divided by K - array - hash table - count
猜你喜欢

tar命令

Sqlplus connects using the instance name

Vmware workstation network card settings and three common network modes

Go learning notes (5) basic types and declarations (4)

液压滑环的特点讲解

Unity项目心得总结

Practice of combining rook CEPH and rainbow, a cloud native storage solution

Fluentd is easy to use. Combined with the rainbow plug-in market, log collection is faster

Detailed explanation of distributed global unique ID solution

TypeORM 框架
随机推荐
如何开始学剪辑?零基础详细解析
Using nocalhost to develop microservice application on rainbow
How to start learning editing? Detailed analysis of zero basis
eBPF Cilium实战(2) - 底层网络可观测性
[data recovery in North Asia] a data recovery case of raid crash caused by hard disk drop during data synchronization of hot spare disk of RAID5 disk array
Global and Chinese market of high-end home theater 2022-2028: Research Report on technology, participants, trends, market size and share
Unity项目心得总结
3D建模与处理软件简介 刘利刚 中国科技大学
Global and Chinese market for kitchen range hoods 2022-2028: Research Report on technology, participants, trends, market size and share
新手在挖财开通证券账户安全吗?
Tcp/ip explanation (version 2) notes / 3 link layer / 3.2 Ethernet and IEEE 802 lan/man standards
云原生存储解决方案Rook-Ceph与Rainbond结合的实践
Use of STM32 expansion board temperature sensor and temperature humidity sensor
Worried about infringement? Must share copyrightless materials on the website. Don't worry about the lack of materials for video clips
3D建模與處理軟件簡介 劉利剛 中國科技大學
Set集合詳細講解
One click deployment of highly available emqx clusters in rainbow
使用 Nocalhost 开发 Rainbond 上的微服务应用
AcWing 888. Finding combinatorial number IV (the problem of finding combinatorial number with high precision)
Vmware workstation network card settings and three common network modes
