Java data structure and algorithm (2) -- Array

Source: Internet
Author: User

Java data structure and algorithm (2) -- Array

What is the use of arrays? -- When you need to arrange 30 numbers in size, it is a good choice to store data structures like arrays. When you are a class teacher, the array is also useful every time you want to record the number of absences of those students. You can insert, delete, and search arrays.


1) Creation and Memory Allocation

Java has two data types: basic and object types, also known as reference types. in Java, arrays are treated as objects and the new operator is used to create arrays.

int array[] = new int[10];
Since it is an object, array is a reference of the array. According to Java programming philosophy (1)-everything is the memory allocation of the object, array will open up space in the stack, in addition, the space stores the address for storing the array storage. The place where the object is actually saved is correct. The new operation opens up the required space in the heap, and then the array points to the header address.


Initialization:

public class UseArray {    public static void main(String[] args) {        int array[] = new int[10];        System.out.println(array[2]);        UseArray a[] = new UseArray[12];        System.out.println(a[1]);        int array2[] ={1,2,3,4,5,5,6};    }}
The value in the array after new is initialized to 0 by default, and the object initialization is empty, null, of course, can also be initialized through.


2) array Encapsulation


public class UseArray {    private int[] array;    private int number = 0;    public UseArray(int max){        array = new int[max];    }    public void insert(int value){        array[number] = value;        number++;    }    public int find(int value){        int index = 0;        for (int i= 0; i < number; i++) {            if(array[i]==value)                return index;        }        return number;    }        public boolean delete(int value){        int index = find(value);        if(index != number){            for (int i = index; i < number-1; i++) {                array[i] = array[i+1];            }            number--;            return true;        }        return false;    }        public void display(){        for (int i = 0; i < number; i++) {            System.out.printf(array[i]+" ");        }    }    public static void main(String[] args) {        UseArray ua  = new UseArray(5);        ua.insert(1);        ua.insert(2);        ua.insert(6);        ua.insert(7);        ua.insert(3);                ua.display();        if(ua.find(5) != ua.number){            System.out.println();        }else{            System.out.println("not found!");        }        if(ua.delete(5)!=true){            System.out.println("can not delete!");        }        ua.display();    }}

Encapsulate the entire array and use number to replace the number of arrays. when inserting data, you do not have to worry about which subscript to insert. Of course, you can also customize a specific method for downloading.


The method is simple and I will not introduce it, but one of the shortcomings is that the delete method only shifts left from the deletion of elements. Therefore, although the number is reduced, however, the last element is not deleted, but hidden when the display output is displayed. However, the position of the last element will be replaced by the new element when the element is inserted next time.


3) Search Optimization-Binary Search

public int find(int value){        int start = 0;        int end = number-1;        while(end>start){            int index =(end + start)/2;             if(array[index]==value){                return index;            }else if(array[index] >value){                end = index-1;            }else {                 start = index+1;            }        }        return number;    }
The prerequisite for Binary Search is that the array is ordered. At the beginning, the index is written as end and start subtraction, resulting in an endless loop. What we need is addition. 1, 2, 3, 6, and 7. Index = 2, value = 7, 3 less than 7, start = 3, then the index requires the intermediate number between 3 and 4, so it is the sum divided by less than 7, start = 4, find to 7.

For sorting, see -- Java data structure and algorithm (3) -- simple sorting.

The same principle applies to storage objects.


4) Large O notation

Set N to the total number of data, and add the insertion time to K.

The total linear search time T = K * N/2, because the query time is about half of the comparison time.

For binary search, T = k * log2 (N ).

In large O notation, O can be regarded as order of, which is about to mean, k/2 is also a constant, so it can be considered as O (N ).


The disadvantage of the array is that the size is fixed and the query is slow. If you want to find millions of data records frequently, will the array be used? No, so the data structure should be selected based on the actual situation to maximize the efficiency.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.