Introduction
The java so example is extracted from the most popular open source projects, you can refer to the following example for usage.
Programming language: Java
Class/type: SO
Example#1File:
Example198.javaProject:
nielspedersen/advanced-programming
public static void main(String[] args) throws IOException, ClassNotFoundException {
File f = new File("objects2.dat");
if (!f.exists()) {
System.out.println("Creating objects and writing them to file:");
SC c = new SC();
SO o1 = new SO(1, c), o2 = new SO(2, c);
o1.c.ci = 3;
o2.c.ci = 4; // update the shared c twice
o1.cprint();
o2.cprint(); // prints i1c4 i2c4
OutputStream os = new FileOutputStream(f);
ObjectOutputStream oos1 = new ObjectOutputStream(os);
oos1.writeObject(o1);
oos1.flush();
ObjectOutputStream oos2 = new ObjectOutputStream(os);
oos2.writeObject(o2);
oos2.close();
System.out.println("\nRun the example again to read objects from file");
} else {
System.out.println("Reading objects from file (non-shared c):");
InputStream is = new FileInputStream(f);
ObjectInputStream ois1 = new ObjectInputStream(is);
SO o1i = (SO) (ois1.readObject());
ObjectInputStream ois2 = new ObjectInputStream(is);
SO o2i = (SO) (ois2.readObject());
o1i.cprint();
o2i.cprint(); // prints i1c4 i2c4
o1i.c.ci = 5;
o2i.c.ci = 6; // update two different c's
o1i.cprint();
o2i.cprint(); // prints i1c5 i2c6
f.delete();
}
System.out.println();
}
Example#2File:
PSO.javaProject:
proverbs/MyRepo
// 求一个基本交换序列作用于编码arr后的编码
public void add(int[] arr, ArrayList<SO> list) {
int temp = -1;
SO s;
for (int i = 0; i < list.size(); i++) {
s = list.get(i);
temp = arr[s.getX()];
arr[s.getX()] = arr[s.getY()];
arr[s.getY()] = temp;
}
}