数据结构与算法分析(Java)3.4、3.5 交集和并集

问题

代码

主程序

package Chapter3; /** * * @author Crissu * 交集和并集 */ public class Three_Four {  public static void main(String[] args) {  // TODO Auto-generated method stub //创建list1 MyArrayList<Integer> list1 = new MyArrayList<>(); list1.add(1); list1.add(3); list1.add(5); list1.add(7); list1.add(9); // 创建list2 MyArrayList<Integer> list2 = new MyArrayList<>(); list2.add(1); list2.add(2); list2.add(5); list2.add(7); list2.add(8); list2.add(9); // 交集 MyArrayList<Integer> mergeList; mergeList = JiaoJi(list1, list2); // 输出 System.out.println("交集"); for(int i=0; i<mergeList.size(); i++) {  System.out.print(mergeList.get(i)); } System.out.println(); // 并集 mergeList = BingJi(list1, list2); // 输出 System.out.println("并集"); for(int i=0; i<mergeList.size(); i++) {  System.out.print(mergeList.get(i)); } System.out.println(); } /** * 交集 * @param list1 * @param list2 * @return */ private static MyArrayList<Integer> JiaoJi(MyArrayList<Integer> list1, MyArrayList<Integer> list2) {  // TODO Auto-generated method stub MyArrayList<Integer> mergeList = new MyArrayList<>(); int index1 = 0; int index2 = 0; int v1, v2; while(index1 < list1.size() && index2 < list2.size()) {  v1 = list1.get(index1); v2 = list2.get(index2); if(v1 < v2) {  index1++; }else if(v1 > v2) {  index2++; }else {  mergeList.add(v1); index1++; index2++; } } return mergeList; } /** * 并集 * @param list1 * @param list2 * @return */ private static MyArrayList<Integer> BingJi(MyArrayList<Integer> list1, MyArrayList<Integer> list2) {  // TODO Auto-generated method stub MyArrayList<Integer> mergeList = new MyArrayList<>(); int index1 = 0; int index2 = 0; int v1, v2; while(index1 < list1.size() && index2 < list2.size()) {  v1 = list1.get(index1); v2 = list2.get(index2); if(v1 < v2) {  mergeList.add(v1); index1++; }else if(v1 > v2) {  mergeList.add(v2); index2++; }else {  mergeList.add(v1); index1++; index2++; } } // 处理剩下的 if(index1 == list1.size()) {  //说明list2还有剩下 for(; index2<list2.size(); index2++) {  v2 = list2.get(index2); mergeList.add(v2); } } if(index2 == list2.size()) {  //说明list1还有剩下 for(; index1<list1.size(); index1++) {  v1 = list1.get(index1); mergeList.add(v1); } } return mergeList; } } 

MyArrayList

package Chapter3; import java.util.Iterator; /** * * @author Crissu * * @param <AnyType> */ public class MyArrayList<AnyType> implements Iterable<AnyType> {  private static final int DEFAULT_CAPACITY = 10; private int theSize; private AnyType[] theItems; public MyArrayList() {  doClear(); } public void clear() {  clear(); } public int size() {  return theSize; } public boolean isEmpty() {  return size()==0; } public void trimToSize() {  ensureCapacity(size()); } public AnyType get(int idx) {  if(idx < 0 || idx >= size()) {  throw new ArrayIndexOutOfBoundsException(); } return theItems[idx]; } public AnyType set(int idx, AnyType newVal) {  if(idx < 0 || idx >= size()) {  throw new ArrayIndexOutOfBoundsException(); } AnyType old = theItems[idx]; theItems[idx] = newVal; return old; } public boolean add(AnyType x) {  add(size(), x); return true; } public void add(int idx, AnyType x) {  if(theItems.length == size()) {  ensureCapacity(size()*2+1); } for(int i = theSize; i>idx; i--) {  theItems[i] = theItems[i-1]; } theItems[idx] = x; theSize++; } public AnyType remove(int idx) {  AnyType removedItem = theItems[idx]; for(int i=idx; i<size()-1; i++) {  theItems[i] = theItems[i++]; } theSize--; return removedItem; } private void doClear() {  // TODO Auto-generated method stub theSize = 0; ensureCapacity(DEFAULT_CAPACITY); } private void ensureCapacity(int newCapacity) {  // TODO Auto-generated method stub if(newCapacity < theSize) {  return; } AnyType[] old = theItems; theItems = (AnyType [])new Object[newCapacity]; for(int i=0; i< size(); i++) {  theItems[i] = old[i]; } } public java.util.Iterator<AnyType> iterator(){  return new ArrayListIterator(); } private class ArrayListIterator implements java.util.Iterator<AnyType>{  private int current = 0; @Override public boolean hasNext() {  // TODO Auto-generated method stub return current<size(); } @Override public AnyType next() {  // TODO Auto-generated method stub if(!hasNext()) throw new java.util.NoSuchElementException(); return theItems[current++]; } public void remove() {  MyArrayList.this.remove(--current); } } } 

结果

全部评论

相关推荐

10-15 15:00
潍坊学院 golang
跨考小白:这又不是官方
投递拼多多集团-PDD等公司10个岗位
点赞 评论 收藏
分享
Yushuu:你的确很厉害,但是有一个小问题:谁问你了?我的意思是,谁在意?我告诉你,根本没人问你,在我们之中0人问了你,我把所有问你的人都请来 party 了,到场人数是0个人,誰问你了?WHO ASKED?谁问汝矣?誰があなたに聞きましたか?누가 물어봤어?我爬上了珠穆朗玛峰也没找到谁问你了,我刚刚潜入了世界上最大的射电望远镜也没开到那个问你的人的盒,在找到谁问你之前我连癌症的解药都发明了出来,我开了最大距离渲染也没找到谁问你了我活在这个被辐射蹂躏了多年的破碎世界的坟墓里目睹全球核战争把人类文明毁灭也没见到谁问你了😆
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务