[英]Java Client Server with RMI
編寫一個應用 RMI 的 Java 客戶端-服務器程序。 客戶端程序生成兩個 arrays,類型為 class Integer。 客戶端調用遠程 object 中服務器端的方法。 服務端將這兩個arrays放入一個數組中,對數組進行排序,將排序后的數組返回給客戶端。 客戶端在控制台上顯示排序后的數組。 使用以下界面。 不要改變它。
import java.util.ArrayList;
import java.rmi.*;
public interface MergeInterface extends Remote {
public ArrayList mergeAndSort(ArrayList a, ArrayList b) throws RemoteException;
}
以下順序程序是將兩個 arrays 放在一起,排序數組並顯示:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> a = new ArrayList<Integer>();
ArrayList<Integer> b = new ArrayList<Integer>();
Random r = new Random();
int m = r.nextInt(900) + 100;
for (int i = 0; i < m; i++) {
a.add(r.nextInt(500) + 10);
}
int n = r.nextInt(900) + 100;
for (int i = 0; i < n; i++) {
b.add(r.nextInt(500) + 10);
}
a.addAll(b);
Collections.sort(a);
for (Integer i : a) {
System.out.println(i);
}
}
}
這是在 class 上實現接口的方法。 我還會考慮為此創建一個通用的 class 或強輸入 ArrayLists,但這需要更改接口。
public interface MergeInterface extends Remote {
public ArrayList mergeAndSort(ArrayList a, ArrayList b) throws RemoteException;
}
public static void main(String[] args) {
ArrayList a = new ArrayList();
ArrayList b = new ArrayList();
Random r = new Random();
int m = r.nextInt(900) + 100;
for (int i = 0; i < m; i++) {
a.add(r.nextInt(500) + 10);
}
int n = r.nextInt(900) + 100;
for (int i = 0; i < n; i++) {
b.add(r.nextInt(500) + 10);
}
for (Integer i : (ArrayList<Integer>)new MergeImpl().mergeAndSort(a, b)) {
System.out.println(i);
}
}
public static class MergeImpl implements MergeInterface {
@Override
public ArrayList mergeAndSort(ArrayList a,
ArrayList b) {
ArrayList merged = new ArrayList();
merged.addAll(a);
merged.addAll(b);
Collections.sort(merged);
return merged;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.