软件构造Lab2漫谈(1)

这次分为三部分。
第一部分主要是考察对泛型编程的理解和应用。
还有关于Set,Map,List等泛型数据的掌握情况。
总结几个在实验中遇到的问题:
1.如何删除List中的元素

		List<Piece> all=this.board.Piece();
		Iterator<Piece> iterator =all.iterator();
		while(iterator.hasNext()) {
			Piece tmPiece=iterator.next();
			if(tmPiece.equals(p)) {
				iterator.remove();
				return true;
			}
		}
		return false;

上面的代码是我P3中的部分内容
删除元素的时候一定要使用迭代器,而不是foreach方法,因为for循环中一边迭代一边对元素做操作是可能报错的,也有可能不报错但是并不能达到删除目的(我遇到的是第二种情况)。
2.Map<A,B>的访问细节
Map实际上是一个键值-值的映射表,它下面有一个方法是keyset(),可以返回集合A,给出P2中部分代码作为例子

	private ArrayList<Relation> RelationList(int index) {//返回邻接矩阵的一行
		ArrayList<Relation> outList =new ArrayList<Relation>();
		List<Person> tlist=this.PersonList();
		Person p1= tlist.get(index);
			for(Person p2:graph.targets(p1).keySet()) {
				outList.add(new Relation(p1, p2));
			}
		return outList;
	}

这里的graph的targets方法对应于Graph中的,是返回一个Map。在此处我要得到图的邻接表中的一行,而map中存储的键值集合keySet保存的Person正是我所需要的

		assertEquals((Integer)1,testGraph.sources(2.0).get(1.0));
    	assertEquals((Integer)2,testGraph.sources(2.0).get(4.0));

这部分是get的一个使用,得到B中元素,键值对应的值
3.Java中队列的使用,和C++有点不同
下面是我的getDistance方法

public int getDistance(Person P1,Person P2) {
		if(!graph.vertices().contains(P1)||!graph.vertices().contains(P2)) 
			return -1;
		else {
			
		
			int dis=0;
			int size =graph.vertices().size();
			int [] visit=new int[size];
            for(int i=0;i<size;i++)
                visit[i]=0;//初始化访问数组
            Queue<Person> queue=new LinkedList<>();
            int indexp1=getMatrixRow(P1);
            visit[indexp1]=1;
            queue.offer(P1);boolean find=false;
            while(!queue.isEmpty()) {
            	Person tp=queue.peek();
            	if(tp.equals(P2)) {
            	      if(tp.equals(P2)) {
                          find = true;
                          queue.clear();//所有元素删除
                          break;
                      }
            	}
            	else{
                    dis++;
                    int index=this.getMatrixRow(tp);
                    ArrayList<Relation> rel=this.RelationList(index);//拿出一行
                    for(int i=0;i<rel.size();i++){
                        int num=this.getMatrixRow(rel.get(i).to());
                        if(visit[num]==0){
                            visit[num]=1;
                            Person next = rel.get(i).to();
                            queue.offer(next);
                        }
                    }
                    queue.poll();//出队一个
                }
            }
            if(find)
                return dis;
            else
                return -1;
            }
		}

这里给出菜鸟教程中的一篇笔记:
在这里插入图片描述
5.动态二维数组的访问
这其实在之前的文章中已经有提到了,但是对于图结构,动态二维数组是我比较喜欢用的,再记录一下

ArrayList<ArrayList<type>> graph=new ArrayList<>();
graph.get(i).get(j);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值