当前位置:网站首页>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;
}
}
边栏推荐
- Grain Mall (I)
- How to open an account is safe,
- 为啥有些线上演唱会总是怪怪的?
- 力扣刷题日记/day2/2022.6.24
- 【2022年江西省研究生数学建模】冰壶运动 思路分析及代码实现
- Numpy 的仿制 2
- Li Kou brush question diary /day8/7.1
- The controversial line of energy replenishment: will fast charging lead to reunification?
- You should know something about ci/cd
- General environmental instructions for the project
猜你喜欢

VMware Tools和open-vm-tools的安装与使用:解决虚拟机不全屏和无法传输文件的问题

【Go语言刷题篇】Go完结篇|函数、结构体、接口、错误入门学习

输入的查询SQL语句,是如何执行的?

celebrate! Kelan sundb and Zhongchuang software complete the compatibility adaptation of seven products

Li Kou brush question diary /day1/2022.6.23

Halcon template matching

爬虫初级学习

Stars open stores, return, return, return

Wireshark抓包TLS协议栏显示版本不一致问题

五千字讲清楚团队自组织建设 | Liga 妙谈
随机推荐
Wireshark抓包TLS协议栏显示版本不一致问题
How is the entered query SQL statement executed?
DB engines database ranking in July 2022: Microsoft SQL Server rose sharply, Oracle fell sharply
力扣刷题日记/day7/2022.6.29
力扣刷题日记/day8/7.1
Implementation of shell script replacement function
爬虫初级学习
Five thousand words to clarify team self-organization construction | Liga wonderful talk
Unity 制作旋转门 推拉门 柜门 抽屉 点击自动开门效果 开关门自动播放音效 (附带编辑器扩展代码)
Li Kou brush question diary /day1/2022.6.23
Performance test of Gatling
. Net ORM framework hisql practice - Chapter 2 - using hisql to realize menu management (add, delete, modify and check)
Device interface analysis of the adapter of I2C subsystem (I2C dev.c file analysis)
MySQL common add, delete, modify and query operations (crud)
Android uses sqliteopenhelper to flash back
输入的查询SQL语句,是如何执行的?
[daily question] 556 Next bigger element III
uni-app与uviewUI实现仿小米商城app(附源码)
Load test practice of pingcode performance test
Summary of subsidy policies across the country for dcmm certification in 2022