一周的学习

这一个星期学到的有:异常,集合,IO流,多线程。

异常

1.我个人理解异常就是在程序运行的过程中发生一些不正常的事件,它会结束正在运行的程序。 2.Java异常处理:Java它有对异常处理的能力,Java有5个关键字来处理异常:try、catch、finally、throw、throws try:它执行可能产生异常的代码 catch:它是用来捕获异常的 finally:它是无论程序出不出现异常,它都会执行的代码 throw:自动抛出异常和手动抛出异常 throws:声明方法可能要抛出的各种异常

public static void main(String[] args) {
		// TODO Auto-generated method stub
		// 异常

		try {
			System.out.print("请输入第一个数:");
			Scanner scanner = new Scanner(System.in);
			int a = scanner.nextInt();
			System.out.print("请输入第一个数:");
			int b = scanner.nextInt();
			System.out.println(a / b);
		} catch (InputMismatchException exception) {
			System.out.println("除数和被除数必须为正数");
		} catch (ArithmeticException exception) {
			System.out.println("除数不能为0");
		} catch (Exception e) {
			System.out.println("其它异常");
		} finally {
			System.out.println("谢谢使用");
		}
	}
复制代码
public static void main(String[] args) {
		// TODO Auto-generated method stub

		try {
			Scanner scanner = new Scanner(System.in);
			System.out.print("请输入课程代号(1~3之间的数字):");
			int a = scanner.nextInt();
			switch (a) {
			case 1:
				System.out.println("C#编程");
				break;
			case 2:
				System.out.println("C语言");
				break;
			case 3:
				System.out.println("JAVA");
				break;
			default:
				System.out.println("只能输入1~3之间的数字");
				break;
			}
		} catch (InputMismatchException exception) {
			System.out.println("输入的数字必须为数字");
		} finally {
			System.out.println("欢迎提出建议");
		}
	}
复制代码
public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			Person person = new Person();
			person.setAge(10);
			person.setSex("无");
			System.out.println("创建成功");
		} catch (Exception e) {
			System.err.println(e.getMessage());
		} finally {
			System.out.println("人是一个高等级动物");
		}
	}
}

class Person {
	private int age;
	private String sex;

	//
	public int getAge() {
		return age;
	}
    //throws Exception是一个异常抛出的管道
	public void setAge(int age) throws Exception {
		if (age > 0) {
			this.age = age;
		} else {
			throw new Exception("年龄必须大于0"); //创建的异常对象
		}
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) throws Exception {
		if ("男".equals(sex) || "女".equals(sex)) {
			this.sex = sex;
		} else {
			throw new Exception("必须是男或女");
		}
	}
复制代码

这些是我做的练习

集合

1.collection集合:list 、set 2.list: arrayList 、LinkedList 3.arrayList

arrayList

public class Demo01 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		List list = new ArrayList();
		list.add(new Dog("强强", 12));
		list.add(new Dog("球球", 12));
		list.add(new Dog("希希", 12));
		list.add(1, new Dog("安安", 11));
		Dog dog2 = new Dog("大大", 13);
		list.add(dog2);
		// 删除
		// E remove(int index)
		// 移除此列表中指定位置上的元素。
		// boolean remove(Object o)
		// 移除此列表中首次出现的指定元素。
		list.remove(0);
		list.remove(dog2);
        // list.clear();// 移除全部
		// 查询
		list.set(1, new Dog("丽丽", 12));
		for (int i = 0; i < list.size(); i++) {
			// get是索取某个索引位置上的内容
			Dog dog = (Dog) list.get(i);
			System.out.println(dog.toString());
		}
	}
}
复制代码

LinkedList

public class Demo01 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		LinkedList list = new LinkedList();// 父类引用指向子类对象
		// 创建对象
		Dog pingping = new Dog("平平", 2);
		Dog tutu = new Dog("兔兔", 2);
		Dog tu2 = new Dog("图图", 2);
		// 添加到集合中
		list.add(pingping);
        	list.addFirst(tutu);
		list.addLast(tu2);
		// 获得第一个元素
		System.out.println(list.getFirst().toString());
		System.out.println(list.getLast().toString());
		// 删除第一个
		list.removeFirst();
		list.removeLast();
		System.out.println("~~~~~~~~~~~~");
		for (int i = 0; i < list.size(); i++) {
			Dog dog = (Dog) list.get(i);
			System.out.println(dog.toString());
		}
	}
}
复制代码

4.set:hashset

hashset

public class Demo04 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Set set = new HashSet();
		Random random = new Random();
		while (set.size() <= 10) {
			set.add(random.nextInt(20) + 1);
		}
		for (Integer integer : set) {
			System.out.println(integer);
		}
	}
}
复制代码
public class Demo05 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//去除重复的字符
		Scanner scanner = new Scanner(System.in);
		System.out.print("请输入一段字符:");
		String aString = scanner.nextLine();
		System.out.println(aString);
		HashSet hs = new HashSet();
		char[] arr = aString.toCharArray();
		for (char c : arr) {
			hs.add(c);
		}
		for (Character ch : hs) {
			System.out.print(ch);
		}
	}
}
复制代码

