文章目录
- ArrayList集合
- String类
- static关键字
- Arrays工具类
- Math工具类
ArrayList集合
1
2
3数组的长度不可以发生改变。 但是ArrayList集合的长度是可以随意变化的
对于ArrayList来说,有一个<E>
代表泛型
泛型:也就是装在集合当中的所有元素,全都是统一的什么类型
注意:泛型只能是引用类型,不能是基本类型
1
2
3
4注意事项: 对于ArrayList集合来说,直接打印的不是地址值,而是内容 如果内容为空,得到空的中括号;[]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18import java.util.ArrayList; public class DemoArrayList { public static void main(String[] args) { //创建一个ArrayList集合,集合名称是list,里面装的全是String字符串类型的数据 ArrayList<String> list = new ArrayList<String>(); System.out.println(list);//[] //向集合当中添加一些数据,需要用到add方法 list.add("fxx"); System.out.println(list); //添加多个 list.add("fxx"); list.add("hhh"); list.add("zzz"); list.add("fff"); System.out.println(list);//[fxx, fxx, hhh, zzz, fff] } }
-
ArrayList当中常用方法有
复制代码1
2
3
4
5
6public boolean add(E e):向集合当中添加元素,参数的类型和泛型一致。 public E get(int index):从集合当中获取元素,参数是索引编号,返回值就是对应位置的元素。 public E remove(int index):从集合中删除元素,参数是索引的编号,返回值就是被删除掉的元素 public int size();获取集合的尺寸长度,返回值是集合中包含的元素的个数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26public class ArrayListMethod { public static void main(String[] args) { ArrayList<String> list = new ArrayList<>(); System.out.println(list);//[] //向集合中添加元素:add boolean success = list.add("xx"); System.out.println(list);//[xx] System.out.println("添加的动作是否成功:"+success);//true list.add("ww"); list.add("hh"); list.add("qq"); System.out.println(list);//[xx, ww, hh, qq] //从集合中获取元素:get(索引值从0开始) String name = list.get(2); System.out.println("返回"+name); System.out.println("返回"+list.get(2)); //从集合中删除元素:remove String whoRemoved = list.remove(3); System.out.println("删除的人是"+whoRemoved); System.out.println(list); //获取集合的长度尺寸 int size = list.size(); System.out.println("长度是:"+size); } }
- 遍历
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20public class ArrayListEach { public static void main(String[] args) { //遍历数组集合 ArrayList<String> list1 = new ArrayList<>(); list1.add("ww"); list1.add("sd"); list1.add("ff"); list1.add("tt"); list1.add("vv"); for(int i=0;i<list1.size();i++) { // String n = list1.get(i); // System.out.println(n); System.out.println(list1.get(i)); } } }
- 如果希望向集合ArrayList当中存储基本类型,必须使用基本类型对应的包装类。
基本类型 | 包装类(引用类型,包装类都位于java.lang包下) |
---|---|
byte | Byte |
short | Short |
int | Integer 【特殊】 |
long | Long |
float | Float |
double | Double |
char | Character 【特殊】 |
boolean | Boolean |
1
2
3自动装箱:基本类型------>包装类型 自动拆箱:包装类型------>基本类型
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15public class ArrayListBasic { public static void main(String[] args) { //错误写法,泛型只能是引用类型,不能是基本类型 // ArrayList<int> list2 = new ArrayList<>(); ArrayList<Integer> list3 = new ArrayList<>(); list3.add(100); list3.add(200); list3.add(300); list3.add(400); System.out.println(list3); int num = list3.get(1); System.out.println(num); } }
String类
-
创建字符串的常见3+1方式
复制代码1
2
3
4
5
6
7
8
9三种构造方法 1、public String():创建一个空白字符串,不含有任何内容。 2、public String(char[] array):根据字符数组的内容,来创建对应的字符串。 3、public String(byte[] array):根据字节数组的内容,来创建对应的字符串。 一种直接创建: String str = "abc"; 注意:直接写上双引号,就是字符串对象
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16public class Demo01String { public static void main(String[] args) { //使用空参构造 String str1 = new String(); System.out.println("第一个字符串"+str1); //根据字符数组创建字符串 char[] charArray = {'A','B','C'}; String str2 = new String(charArray); System.out.println("第二个字符串"+str2); //根据字节数组创建字符串 byte[] byteArray = {97,98,99}; String str3 = new String(byteArray); System.out.println("第三个字符串"+str3); } }
-
字符串的特点:
复制代码1
2
3
4
51、字符串的内容永不可变 2、正因为不可变,所以字符串是可以共享使用的 3、字符串效果上相当于char[]字符数组,但底层原理是byte[]字节数组
-
字符串常量池:
复制代码1
2
3程序当中直接写的双引号字符串,就在字符串常量池中。
注意:
复制代码1
2
3new的不在常量池中。
1
2
3
4
5
6
7
8
9
10
11
12
13
14//对于基本类型来说,==是进行数值的比较。 //对于引用类型来说:==是进行【地址值】的比较 public class Demo02StringPool { public static void main(String[] args) { String str1 = "abc"; String str2 = "abc"; char[] byteString = {'a','b','c'}; String str3 = new String(byteString); System.out.println(str1 == str2);//true System.out.println(str3 == str2);//false System.out.println(str1 == str3);//false } }
-
==是进行对象的地址值的比较,如果确实需要字符串的内容比较,可以使用两个方法:
public boolean equals(Object obj):参数可以是任何对象,只有参数是一个字符串并且内容相同的才会给true,否则false。
注意事项:
1、任何对象都能用object进行接收。
2、equals方法具有对称性,也就是a.equals(b)和b.equals(a)效果一样。
3、如果比较双方一个常量一个变量,推荐把常量字符串写在前面。
推荐: “abc”.equals(str)
不推荐:str.equals(“abc”)有空指针异常
public boolean equalsIgnoreCase(String str):忽略大小写,进行内容比较。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18public class Demo03StringEquals { public static void main(String[] args) { String str1 = "Hello"; String str2 = "Hello"; char[] charArray = {'H','e','l','l','o'}; String str3 = new String(charArray); System.out.println(str1.equals(str2));//true System.out.println(str2.equals(str3));//true System.out.println(str3.equals("Hello"));//true System.out.println("Hello".equals(str2));//true String str4 = "hello"; System.out.println(str1.equals(str4));//false System.out.println("==============================="); String str5 = "hello"; System.out.println(str1.equalsIgnoreCase(str5));//true } }
- String中与获取相关的常用的方法
1
2
3
4
5public int length():获取字符串的长度 public String concat(String str):将当前字符串和参数字符串拼接成为返回值新的字符串 public char charAt(int index):获取指定索引位置的单个字符 public int indexOf(String str):查找参数字符串在本字符串当中首次出现索引位置,如果没有返回-1值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24public class Demo04StringGet { public static void main(String[] args) { //获取字符串长度 int length = "abcdefghijklmn".length(); System.out.println("字符串的长度:"+length); //拼接字符串 String str1 = "Hello"; String str2 = "World"; String str3 = str1.concat(str2); System.out.println(str1);//Hello System.out.println(str2);//World System.out.println(str3);//HelloWorld,新字符串 //获取指定索引位置的单个字符 char ch = "Hello".charAt(1); System.out.println("在一号索引位置的字符是:"+ch);//e System.out.println("================================="); //查找参数字符串在本来字符串当中出现的第一次索引的位置 //如果没有,返回-1 String str4 = "HelloWorldHelloWorld"; int index = str4.indexOf("llo"); System.out.println(index);//2 } }
- 字符串的截取方法
1
2
3public String substring(int index):截取从参数位置一直到字符串末尾,返回新的字符串 public String substring(int begin,int end):截取从begin开始到end结束的中间的字符串([begin,end))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22public class Demo05Substring { public static void main(String[] args) { String str1 = "HelloWorld"; String str2 = str1.substring(5); System.out.println(str1);//HelloWorld System.out.println(str2);//World System.out.println("======================="); String str3 = str1.substring(4,7); System.out.println(str3);//oWo System.out.println("================="); //下面这种写法,字符串的内容仍然没有改变 //下面有两个字符串:"Hello","Java" //strA当中保存的是地址值 //本来是Hello的地址 //后来又变成了Java的地址 String strA = "Hello"; System.out.println(strA);//Hello strA = "Java"; System.out.println(strA);//Java } }
- String当中与转换有关的常用方法
1
2
3
4public char[] toCharArray():将当前字符串拆分成字符数组作为返回值。、 public byte[] getBytes():获得当前字符串底层的字节数组。 public String replace(CharSequence oldString,CharSequence newString):将所有出现的老字符串替换成为新的字符串,返回替换之后的结果新字符串。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25public class Demo06StringConvert { public static void main(String[] args) { //转换成为字符数组 String str1 = "Hello"; char[] n = str1.toCharArray(); for (int i = 0; i <n.length ; i++) { System.out.println(n[i]); } //转换成为字节数组 byte[] w = "abc".getBytes(); for (int i = 0; i <w.length ; i++) { System.out.println(w[i]); } //替换 String str2 = "How old are you?"; String str3 = str2.replace("o","*"); System.out.println(str2);//How old are you? System.out.println(str3);//H*w *ld are y*u? String lang1 = "sb!sb!!sb!!!"; String lang2 = lang1.replace("sb","**"); System.out.println(lang1);//sb!sb!!sb!!! System.out.println(lang2);//**!**!!**!!! } }
- 分割字符串的方法
public String[] split(String regex):按照参数规则,将字符串切成若干部分
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18//注意事项: //split方法的参数其实是一个正则表达式, //如果进行切分".",必须写"\." public class Demo07StringSplit { public static void main(String[] args) { String str1 = "aaa,bbb,ccc"; String[] array1 = str1.split(","); for (int i = 0; i < array1.length; i++) { System.out.println(array1[i]); } String str2 = "aaa.bbb.ccc"; String[] array2 = str2.split("\."); for (int i = 0; i < array2.length; i++) { System.out.println(array2[i]); } } }
static关键字
1
2
3如果一个成员变量使用了static关键字,那么这个变量不再属于对象自己, 而是属于所在的类。多个对象共享一份数据。
- 静态变量
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34public class Student { private String name; private int age; static String room;//所在教室 private int id;//学号 private static int idCounter = 0;//学号计数器 public Student(String name, int age) { this.name = name; this.age = age; this.id = ++idCounter; } public Student() { this.id = ++idCounter; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } }
1
2
3
4
5
6
7
8
9
10
11
12public class Demo01StaticField { public static void main(String[] args) { Student one = new Student("fx",20); one.room = "111寝室"; Student two = new Student("xx",21); System.out.println("姓名:"+one.getName()+"年龄:"+one.getAge()+"寝室:"+one.room+"学号:"+one.getId()); System.out.println("姓名:"+two.getName()+"年龄:"+two.getAge()+"寝室:"+two.room+"学号:"+two.getId()); //姓名:fx年龄:20寝室:111寝室学号:1 //姓名:xx年龄:21寝室:111寝室学号:2 } }
-
静态函数
一旦使用static修饰成员方法,那么这就成了静态方法。
静态方法不属于对象,而是属于类的。复制代码1
2
3
4
5
6
7
8
9
10
11
12如果没有static关键字,那么必须首先创建对象,然后通过对象才能使用它。 如果有了static关键字,那么不需要创建对象,直接就能通过类名称来使用它。 无论是成员变量,还是成员方法。如果有了static,都推荐使用类名称进行调用。 静态变量:类名称.静态变量 静态方法:类名称.静态方法() 注意事项: 1、静态不能直接访问非静态。 原因:因为在内存当中是【先】有的静态内容,【后】有的非静态内容。 2、静态方法当中不能用this 原因:this代表当前对象,通过谁调用的方法,谁就是当前对象
1
2
3
4
5
6
7
8
9public class MyClass { public void method(){ System.out.println("这是一个成员方法"); } public static void methodStatic(){ System.out.println("这是一个静态方法。"); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16public class Demo01StaticMethod { public static void main(String[] args) { MyClass obj = new MyClass();//首先创建对象,才能使用没有static关键字的内容 obj.method(); //对于静态方法来说,可以通过对象名进行调用,也可以直接通过类名称来调用。 obj.methodStatic();//正确,不推荐 MyClass.methodStatic();//正确,推荐 //对于本类当中的静态方法,可以省略类名称 myMethod(); Demo01StaticMethod.myMethod();//完全等效 } public static void myMethod(){ System.out.println("这是自己的方法!"); } }
-
静态代码块
复制代码1
2
3
4
5
6
7特点: 当第一次用到本类时,静态代码块执行唯一一次 静态内容总是优先于非静态,所以静态代码块比构造方法先执行。 静态代码块的典型用途: 用来一次性的对静态成员变量进行赋值
1
2
3
4
5
6
7
8
9public class Person { static{ System.out.println("静态代码块执行!"); } public Person(){ System.out.println("构造方法来了!"); } }
1
2
3
4
5
6
7
8
9
10
11public class Demo01 { public static void main(String[] args) { Person one = new Person(); Person two = new Person(); }//静态代码块执行! // 构造方法来了! // 构造方法来了! }
Arrays工具类
- Arrays是一个与数组相关的工具类,里面提供了大量的静态方法,用来实现数组常见的操作。
1
2
3public static String toString(数组):将参数数组变成字符串(按照默认格式:[元素1,元素2...]) public static void sort(数组):按照默认升序对数组的元素进行排序。
1
2
3
4
5备注: 1、如果是数值,sort默认按照升序从大到小 2、如果是字符串,sort默认按照字母升序 3、如果是自定义的类型,那么这个自定义的类需要有Comparable或者Comparator接口的支持。
1
2
3
4
5
6
7
8
9
10
11public class Demo01Arrays { public static void main(String[] args) { int[] intArray = {10,20,30}; String intStr = Arrays.toString(intArray); System.out.println(intStr);//[10, 20, 30] int[] intArray1 = {2,1,6,4,7,3}; Arrays.sort(intArray1); System.out.println(Arrays.toString(intArray1));//[1, 2, 3, 4, 6, 7] } }
Math工具类
- Math类是数学相关的工具类,里面提供了大量的静态方法,完成与数学运算相关的操作。
1
2
3
4
5
6public static double abs(double num):获取绝对值。 public static double ceil(double num):向上取整。 public static double floor(double num):向下取整。 public static long round(double num):四舍五入 Math.PI代表近似圆周率常量(double)。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public class Demo01Math { public static void main(String[] args) { //获取绝对值 System.out.println(Math.abs(3.14));//3.14 System.out.println(Math.abs(-222));//222 System.out.println("============================"); //向上取整 System.out.println(Math.ceil(3.1));//4.0 System.out.println("============================"); //向下取整 System.out.println(Math.floor(3.9));//3.0 System.out.println("============================"); //四舍五入 System.out.println(Math.round(20.4));//20 } }
最后
以上就是个性鞋垫最近收集整理的关于JavaSE学习(Day03)【ArrayList集合、String类、static、Arrays工具类、Math工具类】ArrayList集合String类static关键字Arrays工具类Math工具类的全部内容,更多相关JavaSE学习(Day03)【ArrayList集合、String类、static、Arrays工具类、Math工具类】ArrayList集合String类static关键字Arrays工具类Math工具类内容请搜索靠谱客的其他文章。
发表评论 取消回复