1.字符串
1.1 String常用的方法
实际的使用:
package com_imooc.zifu;
public class string_test {
public static void main(String[] args) {
String t = "JAVA 编程 基础 学习,I LOVE JAVA";
//1.字符串的位置和数组一样是从0开始的;
System.out.println("字符串的长度是:" + t.length());
System.out.println("'编'第一次出现的位置:" + t.indexOf('编'));
System.out.println("从字符9开始,查找'J'第一次出现的位置:" + t.indexOf("A", 9));
System.out.println("'A'最后一次出现的位置:" + t.lastIndexOf('A'));
System.out.println("取出字符中8号位置对应的值:" + t.charAt(8));
System.out.println("从字符中8号位置开始,截取字符串值:" + t.substring(8));
System.out.println("从字符中1号位置开始,截取到8号位置,获取的字符值:" + t.substring(1, 8));
//2.字符串和Byte数组
//字符串-->Byte数组的转化
byte[] arr = t.getBytes();
for (byte v : arr) {
System.out.println(v);
}
//Byte数组-->字符串的转化
String t1 = new String(arr);
System.out.println("转化结果是:" + t1);
//3.==和equals()的区别
String i = "I LOVE JAVA";
String i1 = "I LOVE JAVA";
String i2 = new String("I LOVE JAVA");
//equals指的是字符是否相等
System.out.println("i和i1是否相等:" + i.equals(i1));
System.out.println("i和i2是否相等:" + i.equals(i2));
//==指的是全等(包括内存空间,指针等)
System.out.println("i和i1是否==:" + (i == i1));
System.out.println("i和i2是否==:" + (i == i2));
}
}
1.2 StringBuilder
string和stringBuilder的区别
stringBuffer和stringBuilder的区别
代码:
package com_imooc.zifu;
public class string_test {
public static void main(String[] args) {
//4.stringbuilder
StringBuilder sb=new StringBuilder("你好");
//追加
sb.append("1212121212");
System.out.println("字符的结果是:"+sb);
//插入
sb.insert(4,"我是加入的");
System.out.println("在4号位置加入:"+sb);
//替换
sb.replace(2,8,"我是插入的,啦啦啦!");
System.out.println("替换2到8号位置:"+sb);
//截取
String bb= sb.substring(2,4);
System.out.println("截取2到4号的字符:"+bb);
//删除
sb.delete(2,sb.length());
System.out.println("删除2号字符到最后:"+sb);
}
}
2.集合
集合的分类:
2.1 List(列表)
我们先学习ArrayList的方法:
package com_imooc.jihe;
import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
public class Arraylist_test {
public static void bianli(List l) {
for (Object v : l) {
System.out.println(" " + v);
}
}
public static void main(String[] args) {
List l = new ArrayList();
//添加
l.add("Java");
l.add("C");
l.add("C++");
l.add("Go");
l.add("Swift");
System.out.println("列表的元素个数:" + l.size());
System.out.println("========================");
//插入
l.addAll(2, Collections.singleton("我什么也不会的")); //可以插入其他的list
l.add(4, "这是什么鬼"); //只能插入单个数据
//获取
Object l1 = l.get(1);
System.out.println("获取的元素是:" + l1);
System.out.println("========================");
//替换
l.set(1, "CCC");
//遍历
System.out.println("首次遍历:");
bianli(l); //自己的遍历发那个发
System.out.println(((ArrayList) l).clone()); //提供的遍历方法
System.out.println("========================");
//移除
l.remove(1); //删除序号为1的元素
l.remove("C++"); //删除指定的元素
System.out.println("删除后的list:");
bianli(l);
System.out.println("========================");
//判断list是否为空
System.out.println("不为空:" + !l.isEmpty());
System.out.println("========================");
}
}
2.2 Set(集)
package com_imooc.jihe;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class Hashset_test {
public static void main(String[] args) {
Set<String> s = new HashSet<String>();
//添加
s.add("blue");
s.add("red");
s.add("black");
s.add("yellow");
s.add("white");
//迭代(遍历)
Iterator rel = s.iterator();
while (rel.hasNext()) {
System.out.println(" " + rel.next());
}
System.out.println("=========================");
for (String v:s){
System.out.println(v);
}
//删除
s.remove("red");
//展示内容
Object ss = ((HashSet) s).clone();
System.out.println(ss);
}
}
运用实例:
创建cat.java
package com_imooc.jihe;
import java.util.Objects;
public class cat {
private String name;
private String pingzhong;
private int age;
public cat(String name, String pingzhong, int age) {
this.name = name;
this.pingzhong = pingzhong;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPingzhong() {
return pingzhong;
}
public void setPingzhong(String pingzhong) {
this.pingzhong = pingzhong;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "名称:'" + name + '\'' +
", 品种:'" + pingzhong + '\'' +
", 年龄:" + age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
cat cat = (cat) o;
return age == cat.age &&
Objects.equals(name, cat.name) &&
Objects.equals(pingzhong, cat.pingzhong);
}
@Override
public int hashCode() {
return Objects.hash(name, pingzhong, age);
}
}
实现curl的功能
package com_imooc.jihe;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class Hashset_anli {
public static void main(String[] args) {
cat c1 = new cat("花花", "中华天原猫", 1);
cat c2 = new cat("芳芳", "英国短毛猫", 2);
Set<cat> h = new HashSet<cat>();
h.add(c1);
h.add(c2);
//1.遍历(需要在cat类中使用totring方法)
Iterator<cat> rel = h.iterator();
while (rel.hasNext()) {
System.out.println(rel.next());
}
System.out.println("===================================");
//2.添加一个和花花一样的类c3
//在重写hashCode和equals以后避免重复
cat c3 = new cat("花花", "中华天原猫", 1);
h.add(c3);
Iterator<cat> re = h.iterator();
while (re.hasNext()) {
System.out.println(re.next());
}
System.out.println(((HashSet<cat>) h).clone());
System.out.println("===================================");
//3.查找
//先判断是否存在于集合中
if (h.contains(c1)) {
System.out.println("花花找到了");
} else {
System.out.println("花花不在我这里");
}
Iterator<cat> r = h.iterator();
while (r.hasNext()) {
cat cc = r.next();
if (cc.getName().equals("花花")) {
System.out.println("可爱的花花,他是:" + cc.getPingzhong());
}
}
//4.删除一
//break必须写,否则会因为删除过后size的大小不一,而导致报错;
for (cat v : h) {
if (v.getName().equals("花花")) {
h.remove(v);
break;
}
}
System.out.println(((HashSet<cat>) h).clone());
//删除二
Set<cat> hh = new HashSet<cat>();
for (cat v : h) {
if (v.getAge() < 10) {
hh.add(v);
}
}
h.removeAll(hh); //批量删除
}
}
2.3 Map
Map的定义:
我们这里讨论的是HashMap:
package com_imooc.jihe;
import java.util.*;
public class Hashmap_test {
public static void main(String[] args) {
Map<String, String> m = new HashMap<String, String>();
//map值的添加
m.put("a", "aaaaaaaaaa");
m.put("b", "bbbbbbbbbb");
m.put("c", "cccccccccc");
//map的删除
m.remove("a");
//map的修改
m.put("b", "dddddddd");
//map的查找
String rel = m.get("c");
System.out.println(rel);
//遍历1(赋值后打印输出)
System.out.println(((HashMap<String, String>) m).clone());
System.out.println(m.values()); //获取values值
System.out.println(m.keySet()); //获取key值
System.out.println("==========================");
//遍历2(输出key和values(过于复杂,不推荐))
Set<Map.Entry<String, String>> ss = m.entrySet();
for (Map.Entry<String, String> v : ss) {
System.out.print(v.getKey() + ":");
System.out.println(v.getValue());
}
System.out.println("==========================");
//遍历3(通过get()和keyset()获取key和values(推荐方式))
Set<String> sm = m.keySet();
for (String v : sm) {
System.out.print(v + ":");
System.out.println(m.get(v));
}
System.out.println("==========================");
//遍历4(通过迭代器)
Iterator<String> ismap=m.values().iterator();
while (ismap.hasNext()){
System.out.println(ismap.next());
}
}
}