DAY10
程序员文章站
2022-09-17 10:21:44
1.异常:public class Test {public static void main(String[] args) {//String[] strs = new String[] {"a","b","c"};////for (int i = 0; i < 4; i++) {//System.out.println(strs[i]);//}//A a = new A();//A a = null;//空指针,java.lang.Null...
1.异常:
public class Test {
public static void main(String[] args) {
// String[] strs = new String[] {"a","b","c"};
//
// for (int i = 0; i < 4; i++) {
// System.out.println(strs[i]);
// }
// A a = new A();
// A a = null;//空指针,java.lang.NullPointerException
// System.out.println(a.i);
// int i = 0;
// System.out.println(3/i);/// by zero
}
}
class A{
int i;
}
2.捕获异常:
int i = 0;
try {//try{}括住可能出异常的代码段
System.out.println(3/i);/// by zero
}catch(Exception e) {//当不知道捕获的是什么异常时,则直接使用Exception
// e.printStackTrace();
// System.out.println(e.getMessage());
}
System.out.println("ok");
3.抛出异常:
public class Test1 {
public static void main(String[] args) {
B b = new B();
try {
b.test1(-100);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class B{
int age;
public void test1(int age) throws Exception{
if (age >= 0 && age <=150) {
this.age = age;
System.out.println(this.age);
}else {
throw new Exception("年龄不在0-150");
}
}
}
4.HashSet:
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class Test3 {
public static void main(String[] args) {
// Set set = new HashSet();
Set<Object> set = new HashSet<Object>();
// //添加
// set.add(1);
// set.add("a");
// System.out.println(set);
// //删除
// set.remove(1);
// System.out.println(set);
// //判断
// System.out.println(set.contains("a"));
// //清空
// set.clear();
// System.out.println(set);
set.add("a");
set.add("b");
set.add("c");
set.add("d");
//迭代器遍历
Iterator it = set.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
//for each迭代
for (Object obj : set) {//把set的每个值取出赋给obj,直到循环set的所有值
System.out.println(obj);
}
//个数
System.out.println(set.size());
//想让集合存同类型的对象:使用泛型
Set<String> set1 = new HashSet<String>();
set1.add("a");
set1.add("1");
}
}
5.TreeSet:
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
public class Test4 {
public static void main(String[] args) {
// Set<Integer> set = new TreeSet<Integer>();
// //TreeSet自然排序
// set.add(5);
// set.add(2);
// set.add(3);
// set.add(4);
// System.out.println(set);
//
// set.remove(5);
// set.contains(3);
// set.clear();
//
// //两种遍历
Person p1 = new Person("张三",23);
Person p2 = new Person("李四",21);
Person p3 = new Person("王五",16);
Person p4 = new Person("赵六",25);
Set<Person> set = new TreeSet<Person>(new Person());
set.add(p1);
set.add(p2);
set.add(p3);
set.add(p4);
for (Person p : set) {
System.out.println(p.name + " " + p.age);
}
}
}
class Person implements Comparator<Person>{//把Person对象存入,并按年龄排序
int age;
String name;
public Person() {
}
public Person(String name,int age) {
this.name = name;
this.age = age;
}
@Override
public int compare(Person o1, Person o2) {//年龄正序排
if(o1.age > o2.age) {
return 1;
}else if(o1.age < o2.age){
return -1;
}else {
return 0;
}
}
}
6.list:
import java.util.ArrayList;
import java.util.List;
public class Test5 {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("b");
list.add("x");
list.add("a");
list.add("d");
list.add("d");
//指定位置插入
list.add(2, "2");
//第一次出现的索引下标
System.out.println(list.indexOf("b"));
//最后一次出现的索引下标
System.out.println(list.lastIndexOf("d"));
list.remove("a");
list.remove(2);
//指定位置替换
list.set(1, "f");
System.out.println(list);
//截取部分
List<String> list2 = list.subList(2, 4);
System.out.println(list2);
//长度
System.out.println(list2.size());
}
}
7.Map:
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class Test6 {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<String, Integer>();
//key必须不同,值可以相同
map.put("c", 2);
map.put("b", 1);
map.put("e", 2);
//根据Key移除键值对
map.remove("b");
System.out.println(map);
//根据key取值
System.out.println(map.get("c"));
//长度
System.out.println(map.size());
//当前map中是否包含指定的key
System.out.println(map.containsKey("c"));
//当前map中是否包含指定的value
System.out.println(map.containsValue(2));
//遍历map
Set<String> keys = map.keySet();//获取key集合
map.values();//获取value集合
//循环key
for (String key : keys) {
System.out.println("key:" + key + ",value:" + map.get(key));
}
//通过map.entrySet();
Set<Entry<String, Integer>> entrys = map.entrySet();
for (Entry<String, Integer> en : entrys) {
System.out.println(en.getKey() + en.getValue());
}
}
}
8.Collections:
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Test7 {
public static void main(String[] args) {
// List<String> list = new ArrayList<String>();
// list.add("a");
// list.add("cc");
// list.add("fc");
// list.add("1");
//
// System.out.println(list);
// //反转
// Collections.reverse(list);
// System.out.println(list);
// //随机排
// Collections.shuffle(list);
// System.out.println(list);
// //升序
// Collections.sort(list);
// System.out.println(list);
Student s1 = new Student("小明",13);
Student s2 = new Student("小红",14);
Student s3 = new Student("小张",11);
Student s4 = new Student("小狗",8);
List<Student> stus = new ArrayList<Student>();
stus.add(s1);
stus.add(s2);
stus.add(s3);
stus.add(s4);
Collections.sort(stus,new Student());
Collections.swap(stus, 0, 2);
// Collections.replaceAll(list, oldVal, newVal)
for (Student stu : stus) {
System.out.println(stu.name + stu.age);
}
}
}
class Student implements Comparator<Student>{
int age;
String name;
public Student() {
}
public Student(String name,int age) {
this.age = age;
this.name = name;
}
@Override
public int compare(Student o1, Student o2) {
if (o1.age > o2.age) {
return 1;
}else if (o1.age < o2.age) {
return -1;
}else {
return 0;
}
}
}
本文地址:https://blog.csdn.net/Geohot_/article/details/107633282