public class Info {
private String name;
private int threadId;
private int time;
private int stateCode;
private String ip;
public Info(String name, int threadId, int time, int stateCode, String ip) {
super();
this.name = name;
this.threadId = threadId;
this.time = time;
this.stateCode = stateCode;
this.ip = ip;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getThreadId() {
return threadId;
}
public void setThreadId(int threadId) {
this.threadId = threadId;
}
public int getTime() {
return time;
}
public void setTime(int time) {
this.time = time;
}
public int getStateCode() {
return stateCode;
}
public void setStateCode(int stateCode) {
this.stateCode = stateCode;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "Info[name=" + name + ", time=" + time + "]";
}
}
import java.util.ArrayList;
public class InfoOperator {
public static void main(String[] args) {
String[] names = { "aaa", "bbb", "ccc", "ddd", "eee" };
String[] ips = { "192.168.1.1", "192.168.1.2", "192.168.1.3",
"192.168.1.4", "192.168.1.5" };
int count = 10;
ArrayList<Info> Infos;
Infos = new ArrayList<Info>();
Info info;
int idx;
int threadId;
int time;
int stateCode;
for (int i = 0; i < count; i++) {
idx = (int) (Math.floor(Math.random() * names.length));
threadId = (int) (Math.floor(Math.random() * 1000));
time = (int) (Math.floor(Math.random() * 1000));
stateCode = (int) (Math.floor(Math.random() * 1000));
info = new Info(names[idx], threadId, time, stateCode, ips[idx]);
Infos.add(info);
}
System.out.println("本次实验数据:");
for (int i = 0; i < count; i++) {
System.out.println(Infos.get(i));
}
System.out.println("-------------------");
ArrayList<ArrayList<Info>> groupList = InfoOperator.groupByName(Infos);
System.out.println(groupList);
for (int i = 0; i < groupList.size(); i++) {
ArrayList<Info> group = groupList.get(i);
System.out.print(group.get(0).getName() + ":");
for (int j = 0; j < group.size(); j++) {
System.out.print(group.get(j).getTime() + "\t");
}
System.out.println();
}
}
public static ArrayList<ArrayList<Info>> groupByName(ArrayList<Info> Infos) {
ArrayList<ArrayList<Info>> groupList = new ArrayList<ArrayList<Info>>();
Info info;
ArrayList<Info> group;
int low;
int high;
int mid;
boolean added;
for (int i = 0; i < Infos.size(); i++) {
info = Infos.get(i);
if (groupList.size() > 0) {
added = false;
low = 0;
mid = low;
high = groupList.size() - 1;
//根据姓名排序,建立分组
while (low <= high) {
mid = (low + high) / 2;
group = groupList.get(mid);
int com = info.getName().compareTo(group.get(0).getName());
if (com == 0) {//将数据添加到已有分组
int begin = 0;
int last = group.size()-1;
int center = begin;
//组内数据根据时间排序
while (begin <= last) {
center = (begin + last) / 2;
if (info.getTime() > group.get(center).getTime()) {
begin = center + 1;
} else {
last = center - 1;
}
}
group.add(last+1, info);
added = true;
break;
} else if (com < 0) {
high = mid - 1;
} else if (com > 0) {
low = mid + 1;
}
}
if (!added) {//建立新分组
group = new ArrayList<>();
group.add(info);
groupList.add(high + 1, group);
}
} else {
group = new ArrayList<>();
group.add(info);
groupList.add(group);
}
}
return groupList;
}
}
上一页 [1] [2]