当前位置:网站首页>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;
}
}
边栏推荐
- 俄罗斯 Arenadata 发布基于PostgreSQL的产品
- Android uses sqliteopenhelper to flash back
- 字节跳动Dev Better技术沙龙成功举办,携手华泰分享Web研发效能提升经验
- Imitation of numpy 2
- Li Kou brush question diary /day3/2022.6.25
- LD_LIBRARY_PATH 环境变量设置
- Open source PostgreSQL extension age for graph database was announced as the top-level project of Apache Software Foundation
- 输入的查询SQL语句,是如何执行的?
- Unity 制作旋转门 推拉门 柜门 抽屉 点击自动开门效果 开关门自动播放音效 (附带编辑器扩展代码)
- 机器学习概念漂移检测方法(Aporia)
猜你喜欢

Once the "king of color TV", he sold pork before delisting
![[go language question brushing chapter] go conclusion chapter | introduction to functions, structures, interfaces, and errors](/img/7a/16b481753d7d57f50dc8787eec8a1a.png)
[go language question brushing chapter] go conclusion chapter | introduction to functions, structures, interfaces, and errors

Li Kou brush question diary /day7/6.30

Halcon template matching

提升复杂场景三维重建精度 | 基于PaddleSeg分割无人机遥感影像

庆贺!科蓝SUNDB与中创软件完成七大产品的兼容性适配

估值900亿,超级芯片IPO来了

表情包坑惨职场人

Li Kou brush question diary /day3/2022.6.25

78 year old professor Huake impacts the IPO, and Fengnian capital is expected to reap dozens of times the return
随机推荐
The money circle boss, who is richer than Li Ka Shing, has just bought a building in Saudi Arabia
How to open an account is safe,
Summary of subsidy policies across the country for dcmm certification in 2022
File processing examples of fopen, FREAD, fwrite, fseek
【2022年江西省研究生数学建模】水汽过饱和的核化除霾 思路分析及代码实现
Imitation of numpy 2
【210】PHP 定界符的用法
How is the entered query SQL statement executed?
The block:usdd has strong growth momentum
LD_ LIBRARY_ Path environment variable setting
android使用SQLiteOpenHelper闪退
字节跳动Dev Better技术沙龙成功举办,携手华泰分享Web研发效能提升经验
力扣刷题日记/day7/6.30
Tutorial on the use of Huawei cloud modelarts (with detailed illustrations)
Li Kou brush question diary /day5/2022.6.27
Digital "new" operation and maintenance of energy industry
You should know something about ci/cd
ARTS_ twenty million two hundred and twenty thousand six hundred and twenty-eight
蓝桥:合根植物
为啥有些线上演唱会总是怪怪的?