目录
java 输出重定向.......................................................................................2
java 输入重定向.....................................................................................2
java 中使用控制台输入.............................................................................2
java 输入输出重定向...............................................................................3
Java 中输入多组数据时的技巧...................................................................3
Java 中对象无重复排序..............................................................................4
单链表......................................................................................................7
循环链表...................................................................................................9
栈..............................................................................................................11
队列..........................................................................................................13
二叉排序树................................................................................................15
最小生成树(普里姆算法).........................................................................18
深度优先搜索和广度优先搜索.....................................................................21
最短路径求解—Dijkstra 算法.......................................................................26
1
1、 java 输出重定向
import java.io.FileOutputStream;
import java.io.PrintStream;
public class Main {
public static void main(String args[]) throws Exception {
PrintStream out = new PrintStream(new
FileOutputStream("pc2.estdout"));
System.setOut(out);
System.out.println("Hello World!");
out.close();
}
}
2、 java 输入重定向
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.util.Scanner;
public class Test08 {
public static void main(String[] args) throws Exception {
// 输入重定向
BufferedInputStream in = new BufferedInputStream(new
FileInputStream("std.in"));
System.setIn(in);
Scanner stdin = new Scanner(System.in);
int a = stdin.nextInt();
int b = stdin.nextInt();
System.out.print(a + b);
}
}
3、java 中使用控制台输入
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String str1 = input.nextLine();
System.out.println(str1);
2
}
}
4、java输入输出重定向
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
// 输入重定向
BufferedInputStream in = new BufferedInputStream(new
FileInputStream("std.in"));
System.setIn(in);
Scanner stdin = new Scanner(System.in);
int a = stdin.nextInt();
int b = stdin.nextInt();
// 输出重定向
PrintStream out = new PrintStream(new
FileOutputStream("estdout.pc2"));
System.setOut(out);
System.out.print(a + b);
out.close(); // 关闭重定向
}
}
Java 中输入多组数据时的技巧
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m = sc.nextInt(); // 表示有m组数据
Activity[] act = null;
Activity[][] t = new Activity[m][];
for (int i = 0; i < m; i++) {
int n = sc.nextInt(); // 表示有n个活动
act = new Activity[n];
for (int k = 0; k < n; k++) {
act[k] = new Activity();
}
3
for (int j = 0; j < n; j++) {
act[j].setTime(sc.nextInt(), sc.nextInt());
}
t[i] = act;
}
for (int i = 0; i < m; i++) {
sort(t[i]);
delete(t[i]);
System.out.println(count(t[i]));
}
}
Java 中对象无重复排序
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Scanner;
public class Per {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m = sc.nextInt(); // 代表m组测试数据
for (int i = 0; i < m; i++) {
int n = sc.nextInt(); // 代表n个长方形
Rect[] s = new Rect[n];
for (int j = 0; j < n; j++) {
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int height = b > c ? b : c;
int width = b < c ? b : c;
s[j] = new Rect(a, height, width);
}
HashSet set = new HashSet();
ArrayList list = new ArrayList();
for (int j = 0; j < s.length; j++) {
if (set.add(s[j]))
list.add(s[j]);
}
Object[] arr = list.toArray();
4
Arrays.sort(arr, new MyComparator());
for (Object j : arr)
System.out.println(j);
}
}
public static class MyComparator implements Comparator {
@Override
public int compare(Object o1, Object o2) {
Rect rc1 = (Rect) o1;
Rect rc2 = (Rect) o2;
if (rc1.id > rc2.id) {
return 1;
} else if (rc1.id < rc2.id) {
return -1;
} else {
if (rc1.height > rc2.height) {
return 1;
} else if (rc1.height < rc2.height) {
return -1;
} else {
if (rc1.width > rc2.width) {
return 1;
} else if (rc1.width < rc2.width) {
return -1;
} else
return 0;
}
}
}
}
public static class Rect {
int height;
int id;
int width;
public Rect(int i, int hei, int wid) {
height = hei;
id = i;
width = wid;
}
5
public String toString() {
return id + " " + height + " " + width;
}
public boolean equals(Object o) {
Rect t = (Rect) o;
return t.height == height && t.id == id && t.width == width;
}
public int hashCode() {
return 3 * height + 4 * id + 5 * width;
}
}
}
6
Java 中的常用数据结构
1. 单链表
public class Main {
public static void main(String[] args){
LinkList l = new LinkList();
l.add("A");
l.add("B");
l.add("C");
l.add("D");
l.add("E");
printList(l.head);
System.out.println();
LinkNode t = l.search("E");
System.out.println(t.data);
l.delete("E");
LinkNode t1 = l.search("E");
System.out.println(t1.data);
}
public static void printList(LinkNode tmp){
System.out.print(tmp.data+" ");
if(tmp.next!=null){
printList(tmp.next);
}
}
}
//定义节点类
class LinkNode{
Object data = null;
LinkNode next = null;
public LinkNode(Object data){
//保存节点内容
//保存节点的下一个节点
this(data,null);
}
public LinkNode(Object data,LinkNode tmp){
this.data = data;
this.next = tmp;
}
}
//定义链表类,对节点进行封装
class LinkList{
7
LinkNode head = null;
LinkNode p = null;
//向链表中增加成员
public void add(Object data){
//定义头结点
//p节点用来保存节点移动后的当前位置
LinkNode tmp = new LinkNode(data);
if(head==null){
head = tmp;
p = tmp;
p.next = null;
}else{
p.next = tmp;
p = tmp;
p.next = null;
}
}
//查找链表中的某节点
public LinkNode search(Object data){
//用于判断节点是否找到
p = head;
boolean i = false;
while(p!=null){
if(p.data==data){
i = true;
break;
}
p = p.next;
}
if(i==false){
return (new LinkNode("不存在"));
}else{
return p;
}
}
//删除链表中某节点
public void delete(Object data){
if(search(data).data=="不存在"){
System.out.println("找不到该节点,删除失败!");
}else{
LinkNode tmp;
tmp = search(data);
if(tmp.next!=null){
//找到要删除的节点tmp
LinkNode ch;
//通过将要删除的节点(即tmp)的下一个节点的内
ch = tmp.next;
//然后再将tmp的下一个节点从链表中移走达到删
容传给tmp,
除tmp的目的
8