Tag Archives: Java

随机存储实现动态数组ArrayList

1、List

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.yusian;
 
public interface List<e> {
 
	static final int ELEMENT_NOT_FOUND = -1;
	/**
	 * 获取数组长度
	 * @return 数组长度
	 */
	public int size();
 
	/**
	 * 是否为空
	 * @return true / false
	 */
	public boolean isEmpty();
 
	/**
	 * 是否包含某元素
	 * @param element
	 * @return true / false
	 */
	public boolean contains(E element);
 
	/**
	 * 添加元素,默认添加到末尾位置
	 * @param element
	 */
	public void add(E element);
 
	/**
	 * 获取元素
	 * @param index 索引
	 * @return 值
	 */
	public E get(int index);
 
	/**
	 * 替换元素
	 * @param index 索引
	 * @param element 元素
	 * @return 原元素内容
	 */
	public E set(int index, E element);
 
	/**
	 * 添加元素
	 * @param index 索引
	 * @param element 元素值
	 */
	public void add(int index, E element);
 
	/**
	 * 移除元素
	 * @return 返回被移除元素
	 */
	public E remove(int index);
 
	/**
	 * 获取元素索引
	 * @param element
	 * @return 索引
	 */
	public int indexOf(E element);
 
	/**
	 * 清除数组
	 */
	public void clear();
}
 
</e>

2、AbstractList


[……]

继续阅读

Java基础之多线程的基本实现

1、实现方式一:继承Thread类,重写run方法

1
2
3
4
class Demo{
	public static void main(String [] args){
		SAThread thread1 = new SAThread();
		SAThread thread2 = new SAThrea[......]<p class="read-more"><a href="https://www.yusian.com/blog/java/2017/03/17/140012953.html">继续阅读</a></p>

Java基础之synchronized同步的使用

0、有关多线程的实现先参考:Java基础之多线程的基本实现
1、两个线程访问同一个对象,如果没有同步的限制,会出现一个意想不同的问题;
2、比如对象成员属性int i = 100;某方法每执行一次都会对该值进行减1操作,直接为0结束;
3、单次调用的基本流程应该是:start->{body}->end;[……]

继续阅读

Java基础知识之IO流的简单使用

1
2
3
4
5
6
7
import java.io.*;
class Demo{
	public static void main(String args[]){
		// 声明输入流的引用
		FileInputStream input = null;
		// 声明输出流的引用
		FileOutputStream[......]<p class="read-more"><a href="https://www.yusian.com/blog/java/2017/03/16/154032945.html">继续阅读</a></p>

Java基础知识之异常处理Exception的基本使用

1、设计一个Person类,该类包含一个age属性,即人的年龄,该值不能为负数;
2、在给该对象属性赋值为负数时,抛出异常,有两种方式可以实现;
2.1、在Person对象的setAge方法中将异常进行抛出,uncheked方式,能正常编译通过,运行时报错;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class Person{
	private int age;
	public void setAge(int age){
		if (age < 0){
			throw new RuntimeException("年龄不能为负数...");
		}
		this.age = age;
	}
}
class Demo{
	public static void main(String args[]){
		Person p = new Person();
		p.setAge(-1);
	}
}
运行结果:
Exception in thread "main" java.lang.RuntimeException: 年龄不能为负数...
	at Person.setAge(Person.java:5)
	at Demo.main(Demo.java:4)

[……]

继续阅读