5.泛型

泛型就是允许在编写集合的时候,先限制集合的数据处理类型

6.Map

map:将键映射到值的对象,一个映射不能包含重复的键;每个键最多只能映射到一个值

HashMap

public static void main(String[] args) {
		// TODO Auto-generated method stub
		Map map = new HashMap();
		Scanner scanner = new Scanner(System.in);
		System.out.print("请输入一段字符串:");
		String a = scanner.next();
		char[] chars = a.toCharArray();
		for (char c : chars) {
			if (map.containsKey(c)) {
				map.put(c, map.get(c) + 1);
			} else {
				map.put(c, 1);
			}
		}
		for (Character character : map.keySet()) {
			System.out.println(character + ":" + map.get(character));
		}
	}
复制代码

7.遍历

list遍历可以用for循环也可以用foreach set只能用foreach

8.迭代器

public static void main(String[] args) {
		// TODO Auto-generated method stub
		Map map = new HashMap();
		// put方法如果集合当中没有对应键值则新增,如果有则修改;
		map.put("YL", "yl");
		map.put("LY", "ly");
		map.put("XY", "xy");
		/*
		 * //第一种迭代器 //获取所有的键 并且存放到set集合中; Set set = map.keySet();
		 * //set集合的迭代器; Iterator iterator = set.iterator();
		 * //首先判断set集合中是否有下一个元素; //如果有则获取下一个元素; while (iterator.hasNext()) {
		 * //next:获取当前指针指向的对象的引用,获得之后,自己把指针向后移一位; String iString =
		 * iterator.next(); System.out.println(iString + "\t" +
		 * map.get(iString)); }
		 */

		// 第二种迭代器
		Set> set2 = map.entrySet();
		for (Map.Entry entry : set2) {
			System.out.println(entry);
		}
	}
复制代码

IO流

1.file类:就是文件在系统当中具体的位置 2.创建文件和文件夹

