Java data structure tells you how to select data sets (2) order table

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/ZackSock/article/details/100779642

Then the contents of the last to speak in detail today, to achieve a sequence table in Java. It took the name MyArrayList, a little casual. Last spoke, realization of sequence table is implemented using an array, then the order of the table at the time of writing requires a member of the array. But an array of fixed-length, additions and deletions to how to achieve it? Realization idea is as follows, and then explained in detail later:

1, define a variable size, to indicate the length of the array, take a reasonable initial value

2,1, create a fixed-length arrays, length size

3, the definition of a variable length represents the length of MyArrayList (noted here that, instead of the length of the array)

So how to achieve, first create MyArrayList when the array created. This time is the length of the array size, the length is 0 MyArrayList. Among the MyArrayList, size and length are two different values. size is the actual length of the array, and length is the length that we inform others of this order of the table. Then the member variables of this class are as follows:

public class MyArrayList<T> {
    //用来存数据的数组
    private T[] data;
    //数组的长度
    private int size = 100;
    //顺序表的长度
    private int length = 0;

    /**
    *    构造方法
    */
    public MyArrayList(){
        //在创建MyArrayList时,创建数组
        data = (T[])new Object[size];
    }

The above code is very simple, because the code common to use generics. So we look to achieve several other important ways, first added in the tail:

/**
* 将数据插入到对尾
* @param value 插入的值
*/
public void add(T value){
    //如果长度没有超过size,就直接添加元素
    if(length < size){
        data[length] = value;
    }else{
        //原数组不够用
        moreData();
        data[length] = value;
    }
    //长度增长
    length++;
}

Length of no more than size when we all know, directly in the array elements plus a final surface just fine. But more than size to add, then, will Subscript out of range, this time need additional treatment. Here the use of self-moreData defined () method, because there is still specify the location to add data but also use this method. Together to tell you the following, let's look at the specified location to add data:

/**
* 指定下标插入
* @param value 插入的值
* @param index 插入的下标
*/
public void add(T value, int index){
    if(index >= size){
        System.out.println("下标越界无法添加");
    }else if(length + 1 > size){
        moreData();
        addDataByIndex(value, index);
    } else{
        addDataByIndex(value, index);
    }
}

Determining whether the first out of range, then the length of the array is determined not enough. Here, then there are a custom method addDataByIndex (), since the repetition of the relationship, is taken out. Below we look at moreData () and addDataByIndex ():

/**
* 当元素组不够用时调用此方法
*/
private void moreData(){
    //把data数组原有的数据取出
    T[] temp = data.clone();
    //从新创建一个更长数组
    size += 50;
    data = (T[])new Object[size];
    //把temp中的数据复制回来并添加新的值
    for (int i = 0; i < temp.length; i++){
        data[i] = temp[i];
    }
}

Because the length of the array can not change's sake, when the length is not enough, only to re-create an array. However, before creating, to the original first data record array (here the array with the temp), then put into the original data after data array becomes long.

Next is addDataByIndex () method:

/**
* 通过下标添加数据的实现
* @param value 插入的值
* @param index 插入的下标
*/
private void addDataByIndex(T value, int index){
    //创建一个数组存储index后面的数据
    T[] temp = (T[])new Object[length - index];
    for(int i = index, j = 0; i < length; i++, j++){
        temp[j] = data[i];
    }
    //插入数据
    data[index] = value;
    //补足后面数据
    for(int i = index + 1, j = 0; i < length+1; i++, j++){
        data[i] = temp[j];
    }
    length++;
}

First save data index from the start down, then insert the data to be inserted in the index, and then re-index the data behind the original assignment. In fact, in fact, removed from the index elements start.

The next step is deleting data. Here, then my convenience, to simplify a bit, does not consider the size of size:

/**
* 删除队尾的数据
*/
public void delete(){
    if(length > 0){
        length--;
        //复制数组中的数据
        T[] temp = data.clone();
        data = (T[])new Object[size];
        for(int i = 0; i < length; i++){
            data[i] = temp[i];
        }
    }else{
        System.out.println("没有可以删除的元素了");
    }
}

Here it is to copy the data to temp them, and then re-create a data. In addition to the data and then the tail assignment back data. There should be other ways to here only as a reference.

Followed by the subscript deleted:

/**
* 删除指定位置的数据
* @param index 下标
*/
public void delete(int index){
    if(index < 0 || index > length - 1){
        System.out.println("下标越界了");
    }else{
        length--;
        T[] temp = data.clone();
        data = (T[])new Object[size];
        for(int i = 0; i < length; i++){
            if(i < index){
                data[i] = temp[i];
            }else{
                data[i] = temp[i + 1];
            }
        }
    }
}

To this is the limit of the index, is in front of the usual assignment, the assignment will later after a (i + 1) th up. There are other methods generally, not described in detail here realized.

Let's solve two problems:

① Why order table query faster?

  Here we do not have to implement the query method, but we know the underlying sequence table is achieved through the array. It can be said query is a query sequence array table, and the data stored in adjacent memory, so fast query time.

② Why order table insert slow?

  We have achieved insertion and deletion methods, you will find multiple copies. We inserted in the pair, probably such a step: to save the index data at the tail -> Insert Data -> save data assignment back. Delete probably such a similar process. This consumes a lot of resources, so slowly. Feel what little data when a large amount of data, there tannoy difference. So learning data structure is still very necessary, it first here today.

Guess you like

Origin blog.csdn.net/ZackSock/article/details/100779642
Recommended