public class ImageGraph1 {
private final int V;//顶点数
private int E ;//边数
private Bag<Integer>[] adj;//邻接表
public ImageGraph1(int v){
this.V = v;
this.E = 0;
this.adj =(Bag<Integer>[])new Bag[v];
for (int i = 0; i < v; i++) {
adj[v] = new Bag<Integer>();
}
}
public ImageGraph1(In in){
this(in.readInt());
int E = in.readInt();
for (int i = 0; i < E; i++) {
int v = in.readInt();
int w = in.readInt();
addEdge(v,w);
}
}
public int V(){return V;}
public int E(){return E;}
public void addEdge(int v,int w){
adj[v].add(w);
adj[w].add(v);
E++;
}
public Iterable<Integer> adj(int v){
return adj[v];
}
}
Bag类
public class Bag<Item> implements Iterable<Item> {
private Item[] items = (Item[])new Object[1];
private int N = 0;
public Bag() {
}
public void add(Item item){
if (N==items.length){resize(2*N);}
items[N++] = item;
}
private void resize(int max){
Item[] temps = (Item[])new Object[max];
for (int i = 0; i < N; i++) {
temps[i] = items[i];
}
items = temps;
}
public boolean isEmpty(){
return N==0;
}
public int size(){
return items.length;
}
@Override
public Iterator<Item> iterator() {
return null;
}
}
In控制台输入
public class In {
public int readInt(){
int in = 0;
InputStreamReader inputStreamReader = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(inputStreamReader);
try{
String inStr = reader.readLine();
Integer num = Integer.parseInt(inStr);
in = num.intValue();
}catch (Exception e){
e.printStackTrace();
}finally {
return in;
}
}
}