当前位置:网站首页>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;
}
}
边栏推荐
- 基于NCF的多模块协同实例
- Self reflection of a small VC after two years of entrepreneurship
- Unity makes revolving door, sliding door, cabinet door drawer, click the effect of automatic door opening and closing, and automatically play the sound effect (with editor extension code)
- Redis主从复制
- Just today, four experts from HSBC gathered to discuss the problems of bank core system transformation, migration and reconstruction
- uni-app与uviewUI实现仿小米商城app(附源码)
- 庆贺!科蓝SUNDB与中创软件完成七大产品的兼容性适配
- 2022年全国CMMI认证补贴政策|昌旭咨询
- 【211】go 处理excel的库的详细文档
- 【Hot100】32. Longest valid bracket
猜你喜欢

Just today, four experts from HSBC gathered to discuss the problems of bank core system transformation, migration and reconstruction

Microservice architecture debate between radical technologists vs Project conservatives

力扣刷题日记/day7/6.30

NBA赛事直播超清画质背后:阿里云视频云「窄带高清2.0」技术深度解读
12 - explore the underlying principles of IOS | runtime [isa details, class structure, method cache | t]

With the stock price plummeting and the market value shrinking, Naixue launched a virtual stock, which was deeply in dispute

ISO27001认证办理流程及2022年补贴政策汇总
![[HCIA continuous update] network management and operation and maintenance](/img/a4/406b145793b701b001f04c7538dab3.png)
[HCIA continuous update] network management and operation and maintenance

Machine learning concept drift detection method (Apria)

力扣刷题日记/day3/2022.6.25
随机推荐
Make a grenade with 3DMAX
Interview summary of large factory Daquan II
Neglected problem: test environment configuration management
Li Kou brush question diary /day7/6.30
[cloud voice suggestion collection] cloud store renewal and upgrading: provide effective suggestions, win a large number of code beans, Huawei AI speaker 2!
【Hot100】32. Longest valid bracket
Device interface analysis of the adapter of I2C subsystem (I2C dev.c file analysis)
.NET ORM框架HiSql实战-第二章-使用Hisql实现菜单管理(增删改查)
An example of multi module collaboration based on NCF
一种将Tree-LSTM的强化学习用于连接顺序选择的方法
Pytoch deep learning environment construction
一直以为做报表只能用EXCEL和PPT,直到我看到了这套模板(附模板)
俄罗斯 Arenadata 发布基于PostgreSQL的产品
【Go语言刷题篇】Go完结篇|函数、结构体、接口、错误入门学习
Flask lightweight web framework
Face_ Attendance statistics of recognition face recognition
线上MySQL的自增id用尽怎么办?
力扣刷题日记/day3/2022.6.25
【每日一题】871. 最低加油次数
未来几年中,软件测试的几大趋势是什么?