Java自写顺序表结构,删除的元素重复

问题是这样的 这个是输出的结果

我的代码是这样的

package com.gen.test.sequence;

import java.util.Arrays;

public class SeqList<T> implements IList<T> {
//	顺序表元素的个数
	private int size;
	private Object[] elems;
	private static final int MIN_CAPACITY = 16;

//	根据长度构造空表
	public SeqList(int length) {
		if (length < MIN_CAPACITY)
			length = MIN_CAPACITY;
//		申请数组空间
		this.elems = new Object[length];
		this.size = 0;
	}

	public SeqList() {
//		调用本类已声明的构造方法
		this(MIN_CAPACITY);
	}

//	构造顺序表
	public SeqList(T[] values) {
		this((int) (values.length * 1.5));
		for (int i = 0; i < values.length; i++) {
			if (values[i] != null)
				this.elems[this.size++] = values[i];
			else
				throw new NullPointerException(i + "null element!");
		}
	}

	@Override
	public Boolean isEmpty() {
		return this.size == 0;
	}

	@Override
	public int size() {
		return this.size;
	}

	@Override
	public int search(T key) {
		for (int i = 0; i < this.elems.length; i++) {
			if (this.elems[i].equals(key)) {
				return i;
			}
		}
		return -1;
	}

	@Override
	public T remove(T key) {
		int pos = search(key);
		if (pos == -1) {
			return null;
		} else {
//			删除此元素
			T t = (T) this.elems[pos];
			for (int i = pos; i < this.size - 1; i++) {
				this.elems[i] = this.elems[i + 1];
			}
			this.elems[this.size-1] = null;
			this.size -= 1;
			return t;
		}
	}

	@Override
	public void clear() {
		for (int i = 0; i < this.size; i++) {
			this.elems[i] = null;
		}
		this.size = 0;
	}

	@Override
	public String toString() {
		if (this.size == 0) {
			return "[]";
		}
		StringBuffer buffer = new StringBuffer("[");
		for (int i = 0; i < this.size - 1; i++) {
			buffer.append(this.elems[i] + ", ");
		}
		buffer.append(this.elems[this.size - 1]).append("]");
		return buffer.toString();
	}

	@Override
	public Boolean isEquals(Object[] obj) {
		boolean flag = true;
		if (this.size != obj.length) {
			return false;
		}
		for (int i = 0; i < this.size; i++) {
			if (!this.elems[i].equals(obj[i]))
				return false;
		}
		return flag;
	}

	@Override
	public T get(int i) {
		if (i >= 0 && i < this.size)
			return (T) this.elems[i];
		return null;
	}

	@Override
	public void set(int i, T x) {
		if (x == null)
			throw new NullPointerException("x==null");
		if (i >= 0 && i < this.size)
			this.elems[i] = x;
		else
			throw new IndexOutOfBoundsException(i + " ");

	}

	@Override
	public int add(int i, T x) {
		int flag = 1;
		if (x == null)
			throw new NullPointerException("x==null");
		if (i >= 0 && i < this.size) {
			for (int j = this.size; j > i; j--) {
				this.elems[j] = this.elems[j - 1];
			}
			this.elems[i] = x;
			this.size++;
			return flag;
		} else
			throw new IndexOutOfBoundsException(i + " ");
	}

	@Override
	public int add(T x) {
		this.elems[this.size] = x;
		this.size++;
		return 1;
	}

	@Override
	public T remove(int i) {
		if (isEmpty())
			return null;
		if (i >= 0 && i < this.size) {
			for (int j = i; j < this.size-1; j++)
				this.elems[j] = this.elems[j + 1];
			this.size--;
			return (T) this.elems[i];
		} else
			throw new IndexOutOfBoundsException(i + " ");
	}

}


自己写的测试代码

package com.gen.test.sequence;

import java.util.ArrayList;

public class Demo {
	public static void main(String[] args) {
		new Josephus1(26, 0, 3);
	}
}

class Josephus1 {
	public Josephus1(int n, int start, int distance) {
		try {
			if (n < 0 || distance <= 0 || distance >= n)
				throw new IllegalArgumentException(" IllegalArgumentException");
			SeqList<String> list = new SeqList<String>(n);
//			ArrayList list = new ArrayList();
			
//			System.out.println(list.elems.length);
			for (int i = 0; i < n; i++)
				list.add((char) ('A' + i) + "");
			System.out.println(list);
			StringBuffer killedList = new StringBuffer();
			while (list.size() > 1) {
				start = (start + distance - 1) % list.size();
//				start = list.size-1;
				String killed = list.remove(start);
				killedList.append(killed);
			}
			System.out.println(list.get(0));
			System.out.println(killedList);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

模拟的是约瑟夫环的问题 但是结果里面有重复的数据

就显得很匪夷所思
Java自写顺序表结构,删除的元素重复_第1张图片
希望得到帮助,谢谢!


后来的故事

Java自写顺序表结构,删除的元素重复_第2张图片
懂得都懂,remove(object)的时候知道考虑返回值会被覆盖,为啥remove(i)的时候不会考虑返回的值会被覆盖呢?

运行

Java自写顺序表结构,删除的元素重复_第3张图片

完美收工!!

你可能感兴趣的:(Java学习资料,Java疑难,java,数据结构,算法)