当前位置:网站首页>力扣刷题日记/day2/2022.6.24
力扣刷题日记/day2/2022.6.24
2022-07-04 16:33:00 【bobo洁厕灵】
新手村
今天还是学习数组的有关概念,字符(String)串数组,创建方式和一般的数组类似,只是类型的不同需要注意
一.List作为一个接口,接口不能被实例化
List list=new List();
//会导致编译出错,这是因为List是一个接口,接口不能被实例化。ArrayList list=new ArrayList();
//这种实例化方式就是正确的。这是因为ArrayList是一个类,继承并实现了List接口。如何实现一个接口(以List接口为例)
通过继承自本接口的实现类的对象实例化List对象,如
List list=new ArrayList();这种实例化的方式:创建了一个ArrayList实现类的对象后把它上溯到了List接口,此时它是一个List对象了,有些ArrayList有但是List没有的属性和方法,它就不能再用了。
ArrayList list=new ArrayList();上述方法创建一对象则保留了ArrayList的所有属性和方法。
两种创建方法的比较
大部分情况下,我们使用的是创建一个ArrayList实现类的对象然后上升到List接口,因为这种方法创建的对象更为方便,List接口有多个实现类,现在用的是ArrayList,如果要将其更换成其它的实现类,如 LinkedList或者Vector等等,这时只需要改变这一行就行了: List list = new LinkedList(); 其它使用了list地方的代码根本不需要改动。 在编写代码时,较为方便。
但是只能调用List接口中的方法,不能调用ArrayList中的方法。
List list=new ArrayList();二.JAVA String.valueOf()方法的用法说明
由基本数据型态转换成String
int i = 10;
String str = String.valueOf(i);此时的str就会是“10”
String.valueOf(int i)和Integer.toString(int i)有什么区别?
String.valueOf()它可以将JAVA基本类型(int,double,boolean等)和对象(Object)转换成String型
toString()是对象的方法,它可以将该对象转换成String型,转换算法根据类型实际需要而定 ,基本上JAVA里面每个对象都会有一个toString方法。
两者的运行结果都是相同的,只是原理不一样
String.valueOf()可以应用到任何数据类型,且不会有异常报出。
Integer.toString()表示先讲int转换成Integer型,然后再将Integer转换成String型。
补充toString方法
//toString(): 返回表示 Integer 值的 String 对象。
//toString(int i): 返回表示指定 int 的 String 对象 
一般String.valueOf()使用的居多,应用的数据类型无限制,且不会报空指针异常
三.创建一个字符串数组的两种方法
//创建一个字符串数组,这种方法比较灵活
ArrayList<String> answer = new ArrayList<String>();
//添加字符串
answer.add("BUFF");
//java中size()方法是对于泛型集合来说的,如果想知道泛型有多少元素,就调用此方法来查看。
answer.size();//百度上看到的,很刻板的一种写法
String [] answer = new String [20];
//添加数组
answer[0] = "str1";
//java中length()是对于String来说的,如果想看字符串的长度,则用length()方法;
answer.length()四.例题

解题思路:先创建一个数组对象(list),再通过for遍历向数组中添加字符串,如果符合上述要求,则add,如果都不符合,就把整数转换String.valueOf成字符串添加进数组中
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;
}
}
边栏推荐
- 五千字讲清楚团队自组织建设 | Liga 妙谈
- 华为云ModelArts的使用教程(附详细图解)
- 线上MySQL的自增id用尽怎么办?
- [system analyst's road] Chapter 7 double disk system design (structured development method)
- Detailed explanation of the maturity classification of ITSS operation and maintenance capability | one article clarifies the ITSS certificate
- 爬虫(6) - 网页数据解析(2) | BeautifulSoup4在爬虫中的使用
- Just today, four experts from HSBC gathered to discuss the problems of bank core system transformation, migration and reconstruction
- Numpy 的仿制 2
- Load test practice of pingcode performance test
- [211] go handles the detailed documents of Excel library
猜你喜欢

TCP两次挥手,你见过吗?那四次握手呢?

The controversial line of energy replenishment: will fast charging lead to reunification?

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

爬虫(6) - 网页数据解析(2) | BeautifulSoup4在爬虫中的使用

Grain Mall (I)

创业两年,一家小VC的自我反思

Talk about seven ways to realize asynchronous programming

Make a grenade with 3DMAX

力扣刷题日记/day8/7.1

Imitation of numpy 2
随机推荐
Is it safe to download the mobile version of Anxin securities and open an account online
Implementation of shell script replacement function
Once the "king of color TV", he sold pork before delisting
力扣刷题日记/day6/6.28
S5PV210芯片I2C适配器驱动分析(i2c-s3c2410.c)
Interview summary of large factory Daquan II
Pytoch deep learning environment construction
78岁华科教授冲击IPO,丰年资本有望斩获数十倍回报
线上MySQL的自增id用尽怎么办?
提升复杂场景三维重建精度 | 基于PaddleSeg分割无人机遥感影像
力扣刷题日记/day5/2022.6.27
12 - explore the underlying principles of IOS | runtime [isa details, class structure, method cache | t]
ISO27001认证办理流程及2022年补贴政策汇总
明星开店,退,退,退
Detectron2 installation method
基于NCF的多模块协同实例
Redis主从复制
Neglected problem: test environment configuration management
Talk about seven ways to realize asynchronous programming
2022年DCMM认证全国各地补贴政策汇总