数据结构与算法分析(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个

红包金额最低5元

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

抵扣说明:

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

余额充值