public static void main(String[] args) {
		// TODO Auto-generated method stub
		// Demo01();
		// Demo02();
		File file = new File("E:\\JAVA\\新建文件夹");
		if (file.mkdirs()) {
			System.out.println("创建成功");
        } else {
			System.out.println("创建失败");
		}
	}

	private static void Demo02() {
		File file = new File("E:\\JAVA\\新建文件夹\\abd");
		if (file.mkdir()) {
			System.out.println("创建成功");
		} else {
			System.out.println("创建失败");
		}
	}

	// 创建空的文件
	private static void Demo01() {
		File file = new File("E:\\JAVA\\新建文件夹\\abd.txt");
		try {
			// file.createNewFile();
			if (file.createNewFile()) {
				System.out.println("创建成功");
			} else {
				System.out.println("创建失败");
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
复制代码

3.重命名和删除

public static void main(String[] args) {
		// Demo01();
		File newfile = new File("E:\\JAVA\\新建文件夹\\abd2");
		if (newfile.delete()) {
			System.out.println("删除成功");
		}
	}

	// 修改
	private static void Demo01() {
		File file = new File("E:\\JAVA\\新建文件夹\\abd");
		// 新的路径
		File newfile = new File("E:\\JAVA\\新建文件夹\\abd2");
		if (file.renameTo(newfile)) {
			System.out.println("修改成功");
		} else {
			System.out.println("修改失败");
		}
	}
复制代码

io流

1.io流:Java对数据的读写(传输)操作通过流的方式 2.字节流:可以操作任何数据 3.字符流:用于操作纯字符文件

字节流

public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		// 创建输入流
		FileInputStream fileInputStream = new FileInputStream(
				"E:\\JAVA\\新建文件夹\\一生所爱.flac");
		// 2、创建输出流
        FileOutputStream fStream = new FileOutputStream("E:\\JAVA\\新建文件夹\\一生所爱2.flac");
		// 3、读写
		int b;
		while ((b = fileInputStream.read()) != -1) {
			fStream.write(b);
		}
		fileInputStream.close();
		fStream.close();
	}
复制代码

字符流

public static void main(String[] args) {
		// TODO Auto-generated method stub
		FileReader fileReader = null;
		FileWriter fileWriter = null;
		try {
			fileReader = new FileReader("E:\\JAVA\\IO流\\爱你.txt");
			fileWriter = new FileWriter("E:\\JAVA\\IO流\\爱你爱你.txt");
			int c;
			while ((c = fileReader.read()) != -1) {
				fileWriter.write(c);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				fileReader.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				fileWriter.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
复制代码
public static void main(String[] args) {
		// TODO Auto-generated method stub
		BufferedReader bf = null;
		BufferedWriter bw = null;
		try {
			bf = new BufferedReader(new FileReader("E:\\JAVA\\IO流\\abc.txt"));
			String line = null;
			StringBuffer stringBuffer = new StringBuffer();
			while ((line = bf.readLine()) != null) {
				stringBuffer.append(line);
			}
			System.out.println("替换前:" + stringBuffer);
			bw = new BufferedWriter(new FileWriter("E:\\JAVA\\IO流\\abc.txt"));
			// 替换字符
			String string = stringBuffer.toString().replace("name", "XL").replace("type", "哈士奇").replace("master",
					"AL");
			bw.write(string);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			try {
				bf.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				bw.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
复制代码

多线程

1.多线程:程序执行的多个路径 2.多线程并发:可以提高程序的执行效率,可以同时完成多个任务 3.多线程的运行:多个程序同时在运行 4.实现多线程运行的方式:1.子类继承thread类,重写run方法。当执行start方法时,直接执行子类的run方法 2.实现runnable接口

public static void main(String[] args) {
		// TODO Auto-generated method stub
		Mythread mythread = new Mythread();
		Mythread mythread2 = new Mythread();
		mythread.setName("你好,来自线程" + mythread);
		mythread.start();
		mythread2.setName("你好,来自线程" + mythread2);
		mythread2.start();
	}

}

class Mythread extends Thread {
	public void run() {
		for (int i = 0; i < 20; i++) {
			System.out.println(Thread.currentThread().getName() + ":" + i);
		}
	}
}
复制代码

休眠线程

public static void main(String[] args) {
		// TODO Auto-generated method stub
		// DemoA();
		for (int i = 10; i > 0; i--) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("敌军还有:" + i + "S到达战场");
		}
		System.out.println("全军出击");
	}

	// ********************************************************************
	private static void DemoA() {
		new Thread() {
			public void run() {
				for (int i = 0; i < 10; i++) {
					System.out.println(Thread.currentThread().getName() + ":" + i);
					try {
						Thread.sleep(1500);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}.start();
		new Thread() {
			public void run() {
				for (int i = 0; i < 10; i++) {
					System.out.println(Thread.currentThread().getName() + ":" + i);
					try {
						Thread.sleep(1500);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}.start();
	}
}
复制代码

线程的优先级

public static void main(String[] args) {
		// TODO Auto-generated method stub
		Thread thread = new Thread(new MyThread2(), "线程A");
		Thread thread2 = new Thread(new MyThread2(), "线程B");
		// 设置线程的优先级
		thread.setPriority(thread.MAX_PRIORITY);
		thread2.setPriority(thread.MIN_PRIORITY);
		thread.start();
		thread2.start();
		System.out.println(thread.getPriority());
		System.out.println(thread2.getPriority());
	}

}

class MyThread2 implements Runnable {

	@Override
	public void run() {
		// TODO Auto-generated method stub
		for (int i = 0; i < 100; i++) {
			System.out.println(Thread.currentThread().getName() + "~~~~~~" + i);
		}
	}

}
复制代码

接口实现线程

public static void main(String[] args) {
		// TODO Auto-generated method stub
		// 创建线程对象
		MyRunnable myRunnable = new MyRunnable();
		// 开启线程
		Thread thread = new Thread(myRunnable);
		Thread thread2 = new Thread(myRunnable);
		thread.start();
		thread2.start();

	}
}

class MyRunnable implements Runnable {

	@Override
	public void run() {
		// TODO Auto-generated method stub
		for (int i = 0; i < 10; i++) {
			System.out.println(Thread.currentThread().getName() + ":" + i);
		}
	}
}
复制代码

多线程执行状态

public static void main(String[] args) {
		// TODO Auto-generated method stub
		Thread thread = new Thread(new MyThread3());
		System.out.println("线程已创建");
		thread.start();
		System.out.println("线程就绪");
	}
}

class MyThread3 implements Runnable {

	@Override
	public void run() {
		// TODO Auto-generated method stub
		System.out.println("程序在运行");
		try {
			System.out.println("马上进入休眠期");
			Thread.sleep(1000);
			System.out.println("经过短暂的休眠之后");
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
复制代码

线程的礼让

public static void main(String[] args) {
		// TODO Auto-generated method stub
		Thread thread = new Thread(new Mythread5(), "线程A");
		Thread thread2 = new Thread(new Mythread5(), "线程B");
		thread.start();
		thread2.start();
	}

}

class MyThread5 implements Runnable {

	@Override
	public void run() {
		// TODO Auto-generated method stub
		for (int i = 0; i < 5; i++) {
			System.out.println(Thread.currentThread().getName() + "正在运行");
			if (i == 3) {
				System.out.println("线程的礼让");
				Thread.yield();
			}
		}
	}
复制代码

多线程练习题

public static void main(String[] args) {
		// TODO Auto-generated method stub
		Thread thread = new Thread(new MyThread4());
		thread.setPriority(10);
		thread.start();
		for (int i = 1; i < 50; i++) {
			System.out.println("普通号:" + i + "号病人在看病");
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (i == 10) {
				try {
					thread.join();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

	}

}

class MyThread4 implements Runnable {
	// 特需号
	@Override
	public void run() {
		// TODO Auto-generated method stub
		for (int i = 1; i < 11; i++) {
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("特需号:" + i + "号病人在看病!");
		}
	}
}
这些就是一星期的总结

复制代码

转载于:https://juejin.im/post/5c2cd257e51d4525b7096fbd

你可能感兴趣的:(一周的学习)