使用链表完成疫情的地区信息统计
定义地区信息统计的标准
public interface Area {
// 定义地区的标准
public String getName();
}
链表的标准
public interface ILink <E>{
void add(E e); //增加数据
int size(); // 获取数据个数
boolean isEmpty();// 判断是否空集合
Object[] toArray();//将集合元素以数组的形式返回
E get(int index); // 根据索引获取数据
void set(int index,E data); // 修改索引数据
boolean contains(E data); // 判断数据是否存在
void remove(E e); // 数据删除
void clean(); // 清空集合
}
链表的具体实现
public class LinkImpl<E> implements ILink<E> {
// -------------以下为Link类中定义的方法------------
private Node root; // 保存根元素
private int count;// 保存数据个数
private int foot; // 描述的是操作数组的脚标
private Object[] returnData;//返回的数据保存
@Override
public void add(E e) {
if (e == null) { // 保存的数据为null
return; // 方法调用直接结束
}
// 数据本身是不具有关联特性的,只有Node类有,那么要想实现关联数据就必须将数据包装在Node类之中
Node newNode = new Node(e);//创建一个新的节点
if (this.root == null) {//现在没有根节点
this.root = newNode;//第一个节点作为根节点
} else {//根节点存在
this.root.addNode(newNode);//将新节点保存在合适的位置
}
this.count++;
}
@Override
public int size() {
return this.count;
}
@Override
public boolean isEmpty() {
return this.count == 0;
}
@Override
public Object[] toArray() {
if (this.isEmpty()) {
return null;
}
this.foot = 0;//脚标清零
this.returnData = new Object[this.count];//根据count来确定数组的长度,开辟数组
this.root.toArrayNode();//利用Node类进行递归数据获取
return this.returnData;
}
@Override
public E get(int index) {
if (index >= this.count) {//索引应该在指定范围之内
return null;//方法结束
}//索引数据的获取应该由Node类完成
this.foot = 0;//重置索引
return this.root.getNode(index);
}
@Override
public void set(int index, E data) {
if(index >= this.count){ // 索引应该在指定的范围之内
return ; // 方法结束
} // 索引数据的获取应该由Node类完成
this.foot = 0 ; // 重置索引的下标
this.root.setNode(index,data); // 修改数据
}
@Override
public boolean contains(E data) {
if(data == null){
return false; // 没有数据
}
return this.root.containsNode(data); // 交给Node类判断
}
@Override
public void remove(E data) {
if(this.contains(data)){
// 判断数据是否存在
if(this.root.data.equals(data)){
// 根节点为要删除节点
this.root = this.root.next ; // 根的下一个节点
}else{ // 交由Node类负责删除
this.root.next.removeNode(this.root,data);
}
this.count --;
}
}
@Override
public void clean() {
this.root = null ; // 后续的所有节点都没了
this.count = 0 ; // 个数清零
}
private class Node {
private E data;
private Node next;
private Node(E data) {
this.data = data;
}
// 第一次调用:this = LinkImpl.root.addNode()
// 第二次调用:this = LinkImpl.root.next
// 第三次调用:this = LinkImpl.root.next.next
public void addNode(Node newNode){
// 保存新的Node数据
if(this.next == null){
// 当前节点的下一个节点为nulll
this.next = newNode;//保存当前节点
}else{
this.next.addNode(newNode);
}
}
// 第一次调用:this = LinkImpl.root
// 第二次调用:this = LinkImpl.root.next
// 第三次调用 :this = LinkImpl.root.next.next
public void toArrayNode() {
LinkImpl.this.returnData[LinkImpl.this.foot++] = this.data;
if (this.next != null) {//还有下一个数据
this.next.toArrayNode();
}
}
public E getNode(int index){
if(LinkImpl.this.foot ++ == index){
// 索引相同
return this.data; // 返回当前数据
}else{
return this.next.getNode(index);
}
}
public void setNode(int index,E data){
if(LinkImpl.this.foot ++ == index){
// 索引相同
this.data = data ; // 修改数据
}else{
this.next.setNode(index,data);
}
}
public boolean containsNode(E data){
if(this.data.equals(data)){
// 对象比较
return true;
}else{
if(this.next == null){
// 没有后续节点
return false; // 找不到
}else{
return this.next.containsNode(data);// 向后继续判断
}
}
}
public void removeNode(Node previous, E data) {
if (this.data.equals(data)) {
previous.next = this.next; // 空出当前节点
} else {
if (this.next != null) {
// 有后续节点
this.next.removeNode(this, data); // 向后继续删除
}
}
}
}
}
Abroad类国外数据对象
public class Abroad implements Area{
private Integer id;
private String name;
private Integer newConfirmedCases;
private Integer newDeaths;
private Integer existingConfirmedDiagnosis;
private Integer cumulativeDiagnosis;
private Integer cumulativeDeaths;
private Integer cumulative_cure;
public Abroad() {
}
public Abroad(String name, Integer newConfirmedCases, Integer newDeaths, Integer existingConfirmedDiagnosis, Integer cumulativeDiagnosis, Integer cumulativeDeaths, Integer cumulative_cure) {
this.name = name;
this.newConfirmedCases = newConfirmedCases;
this.newDeaths = newDeaths;
this.existingConfirmedDiagnosis = existingConfirmedDiagnosis;
this.cumulativeDiagnosis = cumulativeDiagnosis;
this.cumulativeDeaths = cumulativeDeaths;
this.cumulative_cure = cumulative_cure;
}
public Abroad(Integer id, String name, Integer newConfirmedCases, Integer newDeaths, Integer existingConfirmedDiagnosis, Integer cumulativeDiagnosis, Integer cumulativeDeaths, Integer cumulative_cure) {
this.id = id;
this.name = name;
this.newConfirmedCases = newConfirmedCases;
this.newDeaths = newDeaths;
this.existingConfirmedDiagnosis = existingConfirmedDiagnosis;
this.cumulativeDiagnosis = cumulativeDiagnosis;
this.cumulativeDeaths = cumulativeDeaths;
this.cumulative_cure = cumulative_cure;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Override
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getNewConfirmedCases() {
return newConfirmedCases;
}
public void setNewConfirmedCases(Integer newConfirmedCases) {
this.newConfirmedCases = newConfirmedCases;
}
public Integer getNewDeaths() {
return newDeaths;
}
public void setNewDeaths(Integer newDeaths) {
this.newDeaths = newDeaths;
}
public Integer getExistingConfirmedDiagnosis() {
return existingConfirmedDiagnosis;
}
public void setExistingConfirmedDiagnosis(Integer existingConfirmedDiagnosis) {
this.existingConfirmedDiagnosis = existingConfirmedDiagnosis;
}
public Integer getCumulativeDiagnosis() {
return cumulativeDiagnosis;
}
public void setCumulativeDiagnosis(Integer cumulativeDiagnosis) {
this.cumulativeDiagnosis = cumulativeDiagnosis;
}
public Integer getCumulativeDeaths() {
return cumulativeDeaths;
}
public void setCumulativeDeaths(Integer cumulativeDeaths) {
this.cumulativeDeaths = cumulativeDeaths;
}
public Integer getCumulative_cure() {
return cumulative_cure;
}
public void setCumulative_cure(Integer cumulative_cure) {
this.cumulative_cure = cumulative_cure;
}
@Override
public String toString() {
return "[Abroad:" +
"id=" + id +
", name='" + name +
", newConfirmedCases=" + newConfirmedCases +
", newDeaths=" + newDeaths +
", existingConfirmedDiagnosis=" + existingConfirmedDiagnosis +
", cumulativeDiagnosis=" + cumulativeDiagnosis +
", cumulativeDeaths=" + cumulativeDeaths +
", cumulative_cure=" + cumulative_cure +"]";
}
}
链表与对象的结合
public class DataSource {
private ILink<Area> all = new LinkImpl<Area>();//保存多个宠物信息
public void add(Area area){
// 追加地区,数据存放
this.all.add(area);//链表中存放对象
}
public void delete(Area area){
this.all.remove(area);
}
public ILink<Area> search(String name){
ILink<Area> searchResult = new LinkImpl<Area>();//保存查询结果
Object result[] = this.all.toArray();//获取全部数据
if (result != null){
for (Object obj: result){
Area area =(Area) obj;
if (area.getName().contains(name)){
searchResult.add(area);
}
}
}
return searchResult;
}
}