STRING
空参构造创建字符串对象
调用有参构造 数据类型是数组
字节的表现形式
2.String
2.1 概述
Java提供的用于描述字符串的类。在JDK的java.lang包下定义了String这个类。在该类中定义了很多我们常用的成员方法,用于对字符串进行常用操作。
2.2 对象创建
2.2.1 直接使用字面值
可以直接定义String类型的变量直接给其赋值一个字符串字面值
String name = "三更";
2.2.2 使用构造方法
可以使用String中定义的构造方法来创建对象。String有10多个构造方法。我们就针对其中比较常用的几个进行演示。
String()
String(byte[] bytes)
String(byte[] bytes, int offset, int length)
String(char[] value)
String(char[] value, int offset, int count)
演示如下:
public static void main(String[] args) {
//使用无参构造创建。字符串的内容为空 相当于 ""
String s1 = new String();
}
public static void main(String[] args) {
byte[] bytes = {97,98,99};
String s = new String(bytes);
System.out.println(s);//输出结果为 abc
}
public static void main(String[] args) {
byte[] bytes = {97,98,99};
String s = new String(bytes,0,2);
System.out.println(s);//输出结果为ab
String s2 = new String(bytes,0,1);
System.out.println(s2);//输出结果为a
}
public static void main(String[] args) {
char[] chars = {'a','b','c'};
String s = new String(chars);
System.out.println(s);//输出结果为 abc
}
public static void main(String[] args) {
char[] chars = {'a','b','c'};
String s = new String(chars,0,1);
System.out.println(s);//输出结果为 a
String ss = new String(chars,0,2);
System.out.println(s2);//输出结果为 ab
}
2.3 常用方法
String[] split(String regex) 把一个字符串按照指定的分隔符切割成多个字符串,把多个字符串放在一个字符串数组中返回
char[] toCharArray(); 把一个字符串的内容转换成一个字符数组
byte[] getBytes(); 把一个字符串的内容转换成一个byte数组
String substring(int index); 把某个字符串从index索引开始截取到最后
String substring(int begin,int end) 把某个字符串索引begin到索引end截取出来
boolean equals(Object anObject) 判断两个字符串的内容是否相同
split方法演示
public static void main(String[] args) {
String s = "三更,15,男";
String[] strs = s.split(",");
for (int i = 0; i < strs.length; i++) {
System.out.println(strs[i]);
}
}
toCharArray方法演示
public static void main(String[] args) {
String s = "三更,15,男";
char[] chars = s.toCharArray();
for (int i = 0; i < chars.length; i++) {
System.out.println(chars[i]);
}
}
getBytes方法演示
public static void main(String[] args) {
String s = "abcd";
byte[] bytes = s.getBytes();
for (int i = 0; i < bytes.length; i++) {
System.out.println(bytes[i]);
}
}
substring方法演示
public static void main(String[] args) {
String s = "abcd";
String substring = s.substring(1);
System.out.println(substring);
}
public static void main(String[] args) {
String s = "abcd";
String substring = s.substring(1,2);
System.out.println(substring);
}
equals方法演示
public static void main(String[] args) {
String s = "abcd";
String s2 = "abcd";
boolean flag = s.equals(s2);
System.out.println(flag);//输出true
}
2.4 特点
- 一个字符串一旦创建其内容是永远不会变的
- 字符串效果上相当于是char[]字符数组,但是底层其实是byte[]字节数组
3. 包装类
3.1 概述
Java为每一个基本数据类型提供了对应的一个包装类。包装类是一个类,这样Java就可以在包装类中提供常用的方法等供我们使用。
基本数据类型和对应包装类的对应关系为:
基本数据类型 | 包装类 |
---|---|
byte | Byte |
boolean | Boolean |
short | Short |
char | Character |
int | Integer |
long | Long |
float | Float |
double | Double |
包装类的功能基本类似下面就以Integer为例子演示。
3.2 创建对象
3.2.1 直接使用字面值
public static void main(String[] args) {
Integer i = 10;
}
3.2.2 使用构造方法
public static void main(String[] args) {
Integer i = new Integer(10);
Integer i2 = new Integer("11");
}
3.2.3 使用静态方法valueOf
public static void main(String[] args) {
Integer i = Integer.valueOf(10);
Integer i2 = Integer.valueOf("11");
}
3.3. 常用方法
static Integer valueOf(int i) 把int转换转换成Integer对象返回
static Integer valueOf(String s) 把字符串转换转换成Integer对象返回 要求字符串的内容必须为纯数字
static int parseInt(String s) 把字符串转换转换成int返回 要求字符串的内容必须为纯数字
public static void main(String[] args) {
Integer i = Integer.valueOf(10);
Integer i2 = Integer.valueOf("11");
int num = Integer.parseInt("123");
System.out.println(num);
}
3.4 自动装箱与自动拆箱
3.4.1 概述
在JDK1.5中就增加了自动装箱和自动拆箱。主要是让基本数据类和对应的包装类进行自动的转换,方便我们的使用。
3.4.2 自动装箱
自动装箱就是基本数据类型可以自动转换为对应的包装类。例如:
Integer i = 10;
int num = 20;
Integer i2 = num;
构造方法 传入int最后得到integer
3.4.3 自动拆箱
自动拆箱就是包装类可以自动转换为基本数据类型。例如:
Integer i = new Integer(10);
int num = i;
3.4.4 原理
自动装箱其实是通过包装类的静态方法valueOf来实现的转换。
集合-1
重点:字符串 包装类
1.集合的概念
集合就是用于存储多个数据的容器。相对于具有相同功能的数组来说,集合的长度可变会更加灵活方便。Java中提供了使用不同数据结构存储数据的不同集合类,他们有各自不同的特点,并且在类中提供了很多常用了方法,便于我们使用。
2.集合体系结构
集合主要有两个顶层接口,Collection和Map。
object是所有类的祖宗类
在这里插入图片描述
3. 常用list集合
3.1 list集合的特点
List接口下的集合都会有以下特点:
- 有索引
- 可以存储重复元素
- 元素存入的顺序和实际存储的顺序相同
3.2 ArrayList
3.2.1 创建对象
ArrayList list = new ArrayList<>();//不限定集合中存放元素的数据类型
ArrayList<集合元素的数据类型> list2 = new ArrayList<>();//限定集合中存放元素的数据类型
3.2.2 常用方法
boolean add(E e) //添加元素,直接添加到集合的末尾 返回值代表是否添加成功 void add(int index, E element) //往指定索引位置添加元素 boolean remove(Object o)// 删除元素 E remove(int index) //删除指定索引位置的元素,返回值是被删除的元素 E set(int index, E element) //修改指定索引位置的元素 返回值为修改之前的元素值 E get(int index) //获取指定索引位置的元素 返回值为对应的元素 int size() //获取集合中元素的个数 boolean contains(Object o) //判断集合中是否存在某个元素 ,返回值代表是否存在
我们平时对集合用的最多的是add,remove,get,set,size这几个方法。
3.2.3 遍历
1.使用索引遍历
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("三");
list.add("更");
list.add("草");
list.add("堂");
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
2.使用迭代器遍历
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("三");
list.add("更");
list.add("草");
list.add("堂");
Iterator<String> it = list.iterator();
while (it.hasNext()){
String s = it.next();
System.out.println(s);
}
}
注意:为了避免并发修改异常(ConcurrentModificationException)的出现。避免在使用迭代器遍历的过程中对集合进行操作。
3.使用foreach遍历
foreach是java提供的一个语法糖。可以让我们更方便的遍历集合或数组。
格式如下:
for(元素数据类型 变量名 : 遍历的集合或者数组){
//遍历的时候会把遍历到的元素赋值给我们上面定义的变量
}
例如:
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("三");
list.add("更");
list.add("草");
list.add("堂");
for (String s : list) {
System.out.println(s);
}
}
String[] arr = {"三","更","草","堂"};
for(String s : arr){
System.out.println(s);
}
4.转换为数组遍历
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("三");
list.add("更");
list.add("草");
list.add("堂");
//把list集合转换为数组 参数列表 返回值类型 []
// Object[] objects = list.toArray();
//遍历数组
// for (int i = 0; i < objects.length; i++) {
// System.out.println(objects[i]);
// }
// public <T> T[] toArray(T[] a)
String[] strings = list.toArray(new String[0]);
for (String string : strings) {
System.out.println(string);
}
}
3.3 LinkedList
3.3.1 创建对象
LinkedList list = new LinkedList<>();//不限定集合中存放元素的数据类型
LinkedList<集合元素的数据类型> list2 = new LinkedList<>();//限定集合中存放元素的数据类型
3.3.2 常用方法
boolean add(E e) //添加元素,直接添加到集合的末尾 返回值代表是否添加成功 void add(int index, E element) //往指定索引位置添加元素 boolean remove(Object o)// 删除元素 E remove(int index) //删除指定索引位置的元素,返回值是被删除的元素 E set(int index, E element) //修改指定索引位置的元素 返回值为修改之前的元素值 E get(int index) //获取指定索引位置的元素 返回值为对应的元素 int size() //获取集合中元素的个数 boolean contains(Object o) //判断集合中是否存在某个元素 ,返回值代表是否存在
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
//添加元素
list.add("更");
list.add(0,"三");
//删除元素
list.remove("三");
//修改元素
list.set(0,"三更草堂");
//获取元素
String s = list.get(0);
//获取集合大小
int size = list.size();
//判断元素是否存在
boolean flag = list.contains("三更草堂");
}
我们发现上面这些方法其实和ArrayList中的常用方法都是相同的。因为LinkedList和ArrayList都是List接口的实现类,上面的很多方法都是他们共同的接口中定义的方法,所以都会有。
下面是LinkedList的一些特有方法:
void addFirst(E e) //把元素添加到集合的最前面 void addLast(E e) //把元素添加到集合的最后面 E removeFirst() //删除集合最前面的一个元素,返回值代表被删除的元素 E removeLast() //删除集合最后面的一个元素,返回值代表被删除的元素
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
list.add("三");
list.add("更");
list.add("草");
list.add("堂");
list.addFirst("[");
list.addLast("]");
String s = list.removeFirst();
System.out.println(s);
String s1 = list.removeLast();
System.out.println(s1);
}
3.3.3 遍历
同ArrayList。
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
list.add("三");
list.add("更");
list.add("草");
list.add("堂");
//遍历集合
//for循环遍历
// for (int i = 0; i < list.size(); i++) {
// System.out.println(list.get(i));
// }
//迭代器
// Iterator<String> it = list.iterator();
// while (it.hasNext()){
// String s = it.next();
// System.out.println(s);
// }
//foreach
// for(String s : list){
// System.out.println(s);
// }
//转换为数组遍历
String[] strings = list.toArray(new String[0]);
for (int i = 0; i < strings.length; i++) {
System.out.println(strings[i]);
}
}
3.4 ArrayList和LinkedList的区别
都是实现了List接口,不同点是底层存储数据的数据结构不同。ArrayList底层是用数组来存储,而LinkedList是链表。所以各自的特点也和数据结构的特点一样。
ArrayList : 查找快,增删慢
LinkedList: 增删快,查找慢# 集合-1