当前位置:网站首页>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;
}
}
边栏推荐
- Set the transparent hidden taskbar and full screen display of the form
- [cloud native] what is the "grid" of service grid?
- uni-app与uviewUI实现仿小米商城app(附源码)
- The top half and bottom half of the interrupt are introduced and the implementation method (tasklet and work queue)
- I always thought that excel and PPT could only be used for making statements until I saw this set of templates (attached)
- Implementation of shell script replacement function
- 五千字讲清楚团队自组织建设 | Liga 妙谈
- 力扣刷题日记/day5/2022.6.27
- Interview summary of large factory Daquan II
- Detailed explanation of the maturity classification of ITSS operation and maintenance capability | one article clarifies the ITSS certificate
猜你喜欢

为啥有些线上演唱会总是怪怪的?

ISO27001 certification process and 2022 subsidy policy summary

Tutorial on the use of Huawei cloud modelarts (with detailed illustrations)

力扣刷題日記/day6/6.28

TCP waves twice, have you seen it? What about four handshakes?

Improve the accuracy of 3D reconstruction of complex scenes | segmentation of UAV Remote Sensing Images Based on paddleseg

力扣刷题日记/day7/6.30

Journal des problèmes de brosse à boutons de force / day6 / 6.28

力扣刷题日记/day5/2022.6.27

TCP两次挥手,你见过吗?那四次握手呢?
随机推荐
uni-app与uviewUI实现仿小米商城app(附源码)
【云端心声 建议征集】云商店焕新升级:提有效建议,赢海量码豆、华为AI音箱2!
TCP两次挥手,你见过吗?那四次握手呢?
Li Kou brush question diary /day7/2022.6.29
TCP waves twice, have you seen it? What about four handshakes?
With the stock price plummeting and the market value shrinking, Naixue launched a virtual stock, which was deeply in dispute
78 year old professor Huake impacts the IPO, and Fengnian capital is expected to reap dozens of times the return
爬虫初级学习
How to download files using WGet and curl
How is the entered query SQL statement executed?
What types of Thawte wildcard SSL certificates provide
Flask lightweight web framework
NBA赛事直播超清画质背后:阿里云视频云「窄带高清2.0」技术深度解读
[2022 Jiangxi graduate mathematical modeling] curling movement idea analysis and code implementation
An example of multi module collaboration based on NCF
力扣刷题日记/day7/2022.6.29
一、C语言入门基础
为啥有些线上演唱会总是怪怪的?
字节跳动Dev Better技术沙龙成功举办,携手华泰分享Web研发效能提升经验
Li Kou brush question diary /day3/2022.6.25