Data structure review:Array

I. Arrays

1.创建数组

create arrays(Initialization——YOU CAN SET THE CAPACITY OF THE ARRAY*

e.g. new int[5] data;

2.获取数组长度length

get the length of the array

3.把每一个元素取出来(用“:“”遍历)

get the value of every element (You can use colon to tranverse all the elements)

4.注意数组最好的应用场景是索引有语义的情况(因为数组最大的优点在于可以快速查询)

The best condtion to choose array is to connect the indexes to specific meaning (for the purpose of searching quickly)

II. Implementation of Array

1.Build Array class

public class Array {
    
    private int[] data;
    private int size;
    /**
     * 
     * @param capacity
     */
    public Array(int capacity) {
        data = new int[capacity];
        size = 0;
    }
    
    public Array() {
        this(10);
    }

    public int getSize() {
        return size;
    }
    
    public int getCapacity() {
        return data.length;
    }
    
    public boolean isEmpty() {
        return data.length==0;
    }
}

问题:构造函数没写对,注意理解两个构造函数的作用:1.给定数组的容量但没有填值时,注意要给size赋初值0;2.没有给定数组容量时,要给数组一个默认值

2. Add elements

 

posted @ 2019-01-11 06:16  cecilia_xu  阅读(59)  评论(0编辑  收藏  举报