kk Blog —— 通用基础


date [-d @int|str] [+%s|"+%F %T"]
netstat -ltunp
sar -n DEV 1

Java 常用数据结构

Java class 的排序

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
import java.util.*;

class Node implements Comparable
{
	int x,y;
	public int compareTo(Object obj){
		Node oo=(Node)obj;
		if(oo.x < this.x || oo.x == this.x && oo.y <this.y)return 1;
		return -1;
	}
}

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner cin = new Scanner(System.in);
		Node a[]=new Node[11];
		int i,j,k,l;
		for(i=1;i<=10;i++) {
			a[i]=new Node();
			a[i].x=Math.abs(5-i); a[i].y=10-Math.abs(7-i);
		}
		Arrays.sort(a, 1, 11);
		for(i=1;i<=10;i++)System.out.println(a[i].x+" "+a[i].y);
	}
}

Java Vector 的使用

1
2
3
4
5
6
7
8
9
10
11
12
13
import java.util.*;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner cin = new Scanner(System.in);
		Vector V=new Vector();
		V.add(1); V.add(1.234); V.add('a'); V.add("abcd");
		System.out.println((Integer)V.get(0));
		System.out.println((Double)V.get(1));
		System.out.println((Character)V.get(2));
		System.out.println((String)V.get(3));
	}
}

Java 队列的使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import java.util.*;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner cin = new Scanner(System.in);
		Queue Q=new LinkedList<Integer>();
		int i,j,k,l;
		while(!Q.isEmpty())Q.poll(); // 清空队列
		for(i=1;i<=10;i++)Q.offer(i);
		for(i=1;i<=10;i++){
			k=(Integer)Q.poll();
			System.out.println(k);
		}
	}
}

Java 优先队列的使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.*;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner cin = new Scanner(System.in);
		PriorityQueue<Integer> Q = new PriorityQueue<Integer>(30,
			new Comparator<Integer>(){
				public int compare(Integer aa, Integer bb) {
					if(aa < bb)return 1;
					return -1;
				}
			});
		int i,j,k,l;
	   
		Q.clear();
		for(i=1;i<=10;i++)Q.offer(i);
		while(!Q.isEmpty()) {
			k=(Integer)Q.poll();
			System.out.println(k);
		}
	}
}

http://www.yiibai.com/java/util/java_util_hashset.html

Java Collection 的使用

boolean contains(Object o) 如果此set包含指定的元素,此方法返回true。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.util.*;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner cin = new Scanner(System.in);
		Collection set=new HashSet();
		Collection set1=new ArrayList();
		Collection set2=new LinkedList();

		ArrayList LI=new ArrayList();
		LinkedList LD=new LinkedList();

		int i,j,k,l;

		set.clear();          // set1 set2 LI LD 用法类似
		set.add(1); set.add(1.234); set.add('a'); set.add("abcd");
		System.out.println(set.toString());
		set.remove(1); set.remove(3);
		System.out.println(set.toString());
	}
}

http://www.yiibai.com/java/util/java_util_hashmap.html

Java HashMap(很好用) TreeMap

boolean containsKey(Object key) 如果此映射包含指定键的映射此方法返回true。

boolean containsValue(Object value) 如果此映射一个或多个键映射到指定值,该方法返回true

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.*;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner cin = new Scanner(System.in);
		Map map=new HashMap();
		int i,j,k,l;
		map.clear();
		for(i=1;i<=10;i++)map.put(Math.pow(10,i), i);
		for(i=1;i<=10;i++)System.out.println((Integer)map.get(Math.pow(10,i)));
		System.out.println();
	   
		Map tree=new TreeMap();
		tree.clear();
		for(i=1;i<=10;i++)tree.put(i,10-i);
		for(i=1;i<=10;i++)
		{
			k=(Integer)tree.get(i);
			System.out.println(k);
		}
	}
}

language, java

« Java I/O Java MouseListener & MouseMotionListener »