Java Data Structures and Algorithms (ii)--arrays

Source: Internet
Author: User

What is the use of arrays? -When you need to arrange 30 numbers in size, it is a good choice to use data structures such as arrays, and when you are a class head teacher, it is also useful to keep track of the number of absences that students have. Arrays can be inserted, deleted, searched, and so on.


1) Creation and memory allocation

There are two data types in Java, primitive types and object types, and some are referred to as reference types, and in Java the array is treated as an object, and the new operator is used when creating an array.

int array[] = new INT[10];
Since it is an object, then array is a reference to the array, according to the Java Programming Idea (a)--everything is the memory allocation of the object, the array will open up space in the stack, and the space is stored in the storage of the address of the array, the real place to save the object is right, the new operation in the heap to open up the The array then 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 initialization of the object is empty, null, and of course it can be initialized by {}.


2) Use of the array after 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 (); }}

Wrap the entire array, use number in place of the array, insert the data without having to ignore which subscript to interpolate, of course, you can also customize a specific subscript method.


The method is relatively simple not to introduce, but there is a disadvantage in the delete there, in fact, only from the deletion of the beginning of the left to move the element, so, although the number is reduced, but the last element is not deleted, but display output is hidden when the display, but, The next time the element is inserted, the new element will replace the position of the last element.


3) Find optimization--two points to find

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;    }
A binary lookup is provided that the array is already in order. The index is initially subtracted from end and start, resulting in a dead loop. In fact, it is to add. 1,2,3,6,7. index=2,value=7,3 is less than 7,start=3, then index is the intermediate number between 3 and 4, so the sum is divided by 2,6 less than 7,start=4,find to 7.

Sort of words look at--java data structure and algorithm (three)--simple sorting.

The same principle is true for storing objects.


4) Large O-notation

Set N to the total number of data, insert a data time of K.

The total time for linear lookups is T=K*N/2, because the search is about half the number of comparisons.

Two points to find the words t=k*log2 (N).

Large o notation, O can be regarded as order of, about meaning, K/2 is also constant, so can be seen as O (N).


The disadvantage of the array, is the size fixed, look slow, if you want to find millions data, you will also use an array? No, so the choice of data structure should be combined with the actual situation, to achieve the maximum efficiency value.

Java Data Structures and Algorithms (ii)--arrays

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.