当前位置:网站首页>Li Kou brush question diary /day2/2022.6.24
Li Kou brush question diary /day2/2022.6.24
2022-07-04 18:35:00 【Bobo toilet cleaning spirit】
Novice Village
Today, I'm still learning about the concept of array , character (String) String array , The creation method is similar to the general array , Just pay attention to the different types
One .List As an interface , Interface cannot be instantiated
List list=new List();
// Will cause compilation errors , This is because List It's an interface , Interface cannot be instantiated .
ArrayList list=new ArrayList();
// This instantiation method is correct . This is because ArrayList Is a class , Inherit and implement List Interface .
How to implement an interface ( With List Interface, for example )
Instantiate the object through the implementation class inherited from this interface List object , Such as
List list=new ArrayList();
This way of instantiation : Created a ArrayList After implementing the object of the class, trace it up to List Interface , Now it's a List Object , There are some ArrayList Yes, but List No attributes and methods , It can't be used anymore .
ArrayList list=new ArrayList();
The above method creates an object and retains ArrayList All properties and methods of .
Comparison of two creation methods
In most cases , What we use is to create a ArrayList The object that implements the class then rises to List Interface , Because the object created by this method is more convenient ,List Interface has multiple implementation classes , Now it's ArrayList, If you want to replace it with other implementation classes , Such as LinkedList perhaps Vector wait , At this time, we just need to change this line : List list = new LinkedList(); Others use list The local code doesn't need to be changed at all . When writing code , More convenient .
But you can only call List Methods in interfaces , Cannot call ArrayList The method in .
List list=new ArrayList();
Two .JAVA String.valueOf() Method instructions
From basic data type to String
int i = 10;
String str = String.valueOf(i);
At this time str It would be “10”
String.valueOf(int i) and Integer.toString(int i) What's the difference? ?
String.valueOf() It can be JAVA Basic types (int,double,boolean etc. ) And the object (Object) convert to String type
toString() It's the method of the object , It can convert this object into String type , The conversion algorithm depends on the actual needs of the type , Basically JAVA Every object in it will have one toString Method .
Of the two The running results are the same , But the principle is different
String.valueOf() It can be applied to any data type , And there will be no abnormal report .
Integer.toString() Say first int convert to Integer type , And then Integer convert to String type .
Add toString Method
//toString(): Return to indicate Integer It's worth it String object .
//toString(int i): Return indicates that the specified int Of String object
commonly String.valueOf() Most of them are used , There is no limit to the type of data applied , And no null pointer exception will be reported
3、 ... and . There are two ways to create an array of strings
// Create an array of strings , This method is more flexible
ArrayList<String> answer = new ArrayList<String>();
// Add string
answer.add("BUFF");
//java in size() Methods are for generic collections , If you want to know how many elements a generic type has , Call this method to see .
answer.size();
// I saw it on Baidu , A very rigid way of writing
String [] answer = new String [20];
// Add an array
answer[0] = "str1";
//java in length() Is for String Speaking of , If you want to see the length of the string , Then use length() Method ;
answer.length()
Four . Example
Their thinking : First create an array object (list), Re pass for Traversal adds a string to the array , If the above requirements are met , be add, If it doesn't fit , Just convert the integer String.valueOf Add to the array as a string
class Solution {
public List<String> fizzBuzz(int n) {
List<String> answer = new ArrayList<String>();
for(int i=1;i<=n;i++){
if(i%3==0&&i%5==0){
answer.add("FizzBuzz");
}
else if(i%3==0){
answer.add("Fizz");
}
else if(i%5==0){
answer.add("Buzz");
}
else{
answer.add(String.valueOf(i));
}
}
return answer;
}
}
边栏推荐
- android使用SQLiteOpenHelper闪退
- TCP两次挥手,你见过吗?那四次握手呢?
- MySQL common add, delete, modify and query operations (crud)
- Behind the ultra clear image quality of NBA Live Broadcast: an in-depth interpretation of Alibaba cloud video cloud "narrowband HD 2.0" technology
- 力扣刷題日記/day6/6.28
- 【系统盘转回U盘】记录系统盘转回U盘的操作
- 爬虫初级学习
- [go language question brushing chapter] go conclusion chapter | introduction to functions, structures, interfaces, and errors
- 表情包坑惨职场人
- ITSS运维能力成熟度分级详解|一文搞清ITSS证书
猜你喜欢
华为云ModelArts的使用教程(附详细图解)
NBA赛事直播超清画质背后:阿里云视频云「窄带高清2.0」技术深度解读
DB-Engines 2022年7月数据库排行榜:Microsoft SQL Server 大涨,Oracle 大跌
如何提高开发质量
线上MySQL的自增id用尽怎么办?
Digital "new" operation and maintenance of energy industry
Weima, which is going to be listed, still can't give Baidu confidence
I always thought that excel and PPT could only be used for making statements until I saw this set of templates (attached)
五千字讲清楚团队自组织建设 | Liga 妙谈
基于NCF的多模块协同实例
随机推荐
What types of Thawte wildcard SSL certificates provide
LD_ LIBRARY_ Path environment variable setting
Open source PostgreSQL extension age for graph database was announced as the top-level project of Apache Software Foundation
如何提高开发质量
Implementation of shell script replacement function
一、C语言入门基础
为啥有些线上演唱会总是怪怪的?
Is it safe to open an account online? is that true?
Summary of subsidy policies across the country for dcmm certification in 2022
[211] go handles the detailed documents of Excel library
Li Kou brush question diary /day8/7.1
基于NCF的多模块协同实例
Li Kou brush question diary /day5/2022.6.27
android使用SQLiteOpenHelper闪退
【2022年江西省研究生数学建模】冰壶运动 思路分析及代码实现
[cloud native] what is the "grid" of service grid?
MySQL常用增删改查操作(CRUD)
大厂面试总结大全二
怎么开户才是安全的,
Li Kou brush question diary /day1/2022.6.23