1.ArrayList<ArrayList<Integer>> clusterList=new ArrayList<ArrayList<Integer>>();保存各连通子图中包含节点的行号:这样可以避免三维ArrayList。
2.ArrayList<ArrayList<String>> knowList=new ArrayList<ArrayList<String>>();保存各节点
3.ArrayList<String> oneList = (ArrayList<String>) knowList.get(i).clone();
int size1 = oneList.size();
ArrayList<String> anotherList = knowList.get(j);
oneList.retainAll(anotherList);
此处代码必须采用深复制,如果直接ArrayList<String> oneList = (ArrayList<String>) knowList.get(i),那么对oneList 的所有修改都会赋给knowList.get(i),这不是我们想要的。为什么会出现呢?因为String在Java属于特殊类,生成的实例为引用。但是不需要重载clone方法;
对类A实现clone方法。
public class A implements Cloneable {
public String name[];
public A(){
name=new String[2];
}
public Object clone() {
A o = null;
try {
o = (A) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return o;
}
其他深复制方法:
public Object deepClone() throws IOException, OptionalDataException,
ClassNotFoundException {
// 将对象写到流里
ByteArrayOutputStream bo = new ByteArrayOutputStream();
ObjectOutputStream oo = new ObjectOutputStream(bo);
oo.writeObject(this);
// 从流里读出来
ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
ObjectInputStream oi = new ObjectInputStream(bi);
return (oi.readObject());
}
}
public class DeepCopy2 {
/**
* @param args
*/
public static void main(String[] args) throws OptionalDataException,
IOException, ClassNotFoundException {
long t1 = System.currentTimeMillis();
Professor2 p = new Professor2("wangwu", 50);
Student2 s1 = new Student2("zhangsan", 18, p);
Student2 s2 = (Student2) s1.deepClone();
s2.p.name = "lisi";
s2.p.age = 30;
System.out.println("name=" + s1.p.name + "," + "age=" + s1.p.age); // 学生1的教授不改变。
long t2 = System.currentTimeMillis();
System.out.println(t2-t1);
}
}
/**
* @param args
*/
private ArrayList<ArrayList<String>> knowList=new ArrayList<ArrayList<String>>();
private ArrayList<ArrayList<Integer>> clusterList=new ArrayList<ArrayList<Integer>>();
//读取文件,存入ArrayList
public void readFile(String filePath) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(filePath));
String line = null;
while ((line = br.readLine()) != null) {
int i = 0;
String[] str = line.split(" ");
ArrayList<String> e = new ArrayList<String>();
while (i < str.length) {
e.add(str[i]);
i++;
}
//System.out.println(e.toString());
knowList.add(e);
}
br.close();
}
//选择各个节点,存入连通图
public void select() {
int num=0;
for (int i = 0; i < knowList.size(); i++) {
for (int j = i + 1; j < knowList.size(); j++) {
ArrayList<String> oneList = (ArrayList<String>) knowList.get(i).clone();
int size1 = oneList.size();
ArrayList<String> anotherList = knowList.get(j);
int size2 = anotherList.size();
oneList.retainAll(anotherList);
if (oneList.size() != 0) {
double result = Math.pow(oneList.size(), 2)
/ (size1 * size2);
System.out.println("result="+result);
if (result >= 0.2) {
num = isInCollection(j,i);
System.out.println("j="+j);
if (num == -1) {
ArrayList<Integer> record = new ArrayList<Integer>();
record.add(j);record.add(i);
clusterList.add(record);
System.out.println("j="+j);
} else if(num!=-2){
clusterList.get(num).add(j);
System.out.println("j input="+j);
}
}
}
}
}
}
//判断在那个连通子图中
public int isInCollection(int noColumn,int noLine){for(int i=0;i<clusterList.size();i++){
if(clusterList.get(i).contains(noLine)){
if(clusterList.get(i).contains(noColumn))
return -2;
return i;
}
}
return -1;
}