Java动态数组(一维)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_41723615/article/details/88413667

如何写出动态的数组,需要运用泛型技术。

        接下来将是一个完整的demo:

demo的功能有:

 1.向数组中添加元素

 2.在数组中查找元素

 3.在数组中修改元素

 4.在数组中判断是否包含该元素

 5.在数组中搜索寻找元素

 6.在数组中删除元素

public class Array<E> {

	//定义一个int型的数组
	private E[] data;
	//size表示数组有多少个有效元素
	private int size;
	
	/**
	 * @constructor: Array
	 * @param: int capacity
	 * 传入数组的容量capacity构造Array
	 * */
	public Array(int capacity){
		//为数组设置长度
		data = (E[])new Object[capacity];
		//初始时size=0
		size = 0;
	}
	
	//无参数的构造函数,默认数组的的容量capacity=10
	public Array(){
		this(10);
	}
	
	//获取数组中的元素个数
	public int getSize(){
		return size;
	}
	
	//获取数组的容量
	public int getCapacity(){
		return data.length;
	}
	
	// 返回数组是否为空
    public boolean isEmpty(){
        return size == 0;
    }
    
    // 在index索引的位置插入一个新元素e
    public void add(int index, E e){

        if(index < 0 || index > size){
            throw new IllegalArgumentException("Add failed. Require index >= 0 and index <= size.");
        }
        
        if(size == data.length){
        	//扩容两倍
        	resize(2 * data.length);
        }
        //定义最后一个元素i的索引为size-1,插入元素时,如果i>=index,i就向后移动一个位置
        for(int i = size - 1; i >= index ; i --){
        	//让后一个索引位置赋上前一个位置对应的元素
            data[i + 1] = data[i];
        }
        data[index] = e;

        size ++;
    }
    
    /**
	 * @method: addLast
	 * @param: int e
	 * 向所有元素后添加一个元素
	 * */
    // 向所有元素后添加一个新元素
    public void addLast(E e){
        add(size, e);
    }

    // 在所有元素前添加一个新元素
    public void addFirst(E e){
        add(0, e);
    }
    
    
    //获取index索引位置的元素
    E get(int index){
    	//对data进行了隐藏,通过get()方法才能获得
    	//好处:对用户传进来的的元素进行判断
    	if(index < 0 || index >= size){
    		throw new IllegalArgumentException("Get failed. Index is illegal.");
    	}
    	return data[index];
    }
    
    //修改index索引位置的元素为e
    void set(int index, E e){
    	if(index < 0 || index >= size){
    		throw new IllegalArgumentException("set failed. Index is illegal.");
    	}
    	data[index] = e;
    }
    
    
    //查找数组中是否有元素e
    public boolean contains(E e){
    	for(int i = 0 ; i < size ; i ++){
    		if(data[i].equals(e)){
    			return true;
    		}
    	}
    	return false;
    }
    
    //查找数组中元素e所在的索引,如果不存在元素e,则返回-1
    public int find(E e){
    	for(int i = 0 ; i < size ; i ++){
    		if(data[i].equals(e)){
    			return i;
    		}
    	}
    	return -1; 
    }
    
    //从数组中删除index位置的元素,返回删除的元素
    public E remove(int index){
    	if(index < 0 || index >= size){
    		throw new IllegalArgumentException("Remove failed. Index is illegal.");
    	}
    	E ret = data[index];
    	for (int i = index + 1 ; i < size; i++) {
			data[i - 1] = data[i];
		}
    	size --;
    	data[size] = null;  // loitering objects != memory leak
    	
        //符合条件就减容
    	if(size == data.length / 4 && data.length / 2 != 0){
    		resize(data.length / 2);
    	}
    	return ret;
    }
    
    //从数组中删除第一个元素,返回删除的元素
    public E removeFirst(){
    	return remove(0);
    }
    
    //从数组中删除最后一个元素,返回删除的元素
    public E removeLast(){
    	return remove(size - 1);
    }
    
    //从数组中删除元素e
    public void removeElement(E e){
        int index = find(e);
        if(index != -1){
        	remove(index);
        }
    }
    
    @Override
    public String toString() {
    	//拼接一个字符串
        StringBuilder res = new StringBuilder();
        //数组的信息
        res.append(String.format("Array:size = %d,capacity = %d\n", size , data.length));
        res.append('[');
        for(int i = 0 ; i < size ; i ++){
        	res.append(data[i]);
        	//判断i是否是最后一个元素
        	if(i != size - 1){
        		res.append(", ");
        	}
        }
        res.append(']');
    	return res.toString();
    }
    
    private void resize(int newCapacity){
    	E[] newData = (E[])new Object[newCapacity];
    	for(int i = 0 ; i < size ; i ++){
    		//扩容后把原本的数组放进newData[]中
    		newData[i] = data[i];
    	}
    	data = newData;
    }
}

写一个一维的动态数组是比较简单。

下面列出一些有关数组的编程题:

扫描二维码关注公众号,回复: 5577969 查看本文章

1.将一个字符数组的值(neusofteducation)拷贝到另一个字符数组中。(知识点:数组复制)

2.给出一个数组,对其排序并输出打印(冒泡排序等)

3.多维数组定义和创建、数组遍历、数组元素访问

4. 在一个有8个整数(18,25,7,36,13,2,89,63)的数组中找出其中最大的数及其下标

5.将一个数组中的元素逆序存放(知识点:数组遍历、数组元素访问)

6.将一个数组中的重复元素保留一个其他的清零。(知识点:数组遍历、数组元素访问)

7. 输出一个double型二维数组(长度分别为5、4,值自己设定)的值。(知识点:数组定义和创建、多维数组初始化、数组遍历)

8.给定一维数组{ -10,2,3,246,-100,0,5},计算出数组中的平均值、最大值、最小值。(知识点:数组遍历、数组元素访问)

等等更复杂的一些题目。。。以上题目是我在网上查找的。仅供参考。

对于数组的其他用法可以看:https://blog.csdn.net/qq_41723615/article/details/85456789

猜你喜欢

转载自blog.csdn.net/qq_41723615/article/details/88413667